Sunday 22 September 2013

Transaction Management In Spring Part-2

Spring Transaction Managers





Platform-specific Transaction Implementations

Methods available in the org.springframework.transaction.PlatformTransactionManager interface are shown below:

  • PlatformTransactionManager


public interface PlatformTransactionManager {

    TransactionStatus getTransaction(TransactionDefinition definition)
        throws TransactionException;
                                Return a currently active transaction or create a new one, according to the specified propagation behavior.
     
    void commit(TransactionStatus status) throws TransactionException;
            Commit the given transaction, with regard to its status.
    
    void rollback(TransactionStatus status) throws TransactionException;
           Perform a rollback of the given transaction.
}

  • TransactionStatus Interface


A TransactionStatus is associated with a thread of execution. The TransactionStatus interface provides a normal and simple way for transactional code to control transaction execution and query transaction status. They are common to all transaction APIs:

public interface TransactionStatus {

    boolean isNewTransaction();
                  Return whether the present transaction is new (else participating in an existing transaction, or potentially not running in an actual transaction in the first place).

   
    boolean hasSavepoint();
                  Return whether this transaction internally carries a savepoint, that is, has been created as nested transaction based on a savepoint. 

    void setRollbackOnly();
            Set the transaction rollback-only.
    
    boolean isRollbackOnly();
               Return whether the transaction has been marked as rollback-only (either by the application or by the transaction infrastructure).

    
    boolean isCompleted();
                 Return whether this transaction is completed, that is, whether it has already been committed or rolled back. 
}

  • TransactionDefinition interface


The TransactionDefinition interface specifies:
  •          Transaction isolation: It is applied to transactions in general and is directly related with the ACID transaction properties. For example, can this transaction see uncommitted writes from other transactions?
  •          Transaction propagation: All code executed within a transaction scope will run in that transaction and there are several options specifying propagation behavior if a transactional method is executed when a transaction context already exists: For example, simply running in the existing transaction or suspending the existing transaction and creating a new transaction.
  •          Transaction timeout: How long this transaction may run before timing out (automatically being rolled back by the underlying transaction infrastructure).
  •          Read-only status: A read-only transaction does not modify any data.


public interface TransactionDefinition{

int getIsolationLevel();
                    Return the isolation level.

String getName();
                    Return the name of this transaction. Can be null.

int getTimeout();
                    Return the transaction timeout.

boolean isReadOnly();
                    Return whether to optimize as a read-only transaction.
 

Transaction State

There are few different possible Transaction states and they are:

Active – The initial state of transaction and transaction stays in this state while executing.

Failed When the failed state is determined then the normal execution can no longer proceed.

Aborted – The state of the transaction when it has been roll-backed and the database taken back to its initial state.

Partially Committed – The state of the transaction after the final statement has been executed.

Committed – The state of the transaction after successful completion.




Different Transaction States

No comments:

Post a Comment