What is Transaction Management

The sequence of activities that will be performed to complete database operation and its management is known as Transaction Management. All these actions 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 into 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 approaches. Spring transaction management tries to resolve the problem of both transactions. A consistent programming model approach can be used in any environment. The same code will work for different transactions management in different environments.

Different Approach for transaction management

Spring supports two different approaches to transaction management.

Programmatic Transaction Management

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

Declarative Transaction Management

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

Spring transaction management abstraction

To understand transaction management 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 the developer is not forced to handle these exceptions. 

TransactionDefinition

TransactionDefinition is an Interface that specifies below 4 points.

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

Isolation

The degree to which a particular transaction is isolated from other Transactions. Below is the list of Isolation levels and their details

DEFAULTDefault isolation level. It uses the isolation of the underlying data source.
READ_COMMITTEDDirty reads NOT supported; Non-repeatable reads and Phantom reads can occur.
READ_UNCOMMITTEDDirty reads / Non-repeatable reads / Phantom reads all can occur.
REPEATABLE_READDirty reads and non-repeatable reads are prevented; phantom reads can occur.
SERIALIZABLEDirty reads, non-repeatable reads and phantom reads are prevented.

Propagation

MANDATORYSupport a current transaction
NESTEDExecute within a nested transaction if a current transaction exists
NEVERExecute non-transactionally
NOT_SUPPORTEDExecute non-transactionally
REQUIREDSupport a current transaction
REQUIRES_NEWCreate a new transaction, suspend the current transaction if one exists.
SUPPORTSSupport a current transaction, execute non-transaction if none exists.

Timeout

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 a read-only transaction. As read-only transactions do not modify any data. Hence it is optimized in some cases.

8 thoughts

  1. 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,
    P

    1. 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.

      Regards

      1. Hi,
        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?

  2. 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 ???

  3. http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Propagation.html

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

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

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

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

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

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

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

Leave a Reply

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.

Share
Back to top