刘耀文

刘耀文

java开发者
github

Propagation behavior of Spring transactions

Transaction Propagation#

Several Cases of Transaction Propagation#

NoTransaction Propagation MechanismDescriptionRemarks
1requiredIf there is a current transaction, join that transaction. If there is no current transaction, create a new transaction. This is the most commonly used setting.Only creates one transaction.
2requires_newRegardless of whether a transaction exists, create a new, independent transaction. The old transaction is suspended, then the new transaction is created, executed, and committed, after which the old transaction continues and is finally committed.1. A new transaction is created each time. 2. The old transaction is suspended before creating the new transaction. 3. The method executed first commits the transaction, while the method executed later commits the transaction first. 4. The rollback of the old transaction does not affect the commit of the new transaction.
3nestedIf there is a current transaction, the nested transaction uses the same transaction as the outer transaction, but a new savepoint (savepoint) is opened inside the nested transaction. There are two execution scenarios: (1) If a rollback occurs inside the nested transaction, it does not affect the normal commit of the outer transaction. (2) If the outer transaction rolls back, the nested transaction must roll back. (3) If no exceptions occur, the nested transaction and the outer transaction are committed together as a whole. If there is no current transaction, it performs similarly to required.The skin remains, but the fur is burned. Without the skin, where will the fur attach?
4supportsSupports the current transaction. If there is a current transaction, join that transaction; if there is no current transaction, execute non-transactionally.supports does not create a transaction.
5not_supportedDoes not support transactions. If there is a current transaction, suspend the current transaction. If there is no current transaction, execute non-transactionally.
6mandatoryMandatory, must use a transaction. If a transaction already exists, join that transaction; if there is no current transaction, throw an exception.1. mandatory does not create a transaction. 2. The prerequisite for mandatory execution is that a transaction already exists.
7neverProhibits transactions. If there is a current transaction, throw an exception; if there is no current transaction, execute non-transactionally.Must be executed in a non-transactional context, otherwise an error will occur.

Understanding Several Cases of Transaction Propagation#

BehaviorType
Join if exists, ignore if notsupports
Always a new transaction regardless of outerrequires_new
Join if exists, new transaction if notrequired
Join if exists, savepoint if added, new transaction if notnested
No transaction, suspend if outer existsnot_supported
Requires an outer transaction (exception if not met)mandatory
Requires no outer transaction (exception if not met)mandatory

Thread Binding (Synchronization Management)#

This article is synchronized and updated to xLog by Mix Space. The original link is https://me.liuyaowen.club/posts/default/Spring

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.