InterviewVault
Welcome back, Sujit Kumar Mishra
Admin
SK Mishra
Revision Mode
Document technical questions and best-practice answers.
Transaction Management in Spring Boot
Question:
Do you have experience with transaction management in Spring Boot?
Please explain:
1: What is transaction propagation?
2: What is transaction isolation?
3: What are the different propagation and isolation levels?
How are they used with the @Transactional annotation?
Transaction Management in Spring Boot
1: What is transaction propagation?
Propagation defines how transactions behave when one transactional method calls another. It decides if the called method should run in the same transaction or start a new one.
2: What is transaction isolation?
Isolation controls how one transaction sees data changes made by other transactions. It prevents problems like dirty reads, non-repeatable reads, and phantom reads.
3: Different propagation levels
REQUIRED: Uses current transaction or creates a new one if none exists (default).
REQUIRES_NEW: Always starts a new transaction, suspending the current one.
SUPPORTS: Uses current transaction if exists, else runs non-transactional.
NOT_SUPPORTED: Runs non-transactional, suspends any existing transaction.
MANDATORY: Must run within an existing transaction; throws error if none.
NEVER: Must NOT run within a transaction; throws error if one exists.
NESTED: Runs within a nested transaction if current exists.
4: Different isolation levels
DEFAULT: Uses database default.
READ_UNCOMMITTED: Can see uncommitted changes (least safe).
READ_COMMITTED: Only sees committed changes.
REPEATABLE_READ: Same data for repeated reads in a transaction.
SERIALIZABLE: Highest isolation; transactions run one after another.
5: Using @Transactional annotation
You can set propagation and isolation like this:
@Transactional(
propagation = Propagation.REQUIRED,
isolation = Isolation.READ_COMMITTED
)
public void yourMethod() {
// business logic
}
Summary:
1: Propagation: Controls transaction flow between methods.
2: Isolation: Controls data visibility between transactions.
3: @Transactional: Lets you set both using simple attributes.