Spring and transaction management

The Spring Framework excellently supports the integration of transaction managers. It supports Java Transaction API, JDBC, Hibernate, and Java Persistent APIs. The framework supports abstract transaction management known as transaction strategy. The transaction strategy is defined through service provider interface (SPI) through the PlatformTransactionManager interface. The interface has the methods to commit and rollback the transaction. It also has the method to get the transaction specified by TransactionDefinition. All of these methods throw TransactionException, which is a runtime exception.

The getTransaction() method returns TransactionStatus, depending upon the TransactionDefinition parameters. TransactionStatus returned by the method represents a new transaction or the existing one. The following parameters can be specified to define TransactionDefinition:

  • Propagation: This behavior comes in discussion when one transactional method invokes another. In such an invocation, propagation behavior states what transaction behavior it will perform. Let's consider an invoking method and an invoked method. The invoking method may have started the transaction; what should the invoked method do in such cases? Should the invoked method start a new transaction, use the current one, or not support the transaction? The propagation behavior can be specified using the following values:
    • REQUIRED: This says the transaction is a must. If no transaction exists, it will create a new one.
    • REQUIRES_NEW: This specifies having a new transaction every time. The current transaction will be suspended. If no transaction exists, it will create a new one.
    • MANDATORY: This states that the current transaction will be supported; however, if, in case of no ongoing transaction, an exception will be thrown.
    • NESTED: This states that if the current transaction exists, the method will be executed within a nested transaction. If no transaction exists, it will act as PROPAGATION_REQUIRED.
    • NEVER: This states the transaction is not supported, and if it exists, an exception will be thrown.
    • NOT_SUPPORTED: This states that the transaction is not supported. If transaction exists opposite to NEVER, it doesn't throw an exception, but it suspends it.
  • Isolation: We have already had an in-depth discussion about isolation levels.
  • Timeout: This is the timeout value for transaction mentioned in seconds.
  • Read only: This attribute states that the transaction is allowed to only read the data, and no operation leading to updating the data will be supported.

The following are the advantages of using the Spring Framework's transaction management:

  • It provides a lightweight and declarative syntax for transaction management
  • It provides support for flexible transaction propagation management
  • It provides a simple, consistent transaction management API for JTA, JDBC, and Hibernate
  • It provides support for easy transaction management for both declarative as well as programmatic transaction management

Now we know what transaction management is and its parameters to handle, let's dig it more to explore it further and understand the way Spring handles transactions.

Spring facilitates the use of transaction management in two ways, which are as follows:

  • Programmatic transaction management
  • Declarative transaction management

Whether we are using programmatic transaction or declarative transaction, the foremost important component is to define PlatformTransactionManager using dependency injection (DI). One should have a clear idea of how to use local or global transaction, as it is essential to define PlatformTransactionManager. The following are a few configurations that can be used to define PlatformTransactionManager:

  • Using DataSource and PlatformTransactionManager can be defined as follows:
    <bean id="dataSource" 
      <!-DataSource configuration --> 
    </bean> 
    <bean id="transactionManager" class= 
  "org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
      <property name="dataSource" ref="dataSource"/> 
    </bean> 
  • Using JNDI and JTA, define PlatformTransactionManager, as shown in the following piece of code:
<jee: jndi-lookup id="dataSource'jndi-name="jdbc/books"/> 
  <bean id="transactionManager" class= 
"org.springframework.transaction.jta.JtaTransactionManager"> 
</bean> 
  • Using HibernateTransactionManager, define PlatformTransactionManager as follows:
<bean id="sessionFactory" class= 
  "org.springframework.orm.hibernate5.LocalSessionfactoryBean" 
  <!-define parameters for session factory --> 
</bean> 
<bean id="transactionManager" class=
"org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/> </bean>

Let's start with using transaction management in Spring one by one.

..................Content has been hidden....................

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