Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Spring transaction management


May 14, 2021 Spring


Table of contents


Transaction management

A database transaction is a sequence of operations that is considered a single unit of work. T hese operations should either be performed in its entirety or not at all. T ransaction management is an important component of RDBMS for enterprise applications to ensure data integrity and consistency. The concept of a transaction can be described as having four key attributes that can be described as ACID:

  • Atomicity: A transaction should be treated as a single unit of operation, which means that the entire sequence operation either succeeds or fails.

  • Consistency: This represents the consistency of the reference integrity of the database, the unique primary key in the table, and so on.

  • Isolation: Many transactions with the same data set may be processed at the same time, and each transaction should be isolated from other transactions to prevent data corruption.

  • Persistence: Once a transaction has completed all operations, the result of the transaction must be permanent and cannot be removed from the database due to a system failure.

A true RDBMS database system guarantees all four properties for each transaction. A simple view of transactions published to the database using SQL is as follows:

  • Start the transaction with the begin transaction command.

  • Use SQL query statements to perform various deletions, updates, or inserts.

  • If all operations succeed, the commit operation is performed, otherwise all operations are rolled back.

The Spring framework provides an abstraction layer at the top of different underlying transaction management APIs. S pring's transaction support is designed to provide an option for EJB transactions by adding transaction capabilities to POJOs. S pring supports programmatic and declartive transaction management. EJBs require an application server, but Spring transaction management can be implemented without the need for an application server.

Local transactions vs. global transactions

A local transaction is a single transaction resource, such as a JDBC connection, while a global transaction can span multiple transaction resource transactions, such as a transaction in a distributed system.

Local transaction management is useful in a centralized computing environment where application components and resources are located at one unit point, while transaction management involves only one local data manager running in a single machine. Local transactions are easier to implement.

Global transaction management requires a distributed computing environment in which all resources are distributed across multiple systems. I n this case, transaction management needs to be both local and global. Distributed or global transactions are executed across multiple systems and require coordination between the global transaction management system and local data managers of all related systems.

Programmatic vs. declared

Spring supports two types of transaction management:

Declarive transaction management is preferable to programmatic transaction management, and although it is not as flexible as programmatic transaction management, it allows you to control transactions through code. H owever, as a cross-cutting concern, declarate transaction management can be modularized using the AOP approach. Spring supports statement transaction management using the Spring AOP framework.

Spring transaction abstraction

Spring transaction management has five key attributes: isolation level, propagation behavior, read-only, transaction timeout, and rollback rules

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


public interface PlatformTransactionManager {
   TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;
   void commit(TransactionStatus status) throws TransactionException;
   void rollback(TransactionStatus status) throws TransactionException;
}
Serial number Methods and descriptions
1

TransactionStatus getTransaction(TransactionDefinition definition)

Depending on the specified propagation behavior, the method returns the current active transaction or creates a new transaction.

2

void commit(TransactionStatus status)

The method is submitted to the determined transaction and about its state.

3

void rollback(TransactionStatus status)

The method performs a rollback of a given transaction.

TransactionDefinition is the core interface supported by transactions in Spring and is defined as follows:


public interface TransactionDefinition {
   int getPropagationBehavior();
   int getIsolationLevel();
   String getName();
   int getTimeout();
   boolean isReadOnly();
}
Serial number Methods and descriptions
1

int getPropagationBehavior()

The method returns the propagation behavior. Spring provides all transaction propagation options similar to EJB CMT.

2

int getIsolationLevel()

The method returns the degree to which the transaction is independent of the work of other transactions.

3

String getName()

The method returns the name of the transaction.

4

int getTimeout()

The method returns an interval in seconds during which the transaction must complete.

5

boolean isReadOnly()

The method returns whether the transaction is read-only.

Here are the possible values for the isolation level:

Serial number Isolation and description
1

TransactionDefinition.ISOLATION_DEFAULT

This is the default isolation level.

2

TransactionDefinition.ISOLATION_READ_COMMITTED

Indicates that misreading can be prevented;

3

TransactionDefinition.ISOLATION_READ_UNCOMMITTED

Indicates that misreading, non-repeatable, and false reading can occur.

4

TransactionDefinition.ISOLATION_REPEATABLE_READ

Indicates that misreading and non-repeatable reading can be prevented;

5

TransactionDefinition.ISOLATION_SERIALIZABLE

Indicates that misreading, non-repeatable, and false reading can be prevented.

Here are the possible values for the propagation type:

Serial number Propagation and description
1

TransactionDefinition.PROPAGATION_MANDATORY

The current transaction is supported, and if the current transaction does not exist, an exception is thrown.

2

TransactionDefinition.PROPAGATION_NESTED

If the current transaction exists, it is executed in a nested transaction.

3

TransactionDefinition.PROPAGATION_NEVER

The current transaction is not supported;

4

TransactionDefinition.PROPAGATION_NOT_SUPPORTED

The current transaction is not supported;

5

TransactionDefinition.PROPAGATION_REQUIRED

Supports the current transaction, and if there is no transaction, a new transaction is created.

6

TransactionDefinition.PROPAGATION_REQUIRES_NEW

Create a new transaction and suspend the current transaction if there is one.

7

TransactionDefinition.PROPAGATION_SUPPORTS

The current transaction is supported, and if it does not exist, non-transactional is performed.

8

TransactionDefinition.TIMEOUT_DEFAULT

Use the underlying transaction system for the default timeout, or if you do not support the timeout.

The TransactionStatus interface provides transaction code with an easy way to control the execution of transactions and query transaction status.


public interface TransactionStatus extends SavepointManager {
   boolean isNewTransaction();
   boolean hasSavepoint();
   void setRollbackOnly();
   boolean isRollbackOnly();
   boolean isCompleted();
}
Serial number Methods and descriptions
1

boolean hasSavepoint()

The method returns whether there is a save point inside the transaction, that is, a nested transaction has been created based on a save point.

2

boolean isCompleted()

The method returns whether the transaction is complete, that is, whether it has been committed or rolled back.

3

boolean isNewTransaction()

In the case of a new transaction, the method returns true.

4

boolean isRollbackOnly()

The method returns whether the transaction has been marked as rollback-only.

5

void setRollbackOnly()

The method sets the transaction to a rollback-only tag.