Sobes.tech
Junior — Middle

Kas saaksite selgitada, mida mõistetakse levitamise tasemetena all tehingute haldamisel?

sobes.tech AI

Vastus AI-lt

The propagation levels in transaction management determine how a transaction behaves when calling methods that can also be transactional. This is important to understand whether a method will use an existing transaction, create a new one, or operate without a transaction.

The 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 any).
  • SUPPORTS: if there is a transaction, the method works within it; otherwise, without a transaction.
  • NOT_SUPPORTED: the method always executes without a transaction, suspending the current one.
  • MANDATORY: the method must be executed within an existing transaction; otherwise, an exception is thrown.
  • NEVER: the method must not be executed within a transaction; otherwise, an exception is thrown.
  • NESTED: if there is a transaction, a nested (savepoint) transaction is created; otherwise, a new one is.

Example:

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveData() {
    // this method is always in a new transaction
}