Spring's transaction management is based on AOP, and AOP is based on methods. Spring's transaction attributes are propagation behavior, isolation level, read-only and timeout attributes. These attributes provide methods and description strategies for transaction applications.
In J stratified mode ava EE developers often used in, Spring's transaction processing business logic layer is located, it provides a solution for the transaction.
Spring transaction management has the following three core interfaces
1. PlatformTransactionManager
The PlatformTransactionManager interface is a platform transaction manager provided by Spring for managing transactions. Three transaction operation methods are provided in this interface, as follows.
- TransactionStatus getTransaction (TransactionDefinition definition): used to obtain transaction status information.
- void commit (TransactionStatus status): Used to commit transactions.
- void rollback (TransactionStatus status): used to roll back the transaction.
In the project, Spring encapsulates the transaction details configured in xml into the object TransactionDefinition, and then obtains the status of the transaction (TransactionStatus) through the getTransaction () method of the transaction manager, and performs the next operation on the transaction.
2. TransactionDefinition
The TransactionDefinition interface is the object of transaction definition (description), which provides methods for obtaining transaction-related information, including five operations, as follows.
- String getName (): Get the transaction object name.
- int getIsolationLevel (): Get the isolation level of the transaction.
- int getPropagationBehavior (): Get the propagation behavior of the transaction.
- int getTimeout (): Get the timeout time of the transaction.
- boolean isReadOnly (): Get whether the transaction is read-only.
In the description of the above five methods, the transaction propagation behavior refers to the transactions used before and after different operations in the same method. The types of communication behavior are shown in Table 1.
Attribute name | value | Description |
---|---|---|
PROPAGATION_REQUIRED | required | Support the current transaction. If the A method is already in the transaction, the B transaction will be used directly. Otherwise, a new transaction will be created |
PROPAGATION_SUPPORTS | supports | Support the current transaction. If the A method is already in the transaction, the B transaction will be used directly. Otherwise it will be executed in a non-transactional state |
PROPAGATION_MANDATORY | mandatory | Support the current transaction. If method A has no transaction, an exception is thrown |
PROPAGATION_REQUIRES_NEW | requires_new | A new transaction will be created, if the A method is already in the transaction, the A transaction will be suspended |
PROPAGATION_NOT_SUPPORTED | not_supported | Does not support the current transaction, always executed in a non-transactional state. If method A is already in a transaction, suspend it |
PROPAGATION_NEVER | never | Does not support the current transaction, if the A method is in the transaction, an exception is thrown |
PROPAGATION.NESTED | nested | Nested transactions, the bottom layer will use Savepoint to form nested transactions |
In the transaction management process, the propagation behavior can control whether and how transactions need to be created.
Normally, the data query will not change the original data, so no transaction management is required, and transaction management must be performed for data addition, modification, and deletion. If the propagation behavior of the transaction is not specified, the default propagation behavior of Spring 3 is required.
3. TransactionStatus
The TransactionStatus interface is the status of a transaction, which describes the status information of a transaction at a certain point in time. It contains six operations, as shown in Table 2.
name | Explanation |
---|---|
void flush() | Refresh transaction |
boolean hasSavepoint() | Get if savepoint exists |
boolean isCompleted() | Get transaction completed |
boolean isNewTransaction() | Get new transaction |
boolean isRollbackOnly() | Get whether to roll back |
void setRollbackOnly() | Set transaction rollback |