2. Spring Boot : Transition and it's propagation | Interview Questions

preview_player
Показать описание
In Spring Boot, the @Transactional annotation is used to indicate that a method should be executed within a transaction. When a method annotated with @Transactional is invoked, Spring will automatically start and manage a transaction for the duration of the method execution. If an exception occurs within the method, Spring will roll back the transaction to its initial state, undoing any changes made during the method execution.

Here are the commonly used propagation behaviors in Spring:

REQUIRED:

This is the default propagation behavior.

It specifies that a new transaction should be started if no transaction exists.

If a transaction already exists, the method will join the existing transaction.

REQUIRES_NEW:

This propagation behavior always starts a new, independent transaction.

It suspends the existing transaction (if any) for the duration of the method execution and starts a new transaction.

If an existing transaction is present, it will be resumed after the method execution.

SUPPORTS:

This propagation behavior allows a method to execute within a transaction if one exists.

If no transaction exists, the method will execute without a transaction.

MANDATORY:

This propagation behavior requires an existing transaction to be present.

If no transaction exists, it will throw an exception.

NESTED:

This propagation behavior starts a nested transaction if a new one is required.

It runs within the scope of an existing transaction, creating a savepoint.

If no transaction exists, it behaves the same as REQUIRED.

NEVER:

This propagation behavior specifies that the method should not run within a transaction context.

If a transaction exists, an exception is thrown.

NOT_SUPPORTED:

This propagation behavior specifies that the method should not run within a transaction context.

If a transaction exists, it will be suspended for the duration of the method execution.

UNKNOWN:

This propagation behavior is a sentinel value indicating that the caller's propagation setting should be used.
Рекомендации по теме