Spring Transaction Management – Types and Methods

1. Objective

In our last tutorial, we studied Spring JDBC Framework. Today, we will be learning about Spring Transaction Management. In this article, you will learn about the types of Spring Transaction Management.
Transaction Management in Spring Framework is an important part of RDBMS. A database transaction can be defined as a set of actions which are treated as a single unit. These actions can be either complete or incomplete which have no effect at all.
So, let’s start Spring Transaction Management Tutorial.

Spring Transaction Management

Spring Transaction Management

2. Spring Transaction Management

The concept of Spring Transaction Management can be described as a key property called as an ACID:

  • Atomicity- It says that transaction is atomic in nature i.e. it is either full or it isn’t.
  • Consistency- It says that after the end of the transaction the system should be in a be a valid state.
  • Isolation- It says that all the transactions of the system should be done in isolation.
  • Durability- It says that a transaction should be durable when all the changes made to the system are permanent.

Let’s Discuss the Features of Spring Framework

Spring Transaction Management - Types and Methods

Spring Transaction Management – Spring ACID

Spring Transaction Management

Spring Transaction Management

3. Types of Transaction Management in Spring Management

The Spring Framework supports following types of Transaction Management:

a. Programmatic Transaction Management in Spring

Spring Programmatic Transaction Management allows you to manage transaction with programming in your source code. It gives you lot of flexibility which might be hard to maintain. For Programmatic approach, PlatformTransactionManager is used for implementation. For creating new transactions, you need an instance of TransactionDefinition. You should create ofDefaultTransactionDefinition instances for using default transaction attributes. Once the TransactionDefinition gets created you can call getTransaction() for starting a transaction. This method returns an instance of TransactionStatus which helps in tracking current status. If everything goes well you can use commit() of PlatformTransactionManager.
Read About Spring Framework Architecture and 4 Modules

b. Declarative Transaction Management in Spring

Spring Declarative Transaction Management allows you to manage transaction using configuration, unlike Programmatic approach. Declarative Transaction allows separating transactions from business code. So, you can use XML configurations for managing transactions. The bean configuration helps in specifying the methods to be transactional. Some of the steps required for the Declarative approach:

  • Make use of the tag <tx: advice /> for creating transaction-handling advice. Also, at the same time define pointcut which matches all methods you wish to make a transaction.
  • If the method is there in the configuration then the advice will begin transaction before calling the method.
  • The Target() will get executed in a try block.
  • If Target() finishes the AOP commits transaction successfully else rollback happens.

c. Spring Transaction Abstractions

Spring Transaction Management uses org.springframework.transaction.PlatformTransactionManager which is defined as below:

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

Some above methods are described as follow:
i. TransactionStatus getTransaction(TransactionDefinition def) 
This method is for returning current active transaction or creates a new one.
ii. Void commit(TransactionStatus stat)
This method commits when the given transaction gets completed.
iii. Void rollback(TransactionStatus stat)
This method does rollback of the given transaction.
The TransactionDefinition is a core interface which is defined as follows along with the description of the methods:

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

Do you know How to Design Customised Events in Spring Framework
iv. Int getPropagationBehavior()
This method returns the propagation behavior an integer. Spring offers all the transaction options similar to EJB CMT.
v. Int getIsolationLevel()
This method returns the degree of which transaction is separated from other transactions.
vi. String getName()
This method is of string type which returns the name of a transaction.
vii. Int getTimeout()
This is an integer method which returns the time up to which the transaction should get completed.
viii. Bool isReadOnly()
This Boolean type method returns whether the transaction is a read-only transaction or not.
Now the TransactionStatus interface which controls query transaction status and execution in Spring Framework along with its methods are as follows:

public interface TransactionStatus extends SavepointManager {
boolean isNewTransaction();
boolean hasSavepoint();
void setRollbackOnly();
boolean isRollbackOnly();
boolean isCompleted();

ix. Bool hasSavepoint()
This method returns Boolean type for the transactions. It tells whether the transaction internally carries savepoint or not.
x. Bool isCompleted()
This method returns Boolean type for the transactions. It tells whether the transactions are complete or not.
Let’s Learn about Integration of Spring Logging with log4j
xi. Bool isNewTransaction()
This method returns Boolean type. It returns true when the present transaction is new.
xii. Void setRollbackOnly()
This makes the transactions to be rollback only.
xiii. Bool isRollbackOnly()
It is a Boolean type method. This method returns whether the transaction is rollback only.
So, this was all about Spring Transaction Management Tutorial. Hope you like our explanation.

4. Conclusion

Hence, in this Spring Transaction Management Tutorial, you studied the meaning of the transaction along with its key ACID property. Also, you learned about the types of Spring transaction management and their methods. Furthermore, if you have any doubt feel free to ask in the comment section.
Related Topic- Spring Event Handling
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.