Transaction Management in Spring Framework

In the previous article, we have seen How to use JDBC in Spring Framework you can check it here How to use JDBC with Spring. In this article, we will understand Transaction Management in Spring.

What is Transaction Management?

While working with the RDMS database, every single unit of work is considered a Transaction. In the database term, it means, either all the operation is done or none. The main responsibility of Transaction Management is to ensure that all the data is consistent when the system fails or any system is restarted after some crash.

In RDMS, the transaction has the following properties:

  • Atomicity: It means, all the transaction is completed fully or it isn’t.
  • Consistency: It means, at the end of the transaction the system should be in a valid state.
  • Isolation: It means each transaction should be unaware of others to prevent data corruption.
  • Durability: It means, once the transaction ends the data should be there in the database and which cannot be erased even if the system fails.

Spring Framework Transaction Management

Spring Frameworks provides us an abstract layer of transaction management APIs. The Spring Framework has the following type of Transaction Management.

Programmatic

In this type, you manage the transaction using the programmatic way means in source code. It has a lot of flexibility but it is difficult to maintain.

  • PlatformTransactionManagement is used for implementation.
  • An instance of TransactionDefinition is created for new Transaction.
  • Then, create an Instance of DefaultTransactionManager to use the default transaction attribute.
  • Now, call getTransaction() to start a transaction. The getTransaction() method returns an instance of TransactionStatus which will help us to track the current status.
  • If everything is successful, then use the commit() method of PlatformTransactionManager.

Declarative

In this type, you manage the transaction using the configuration. Here, the Declarative Transaction will be separated from the business code. In this type, we can use an XML file or annotation-based configuration. Spring supports declarative transaction Management through the Spring AOP Framework. If you don’t know what is AOP check this article AOP in Spring Framework

  • Create the advice first, using the <tx: advice/> to create transaction handling.
  • Create the pointcut on the method on which you want to make a transaction.
  • If the method is present in the configuration file, then the advice will begin the transaction.
  • The Target() method will get called in a try block.
  • If Target() method has done the AOP commits, then the transaction will be successful or else rollback.

Spring Transaction Abstraction

In this type, It uses org.springframework.transaction.PlatformTransactionManager interface.

public interface PlatformTransactionManager {
   TransactionStatus getTransaction(TransactionDefinition definition);
   throws TransactionException;
   
   void commit(TransactionStatus status) throws TransactionException;
   void rollback(TransactionStatus status) throws TransactionException;
}

Let us understand the method in the PlatformTransactionManager interface:

  1. getTransaction(TransactionDefinition definition):  This method returns the active transaction going on or creates a new one.
  2. commit(TransactionStatus status): This method commits the changes when the transaction is successful.
  3. rollback(TransactionStatus status): This method does the rollbacks of the given transaction.

Here, we can see the TransactionDefinition is used which is the provides of the Transaction Support in Spring. It is defined as the following.

public interface TransactionDefinition {
int getPropagationBehavior();
int getIsolationLevel();
String getName();
int getTimeout();
boolean isReadOnly();
}

Let us understand the method in the TransactionDefinition interface:

  • getPropagationBehavior(): This method returns the propagation behavior
  • .getIsolationLevel(): This method returns the degree to which a transaction is separated from other transactions.
  • getName(): This method returns the name of the transaction.
  • getTimeout(): This method returns the time in which the transaction should be completed.
  • isReadOnly(): This method returns the boolean type whether it is READ-ONLY.

Along with these interfaces, there is one more interface called TransactionStatus which controls the query transaction status. It is defined as follows:

public interface TransactionStatus extends SavepointManager {
boolean isNewTransaction();
boolean hasSavepoint();
void setRollbackOnly();
boolean isRollbackOnly();
boolean isCompleted();
}
  • isNewTransaction() :  This method returns true f the transaction is new.
  • hasSavepoint(): This method tells us that whether a transaction carries any save point.
  • setRollbackOnly(): This method makes the transaction rollback.
  • isRollbackOnly(): This method returns true if the transaction is rollback only.
  • isCompleted(): This method tells us whether the transaction is completed or not.

Hence, this was all about Transaction Management in Spring Framework.