InterviewVault
Welcome back, Sujit Kumar Mishra
Admin
SK Mishra
Revision Mode
Document technical questions and best-practice answers.
What are the different Transaction Propagation Levels in Spring?
Transaction Propagation Levels in Spring (Simple Explanation):
1: REQUIRED – Join the current transaction if one exists; otherwise, start a new one.
(Most common, default option.)
2: REQUIRES_NEW – Always start a new transaction, suspending any existing one.
3: SUPPORTS – Join the current transaction if one exists; if not, run without a transaction.
4: NOT_SUPPORTED – Run without a transaction, suspending any existing one.
5: MANDATORY – Must run inside an existing transaction; throws an error if none exists.
6: NEVER – Must run without a transaction; throws an error if a transaction exists.
7: NESTED – Runs within a nested transaction if a current transaction exists; otherwise, acts like REQUIRED.
Easy way to remember:
- REQUIRED and SUPPORTS join existing transactions.
- REQUIRES_NEW and NESTED always create a new or nested transaction.
- MANDATORY and NEVER enforce presence or absence of a transaction.
- NOT_SUPPORTED ignores transactions completely.
Example:
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@Transactional(propagation = Propagation.REQUIRED)
// Change propagation type as needed
public void doSomething() {
// Business logic here
}
}