Sobes.tech
Junior — Middle

Can you tell about different types of Propagation that can be set in the @Transactional annotation?

sobes.tech AI

Answer from AI

In the @Transactional annotation in Spring, different Propagation types can be set, which determine the behavior of transactions:

  • REQUIRED (default): if a transaction exists, use it; otherwise, create a new one.
  • REQUIRES_NEW: always create a new transaction, suspending the existing one if present.
  • SUPPORTS: if a transaction exists, use it; otherwise, execute non-transactionally.
  • NOT_SUPPORTED: execute non-transactionally, suspending any existing transaction.
  • MANDATORY: must have an existing transaction; otherwise, an exception is thrown.
  • NEVER: execute non-transactionally; if a transaction exists, an exception is thrown.
  • NESTED: if a transaction exists, create a nested transaction (savepoint); otherwise, create a new one.

Example:

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void someMethod() {
    // code
}
Can you tell about different types of Propagation… - sobes.tech