This article is generated by AI translation.
Transaction Propagation
When multiple transactional methods call each other in the same thread, propagation behavior determines how transactions propagate between these methods.
Join Existing Transaction (REQUIRED)
Try to join an existing transaction; if none exists, start a new one.
- Constant:
Propagation.REQUIRED
| Time | Transaction A | Transaction B | Effect |
|---|---|---|---|
| T1 | begin | Start Transaction A | |
| T2 | insert data1 | ||
| T3 | begin | Join Transaction A (no-op) | |
| T4 | insert data2 | ||
| T5 | commit/rollback | No-op (decided by outer) | |
| T6 | insert data3 | ||
| T7 | commit/rollback | Commit/rollback Transaction A |
Independent Transaction (REQUIRES_NEW)
Suspend the current transaction (if any) and start a brand new independent transaction. The new transaction is unrelated to the old one.
- Constant:
Propagation.REQUIRES_NEW
- Suspension means the Connection bound to the current thread becomes temporarily unavailable.
- After suspension, the transaction manager creates a new Connection for the current thread's database connection.
| Time | Transaction A | Transaction B | Effect |
|---|---|---|---|
| T1 | begin | Start Transaction A | |
| T2 | insert data1 | ||
| T3 | begin | Suspend A → new Connection B → start Transaction B | |
| T4 | insert data2 | ||
| T5 | commit/rollback | Commit/rollback B → resume A | |
| T6 | insert data3 | ||
| T7 | commit/rollback | Commit/rollback Transaction A |
Nested Transaction (NESTED)
Create a subtransaction inside the current one using a Savepoint. Rolling back the subtransaction does not affect the outer transaction, but rolling back the outer transaction also rolls back the subtransaction.
- Constant:
Propagation.NESTED
| Time | Transaction A | Transaction B | Effect |
|---|---|---|---|
| T1 | begin | Start Transaction A | |
| T2 | insert data1 | ||
| T3 | begin | Create Savepoint B | |
| T4 | insert data2 | ||
| T5 | commit/rollback | Release/rollback Savepoint B | |
| T6 | insert data3 | ||
| T7 | commit/rollback | Commit/rollback Transaction A |
Follow Current Context (SUPPORTS)
If no transaction exists, run non-transactionally; if one exists, join the current transaction (same effect as REQUIRED).
- Constant:
Propagation.SUPPORTS
SUPPORTS essentially means do nothing — it will not actively start nor prevent a transaction.
Non-Transactional (NOT_SUPPORTED)
If no transaction exists, run non-transactionally; if one exists, suspend the current transaction and run non-transactionally.
- Constant:
Propagation.NOT_SUPPORTED
| Time | Transaction A | Transaction B | Effect |
|---|---|---|---|
| T1 | begin | Start Transaction A | |
| T2 | insert data1 | ||
| T3 | begin | Suspend Transaction A | |
| T4 | insert data2 | Run non-transactionally | |
| T5 | commit/rollback | Resume Transaction A | |
| T6 | insert data3 | ||
| T7 | commit/rollback | Commit/rollback Transaction A |
Exclude Transaction (NEVER)
If no transaction exists, run non-transactionally; if one exists, throw an exception.
- Constant:
Propagation.NEVER
Require Transaction (MANDATORY)
If a transaction exists, join it; if none exists, throw an exception.
- Constant:
Propagation.MANDATORY
Propagation Behavior Comparison
| Propagation | No Transaction | Has Transaction |
|---|---|---|
REQUIRED | Start new | Join existing |
REQUIRES_NEW | Start new | Suspend existing → start new |
NESTED | Start new | Savepoint subtransaction |
SUPPORTS | Run non-transactionally | Join existing |
NOT_SUPPORTED | Run non-transactionally | Suspend existing → run non-transactionally |
NEVER | Run non-transactionally | Throw exception |
MANDATORY | Throw exception | Join existing |