Sobes.tech
Junior — Middle

Can you list and explain different propagation strategies in Spring transactions?

sobes.tech AI

Answer from AI

In Spring, there are several propagation strategies for managing transactions:

  • REQUIRED (default): if a transaction exists, the method runs within it; otherwise, a new one is created.
  • REQUIRES_NEW: always creates a new transaction, suspending the existing one.
  • SUPPORTS: if a transaction exists, the method runs within it; otherwise, it runs without a transaction.
  • NOT_SUPPORTED: the method runs outside of a transaction, suspending any existing one.
  • MANDATORY: the method must run within an existing transaction; otherwise, an exception is thrown.
  • NEVER: the method must not run within a transaction; otherwise, an exception is thrown.
  • NESTED: if a transaction exists, a nested transaction (savepoint) is created; otherwise, it behaves like REQUIRED.

Example usage:

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveData() {
    // code executed within a new transaction
}
Can you list and explain different propagation… - sobes.tech