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

TransactionDefinition is an Interface which specifies below 4 points.

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

Isolation

Degree to which particular transaction is isolated from other Transaction.

Below is the list of Isolation level and their details

DEFAULT

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

READ_COMMITTED

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

READ_UNCOMMITTED

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

REPEATABLE_READ

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

SERIALIZABLE

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

 

Propagation

MANDATORY

Support a current transaction

NESTED

Execute within a nested transaction if a current transaction exists

NEVER

Execute non-transactionally

NOT_SUPPORTED

Execute non-transactionally

REQUIRED

Support a current transaction

REQUIRES_NEW

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

SUPPORTS

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

Me & Jayshree Singh. Main brain behind this blog/QnA site. We have written all these article as per my personal experience. I have published it mainly to help. These articles might have some problems. So bear with me and let me know if you find any problem in any of the article. You can find me on

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">