Spring Transaction Management Tutorial

What is Transaction Management

Sequence of action that will be performed to complete database operation and its management is known as Transaction Management. All these action in combination will be treated as ONE action only. So that DB doesn’t fall in inconsistent mode ever. For more details you can search for ACID property of relation DB.

Type of Transaction Management

In J2EE, Transaction Management can be divided in two types.

  1. Global Transaction
  2. Local Transaction

Global Transaction

  • Use to work with multiple transaction resources like RDBMS or Message Queue (Pros)
  • Managed by Application Server (WebSphere, Weblogic) using JTA (Cons)
  • JNDI is required to use JTA
  • Code can not be reused as JTA is available at server level(Cons)
  • Example of Global Transaction : EJB CMT

Local Transaction

  • Use to work with specific resource(transaction associated with JDBC)
  • Can not work across multiple transaction resource opposite to Global transaction (cons)
  • Most of web application uses only single resources hence it is best option to use in normal app.

Spring Framework Transaction Management

As you can see above there are some pros and cons associated with both approach. Spring transaction management tries to resolve the problem of both transactions. Consistent programming model approach can be used in any environment. Same code will work for different transactions management in different environment.

Different Approach for transaction management

Spring supports two different approach for transaction management.

Programmatic Transaction Management

Here you will write code for transaction management.Spring API dependency. Not good for maintenance. Good for development. Flexibity.

Declarative Transaction Management

Here you will use XML or annotation for transaction management. Less flexible but preferable over programmatic approach. In normal case no code is required for transaction management.

Spring transaction management abstraction

To understand transaction mangement you should understand abstraction(Transaction strategy) in Spring. Which is defined in Spring using PlatformTransactionManager Interface.

As you can see in PlatformTransactionManager Interface all methods throw TransactionException. This Exception itself is UncheckedException means developer is not forced to handle these exceptions. 


TransactionDefinition is an Interface which specifies below 4 points.

  1. Isolation
  2. Propagation
  3. Timeout
  4. Read-Only status


Degree to which particular transaction is isolated from other Transaction. Below is the list of Isolation level and their details


Default isolation level. It uses the isolation of underlying datasource.


Dirty reads NOT supported; Non-repeatable reads and Phantom reads can occur.


Dirty reads / Non-repeatable reads / Phantom reads all can occur.


Dirty reads and non-repeatable reads are prevented; phantom reads can occur.


Dirty reads, non-repeatable reads and phantom reads are prevented.




Support a current transaction


Execute within a nested transaction if a current transaction exists


Execute non-transactionally


Execute non-transactionally


Support a current transaction


Create a new transaction, suspend the current transaction if one exists.


Support a current transaction, execute non-transaction if none exists.



This setting is used to define how long this transaction may run before timing out (candidate for rolled back by the underlying transaction infrastructure).

Read-Only Status

This setting is used to specify the a read-only transaction. As read only transactions does not modify any data. Hence it is optimized in some case.

About the author

J Singh

Main brain behind this blog/QnA site. She has written all these article as per her personal experience. She has published it mainly to help beginners. These articles might have some problems. So bear with her and let her know if you find any problem in any of the article.

1 Comment

  • This is a very good intro. Can you take it one step further and explain what happens inside SpringMVC and Hibernate when a user transaction span several forms and pages.

    Many thanks,

Leave a Comment