Transaction Propagation#
Several Cases of Transaction Propagation#
No | Transaction Propagation Mechanism | Description | Remarks |
---|---|---|---|
1 | required | If 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. |
2 | requires_new | Regardless 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. |
3 | nested | If 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? |
4 | supports | Supports 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. |
5 | not_supported | Does not support transactions. If there is a current transaction, suspend the current transaction. If there is no current transaction, execute non-transactionally. | |
6 | mandatory | Mandatory, 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. |
7 | never | Prohibits 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#
Behavior | Type |
---|---|
Join if exists, ignore if not | supports |
Always a new transaction regardless of outer | requires_new |
Join if exists, new transaction if not | required |
Join if exists, savepoint if added, new transaction if not | nested |
No transaction, suspend if outer exists | not_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