Spring Framework transaction abstraction by R4R Team

Spring Framework transaction abstraction:-

The key to the Spring transaction abstraction is defined by the org.springframework.transaction.PlatformTransactionManager interface.which shows the following code:
 

public interface PlatformTransactionManager {

TransactionStatus getTransaction(

TransactionDefinition definition) throws TransactionException;

void commit(TransactionStatus status) throws TransactionException;

void rollback(TransactionStatus status) throws TransactionException;

}

This is primarily a service provider interface (SPI), although it can be used programmatically from your application code. Because PlatformTransactionManager is an interface, it can be easily mocked or stubbed as necessary.

The getTransaction(..) method returns a TransactionStatus object, depending on a TransactionDefinition parameter. The returned TransactionStatus might represent a new transaction, or can represent an existing transaction if a matching transaction exists in the current call stack.

The TransactionDefinition interface specifies:

Isolation: The degree to which this transaction is isolated from the work of other transactions.

Propagation: Typically, all code executed within a transaction scope will run in that transaction. However, you have the option of specifying the behavior in the event that a transactional method is executed when a transaction context already exists.

Timeout: How long this transaction runs before timing out and being rolled back automatically by the underlying transaction infrastructure.

Read-only status: A read-only transaction can be used when your code reads but does not modify data. Read-only transactions can be a useful optimization in some cases, such as when you are using Hibernate.
 

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();

void flush();

boolean isCompleted();

}

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.

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!