Annotation-based transaction management

The @Transactionl annotation facilitates to develop annotation-based declarative transaction management that can be applied to interface level, class level, as well as method level. While using the proxies, we shouldn't apply the annotation that are having visibilities such as private, protected, or default. Though the @Transactional annotation can be applied either to an interface or its methods, Spring recommends to apply the annotation to either a concrete class or its methods. To enable the annotation-based support, one needs to configure the following configuration along with the transaction manager:

<bean id="transactionManager" class=" your choice of transaction 
  manager"> 
  <!-transaction manager configuration - -> 
</bean> 
<tx:annotation-driven transaction-manager="transcationManager"/> 

The transaction-manager attribute can be omitted if the bean written for PlatformTransactionManager has its name as transactionManager.

The following are the attributes that can be used to customize the behavior of the transaction with the @Transactional annotation:

  • value: This is to specify the transaction manager to be used
  • propagation: This is to specify the propagation behavior
  • isolation: This is to specify the isolation levels
  • readonly: This is to specify the read or write behavior
  • timeout: This is to specify the transaction timeout
  • rollbackForClassName: This is to specify the array of exception classes that causes the transaction to rollback
  • rollbackFor: This is to specify the array of exception classes that causes the transaction to rollback
  • noRollbackFor: This is to specify the array of exception classes that doesn't cause the transaction to rollback
  • noRollbackForClassName: This is to specify the array of exception classes that doesn't cause the transaction to rollback

Let's use the @Transactional annotation to demonstrate declarative transaction management in the application instead of programmatic transaction management with the help of the following steps:

  1. Create Ch05_Declarative_Transaction_Management and add the required jars, as we did in the earlier application.
  2. Copy com.packt.ch03.beans and com.packt.ch03.dao from the Ch05_PlatformTransactionManager application.
  3. Copy the BookService.java interface in the com.packt.ch05.service packages.
  4. Create a BookServiceImpl class in the com.packt.ch05.service package and add a data member of type BookDAO.
  5. Annotate the data member of type BookDAO with @Autowired.
  6. Annotate searchBook () with @Transactional(readOnly=true) and write the code to search data using JdbcTemplate. The class will be as follows:
@Service(value = "bookService") 
public class BookServiceImpl implements BookService { 
 
  @Autowired 
  BookDAO bookDAO; 
 
  @Override 
  @Transactional(readOnly=true) 
  public Book searchBook(longISBN)  
  { 
    Book book = bookDAO.serachBook(ISBN); 
    return book; 
  } 
  1. Copy connection_new.xml from Ch05_PlatformTransactionManager in the classpath.
  2. Now we will need to tell the Spring Framework to find out all the beans that are annotated by the @Trasnactional annotation. It will be simply done by adding the following configuration in XML:
<tx:annotation-driven /> 
  1. To add the preceding configuration, we will first have to add tx as a namespace in XML. Update the schema configuration from connection_new.xml, as follows:
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans.xsd 
  http://www.springframework.org/schema/context  
  http://www.springframework.org/schema/ 
    context/spring-context.xsd 
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx.xsd"> 
  1. Now, we can add the following configuration:
<tx:annotation-driven /> 
  1. Copy MainBookService_operation.java and execute it to get the output.
  2. Now, add the addBook() method to understand readOnly=true. The code is written as follows:
@Transactional(readOnly=true) 
public boolean addBook(Book book) { 
  if (searchBook(book.getISBN()).getISBN() == 98564567l) { 
    System.out.println("no book"); 
    int rows = bookDAO.addBook(book); 
 
    if (rows> 0) { 
      return true; 
    } 
  } 
  return false; 
} 
  1. Execute MainBookService_operation.java to get the following output specifying that the read-only transaction is not allowed to modify data:
  1. Edit addBook() to remove the read-only transaction by specifying read-only=false, which is the default behavior of the transaction.
  2. The main code will execute, successfully performing the operations.
Use programmatic transaction management if the application has few transaction operations using TransactionTemplate. In case of having numerous transactional operations to keep it simple and cohesive, choose declarative transaction management.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.67.251