Hint
This article is generated by AI translation.
Transaction Propagation
A simple way to think about propagation: when multiple transactional methods call each other in the same thread, how does the transaction flow between them?
Join existing transaction (REQUIRED)
- Idea: try to join an existing transaction; if none exists, start a new one.
- Constant: Propagation.REQUIRED
| Time | Transaction A | Transaction B | Effect |
|-------|-----------------|-----------------|-------------------|
| 00:01 | begin | | begin A |
| 00:02 | insert data1 | | |
| 00:03 | | begin | Do nothing |
| 00:04 | | insert data2 | |
| 00:05 | | commit/rollback | Do nothing |
| 00:06 | insert data3 | | |
| 00:07 | commit/rollback | | commit/rollback A |
New independent transaction (REQUIRES_NEW)
- Idea: suspend the current transaction (if any) and start a brand new one. The new transaction is independent of the suspended one.
- Constant: Propagation.REQUIRES_NEW
info
- Suspension means the Connection bound to the current thread is temporarily unavailable to the transaction manager.
- After suspension, the transaction manager creates a new Connection and binds it to the thread.
| Time | Transaction A | Transaction B | Effect |
|-------|-----------------|------------------|----------------------------------------|
| 00:01 | begin | | begin A |
| 00:02 | insert data1 | | |
| 00:03 | | begin | suspend A > new Connection B > begin B |
| 00:04 | | insert data2 | |
| 00:05 | | commit/rollback | commit/rollback B > resume A |
| 00:06 | insert data3 | | |
| 00:07 | commit/rollback | | commit/rollback A |
Nested transaction (NESTED)
- Idea: create a subtransaction inside the current one using a
Savepoint. - Constant: Propagation.NESTED
| Time | Transaction A | Transaction B | Effect |
|-------|-----------------|-----------------|-----------------------------|
| 00:01 | begin | | begin A |
| 00:02 | insert data1 | | |
| 00:03 | | begin | Savepoint B |
| 00:04 | | insert data2 | |
| 00:05 | | commit/rollback | commit/rollback Savepoint B |
| 00:06 | insert data3 | | |
| 00:07 | commit/rollback | | commit/rollback A |
Follow current context (SUPPORTS)
- Idea: if no transaction exists, run non-transactionally; if one exists, join it.
- Constant: Propagation.SUPPORTS
info
SUPPORTS is the simplest propagation setting; its essence is do nothing special.
| Time | Transaction A | Transaction B | Effect |
|-------|-----------------|-----------------|-------------|
| 00:01 | begin | | ... |
| 00:02 | insert data1 | | |
| 00:03 | | begin | Do nothing |
| 00:04 | | insert data2 | |
| 00:05 | | commit/rollback | Do nothing |
| 00:06 | insert data3 | | |
| 00:07 | commit/rollback | | ... |
- If Transaction A has already begun, the effect is the same as REQUIRED.
Non-transactional (NOT_SUPPORTED)
- Idea: if no transaction exists, run non-transactionally; if one exists, suspend it.
- Constant: Propagation.NOT_SUPPORTED
| Time | Transaction A | Transaction B | Effect |
|-------|-----------------|---------------------|-------------------------|
| 00:01 | begin | | ... |
| 00:02 | insert data1 | | |
| 00:03 | | begin | if A exist > suspend A |
| 00:04 | | insert data2 | |
| 00:05 | | commit/rollback | if A suspend > resume A |
| 00:06 | insert data3 | | |
| 00:07 | commit/rollback | | ... |
Explicitly no transaction (NEVER)
- Idea: if no transaction exists, run non-transactionally; if one exists, throw an exception.
- Constant: Propagation.NEVER
| Time | Transaction A | Transaction B | Effect |
|-------|-----------------|-----------------|------------------------------|
| 00:01 | begin | | ... |
| 00:02 | insert data1 | | |
| 00:03 | | begin | if A exist > throw Exception |
| 00:04 | | insert data2 | |
| 00:05 | | commit/rollback | Do nothing |
| 00:06 | insert data3 | | |
| 00:07 | commit/rollback | | ... |
Transaction required (MANDATORY)
- Idea: if no transaction exists, throw an exception; otherwise use the current transaction.
- Constant: Propagation.MANDATORY
| Time | Transaction A | Transaction B | Effect |
|-------|-----------------|-----------------|----------------------------------|
| 00:01 | begin | | ... |
| 00:02 | insert data1 | | |
| 00:03 | | begin | if A not exist > throw Exception |
| 00:04 | | insert data2 | |
| 00:05 | | commit/rollback | Do nothing |
| 00:06 | insert data3 | | |
| 00:07 | commit/rollback | | ... |