刘耀文

刘耀文

java开发者
github

Propagation behavior of Spring transactions

Transaction Propagation#

Several Scenarios of Transaction Propagation#

NumberMechanismDescriptionRemarks
1requiredIf a transaction exists, join it. If no transaction exists, create a new one. This is the most commonly used setting.Only create one transaction.
2requires_newAlways create a new transaction, regardless of whether a transaction exists or not. The old transaction is suspended first, then a new transaction is created. After the new transaction is executed and committed, the old transaction is resumed and finally committed.1. Create a new transaction every time. 2. Suspend the old transaction before creating the new transaction. 3. Commit the later executed method first, and commit the earlier executed method later. 4. The rollback of the old transaction does not affect the commit of the new transaction.
3nestedIf a transaction exists, the nested transaction uses the same transaction as the outer transaction, but with a new savepoint. There are two execution scenarios: (1) If a rollback occurs in the nested transaction, it does not affect the normal commit of the outer transaction. (2) If a rollback occurs in the outer transaction, the nested transaction will also be rolled back. (3) If no exception occurs, the nested transaction and the outer transaction are committed together as a whole. If no transaction exists, perform operations similar to required.The skin is still there, but the hair is burned. If the skin is gone, where will the hair attach to?
4supportsSupport the current transaction. If a transaction exists, join it. If no transaction exists, execute it non-transactionally.Supports does not create a transaction.
5not_supportedDo not support transactions. If a transaction exists, suspend the current transaction. If no transaction exists, execute it non-transactionally.
6mandatoryForce, must use a transaction. If a transaction already exists, join it. If no transaction exists, throw an exception.1. Mandatory does not create a transaction. 2. The prerequisite for the execution of mandatory is that a transaction already exists.
7neverProhibit transactions. If a transaction exists, throw an exception. If no transaction exists, execute it non-transactionally.Must be executed in a non-transactional context, otherwise an error will occur.

Understanding Several Scenarios of Transaction Propagation#

BehaviorType
Join if exists, ignore if notsupports
Always a new transaction, regardless of whether there is an outer transaction or notrequires_new
Join if exists, create a new one if notrequired
Join if exists, add savepoint, create a new one if notnested
No transaction, suspend if there is an outer transactionnot_supported
Requires an outer transaction (throws an exception if not satisfied)mandatory
Requires no outer transaction (throws an exception if not satisfied)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.