Search This Blog

Thursday, 11 October 2012

Attributes in Spring's @Transactional Annotation

In the previous few posts we saw how to use Spring's transaction management.We saw declarative and programmatic support of transactions in Spring. In case of Spring's declarative transaction management using annotations, there are five main attributes that matter:
1: Propagation: This decides if method should execute within an existing/new or no transaction.


Propagation Behavior
PROPAGATION_REQUIRED Support a current transaction, create a new one if none exists.This is the default setting. If an existing transaction is in progress, method will run within that transaction, else a new transaction will be started.
PROPAGATION_SUPPORTS Support a current transaction, execute non -transactionally if none exists.
PROPAGATION_MANDATORY Support a current transaction, throw an exception if none exists.
PROPAGATION_REQUIRES_NEW Create a new transaction, suspend the current transaction if one exists
PROPAGATION_NOT_SUPPORTED Execute non - transactionally, suspend the current transaction if one exists.
PROPAGATION_NEVER Execute non - transactionally, throw an exception if a transaction exists
PROPAGATION_NESTED Method should run within a nested transaction if an existing transaction exists.The nested transaction can be committed/roll backed independently of the enclosing transaction. If no enclosing transaction behave like PROPAGATION_REQUIRED.
Propagation.REQUIRED is the default value.

2: Isolation Level:
This map to the isolation attribute of ACID properties. It decides the level of isolation between transactions.

Isolation Level Behavior
DEFAULT Use the default isolation level of the underlying database.
READ_UNCOMMITTED Allows a row changed by one transaction to be read by another transaction before any changes in that row have been committed. This means dirty reads, non-repeatable reads and phantom reads can occur. This is equivalent to Connection.TRANSACTION_READ_UNCOMMITTED. It is also the most efficient isolation level.
READ_COMMITTED It prohibits a transaction from reading a row with uncommitted changes in it. This prevents dirty reads, but non-repeatable reads and phantom reads can occur. This is equivalent to Connection.TRANSACTION_READ_COMMITTED.
REPEATABLE_READ It prohibits a transaction from reading a row with uncommitted changes in it, Dirty reads and non-repeatable reads are prevented; phantom reads can occur. This is equivalent to Connection.TRANSACTION_REPEATABLE_READ.
SERIALIZABLE It prohibits dirty reads, non-repeatable reads and phantom reads. It prevent all problems but is the least efficient isolation level.
DEFAULT is the default value.

3: Read only
If an operation does not modify the data in database, but simply reads it then a read-only transaction can be used. Read only transactions provide the database with an opportunity to apply additional optimizations.As these optimizations need to be made at the start of the transaction the value of true is useful only in case of PROPAGATION_REQUIRED, PROPAGATION_REQUIRES_NEW and PROPAGATION_NESTED. The default value is false.

4: Transaction timeout
To prevent long running transactions a timeout value can be set, which causes the transaction to rollback if it does not complete in the specified time. As the clock is started at the start of the transaction the value of true is useful only in case of PROPAGATION_REQUIRED, PROPAGATION_REQUIRES_NEW and PROPAGATION_NESTED.This value is in seconds. The default value is -1.

5:Rollback Rules:
Spring commits a transaction when the method executes successfully. So accordingly to rollback a transaction, the method must fail. Failure is identified by exceptions. By default when a method throws a run-time exception, Spring will rollback the transaction. The behavior can be modified to include any exception checked or unchecked. Spring also allows us to specify exceptions on which the rollback should not occur.

These five properties controls the transactional behavior of Spring in declared transactions.

4 comments:

  1. Really liked this post .Thanks a lot.

    ReplyDelete
  2. Great job, I want practical implementation. I am using spring3.1,Hibernate,JPA 2.0.
    In one form, when i submit that form insert data in four table . if exception occured during inserting reacord for fourth table then roll back all three table entry.
    waiting for reply

    ReplyDelete
  3. It is difficult to debug this issue with only so much information Ganesh, but here are certain things to look out for - are the updates all being done in one method ? If not are the methods running within the same transactional context ? (Am assuming you are dealing with a single run time exception being thrown here) You will have to lower the logs levels and check what Spring's rollback rules are saying.

    ReplyDelete
  4. Neat and precise explanation. Well Done Robin

    ReplyDelete