Programmatic vs. Declarative

Spring supports two types of transaction management:

  1. Programmatic transaction management: This means that you have manage the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain.
  2. Declarative transaction management: This means you separate transaction management from the business code. You only use annotations or XML based configuration to manage the transactions.

Declarative transaction management is preferable over programmatic transaction management though it is less flexible than programmatic transaction management, which allows you to control transactions through your code. But as a kind of crosscutting concern, declarative transaction management can be modularized with the AOP approach. Spring supports declarative transaction management through the Spring AOP framework.

Spring Transaction Abstractions

The key to the Spring transaction abstraction is defined by theorg.springframework.transaction.PlatformTransactionManager interface, which is as follows:

public interface PlatformTransactionManager {
   TransactionStatus getTransaction(TransactionDefinition definition);
   throws TransactionException;
   void commit(TransactionStatus status) throws TransactionException;
   void rollback(TransactionStatus status) throws TransactionException;
S.N. Method & Description
1 TransactionStatus getTransaction(TransactionDefinition definition)
This method returns a currently active transaction or create a new one, according to the specified propagation behavior.
2 void commit(TransactionStatus status)
This method commits the given transaction, with regard to its status.
3 void rollback(TransactionStatus status)
This method performs a rollback of the given transaction.

The TransactionDefinition is the core interface of the transaction support in Spring and it is defined as below:

public interface TransactionDefinition {
   int getPropagationBehavior();
   int getIsolationLevel();
   String getName();
   int getTimeout();
   boolean isReadOnly();
S.N. Method & Description
1 int getPropagationBehavior()
This method returns the propagation behavior. Spring offers all of the transaction propagation options familiar from EJB CMT.
2 int getIsolationLevel()
This method returns the degree to which this transaction is isolated from the work of other transactions.
3 String getName()
This method returns the name of this transaction.
4 int getTimeout()
This method returns the time in seconds in which the transaction must complete.
5 boolean isReadOnly()
This method returns whether the transaction is read-only.

Following are the possible values for isolation level:

S.N. Isolation & Description
1 TransactionDefinition.ISOLATION_DEFAULT
This is the default isolation level.
2 TransactionDefinition.ISOLATION_READ_COMMITTED
Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.
Indicates that dirty reads, non-repeatable reads and phantom reads can occur.
Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.
5 TransactionDefinition.ISOLATION_SERIALIZABLE
Indicates that dirty reads, non-repeatable reads and phantom reads are prevented.

Following are the possible values for propagation types:

S.N. Propagation & Description
1 TransactionDefinition.PROPAGATION_MANDATORY
Support a current transaction; throw an exception if no current transaction exists.
2 TransactionDefinition.PROPAGATION_NESTED 
Execute within a nested transaction if a current transaction exists.
3 TransactionDefinition.PROPAGATION_NEVER 
Do not support a current transaction; throw an exception if a current transaction exists.
4 TransactionDefinition.PROPAGATION_NOT_SUPPORTED 
Do not support a current transaction; rather always execute non-transactionally.
5 TransactionDefinition.PROPAGATION_REQUIRED 
Support a current transaction; create a new one if none exists.
6 TransactionDefinition.PROPAGATION_REQUIRES_NEW 
Create a new transaction, suspending the current transaction if one exists.
7 TransactionDefinition.PROPAGATION_SUPPORTS 
Support a current transaction; execute non-transactionally if none exists.
8 TransactionDefinition.TIMEOUT_DEFAULT 
Use the default timeout of the underlying transaction system, or none if timeouts are not supported.

The TransactionStatus interface provides a simple way for transactional code to control transaction execution and query transaction status.

public interface TransactionStatus extends SavepointManager {
   boolean isNewTransaction();
   boolean hasSavepoint();
   void setRollbackOnly();
   boolean isRollbackOnly();
   boolean isCompleted();
S.N. Method & Description
1 boolean hasSavepoint()
This method returns whether this transaction internally carries a savepoint, that is, has been created as nested transaction based on a savepoint.
2 boolean isCompleted()
This method returns whether this transaction is completed, that is, whether it has already been committed or rolled back.
3 boolean isNewTransaction()
This method returns true in case the present transaction is new.
4 boolean isRollbackOnly()
This method returns whether the transaction has been marked as rollback-only.
5 void setRollbackOnly() 
This method sets the transaction rollback-only.