Transaction Propagation Mechanism
PROPAGATION_REQUIRED
This is the default value of the annotation:
- If there is no physical transaction currently, Spring will create one
- If a physical transaction already exists, it will join that transaction
- All logical transactions map to the same physical transaction
PROPAGATION_REQUIRES_NEW
Spring will always create a new independent physical transaction regardless of whether a current transaction exists:
- Internal transaction starts a brand new database connection
- If an external transaction exists, it will be suspended first
- Internal transaction’s commit or rollback does not affect external transaction
- Typical applications: Logging, auditing and other non-core businesses
PROPAGATION_NESTED
Similar to PROPAGATION_REQUIRED, but with more fine-grained rollback control:
- Creates nested transaction, sets SAVEPOINT
- Nested transaction rollback does not affect external transaction, only rolls back to savepoint
- Typical applications: Batch processing where single record failure does not affect entire batch
PROPAGATION_MANDATORY
Current method must run within an existing physical transaction:
- If no active transaction exists, throws IllegalTransactionStateException
- Typical applications: Core businesses like fund transfers, inventory deduction
PROPAGATION_NEVER
Method must execute in a non-transactional environment:
- If a transaction context exists, throws IllegalTransactionStateException
- Typical applications: Read operations that don’t need transactions
PROPAGATION_SUPPORTS
Method can run in a transaction or not:
- When a physical transaction exists, joins existing transaction
- When no physical transaction exists, executes in a non-transactional manner
- Typical applications: Query operations
PROPAGATION_NOT_SUPPORTED
Current method should not run in a transaction context:
- If an active transaction exists, suspends it
- Resumes suspended transaction after method execution completes
- Typical applications: Non-core businesses like logging, sending notifications