Scroll to top

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.

public interface PlatformTransactionManager {

  TransactionStatus getTransaction(TransactionDefinition definition)
    throws TransactionException;

  void commit(TransactionStatus status) throws TransactionException;

  void rollback(TransactionStatus status) throws TransactionException;

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.

Author avatar


  1. Pupsick

    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,

  2. copy paste from spring document

    • Quite true. Everything is written in Java Doc and Spring Doc. Every article or tutorial just try to highlight main parts and simplify things. Here in this article i didn’t add anything coz there was nothing to add apart from just highlighting the main point which is required for beginners. It was kind of notes for me when i was a beginners and i uploaded the same on net so that others get benefited with it. But in the end you can go to Spring or Java doc.


      • Himanshu

        Somehow I can not use @Transactional annotation and ( somehow I want to make timeout configurable and want to read it’s value from external property file, timeout is constant value and didn’t accept constant value).

        How can I make use of Transactional property using Spring’s java code ? is it possible?

  3. Harinath

    Thank you Sir…..

  4. Aanie

    Can you explain what are the values of each variable and what is returned in each argument like what is returned by definition ,what is returned by status,how commit works by taking different status ???

  5. Atul

    Support a current transaction, throw an exception if none exists.

    Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED else.

    Execute non-transactionally, throw an exception if a transaction exists.

    Execute non-transactionally, suspend the current transaction if one exists.

    Support a current transaction, create a new one if none exists.

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

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

Post a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.