Junior — Middle
Can you explain what is meant by propagation levels in transaction management?
sobes.tech AI
Answer from AI
Propagation levels in transaction management determine how a transaction behaves when calling methods that may themselves be transactional. This is important to understand whether a method will use an existing transaction, create a new one, or work without a transaction.
Main propagation levels in Spring (Java):
- REQUIRED (default): if there is an existing transaction, the method joins it; otherwise, a new one is created.
- REQUIRES_NEW: always creates a new transaction, suspending the current one if it exists.
- SUPPORTS: if a transaction exists, the method works within it; otherwise, it works without a transaction.
- NOT_SUPPORTED: the method always executes without a transaction, suspending the current one if it exists.
- MANDATORY: the method must run within an existing transaction; if none exists, an exception is thrown.
- NEVER: the method must not run within a transaction; if it does, an exception is thrown.
- NESTED: if a transaction exists, a nested (savepoint) transaction is created; otherwise, a new one is created.
Example:
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveData() {
// this method always runs in a new transaction
}