What is the propagation and isolation level of a Spring transaction?
Learn Spring transaction propagation (REQUIRED, REQUIRES_NEW, NESTED) and isolation levels, and how each prevents dirty, non-repeatable, and phantom reads.
Expected Interview Answer
Propagation controls how a transactional method behaves when a transaction already exists (whether it joins, suspends, or creates a new one), while isolation controls how visible one transaction's uncommitted changes are to concurrent transactions.
Propagation options like REQUIRED (default, join or create), REQUIRES_NEW (always start a new, suspending any existing), NESTED (savepoint within the outer), and SUPPORTS or MANDATORY govern transaction boundaries. Isolation levels, READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, and SERIALIZABLE, trade concurrency for protection against dirty reads, non-repeatable reads, and phantom reads. Higher isolation reduces anomalies but increases locking and contention.
- Fine-grained control over transaction boundaries
- REQUIRES_NEW isolates independent units like audit logs
- Isolation levels prevent specific read anomalies
- Tune concurrency versus consistency per use case
- NESTED enables partial rollback via savepoints
AI Mentor Explanation
Propagation is like how a new batter joins an innings: REQUIRED means they continue the same innings already in progress, while REQUIRES_NEW is like starting a completely separate practice match that pauses the main one. Isolation is the sight-screen: at low isolation batters glimpse deliveries meant for another net and get confused, at high isolation each net is fully screened so no one sees another's balls.
Step-by-Step Explanation
Step 1
Choose a propagation
REQUIRED joins or creates; REQUIRES_NEW suspends and starts fresh; NESTED uses a savepoint; MANDATORY requires an existing one.
Step 2
Understand REQUIRED default
Most methods use REQUIRED so nested service calls share one transaction and commit or roll back together.
Step 3
Use REQUIRES_NEW for independence
For audit logs or notifications that must persist regardless of the caller's outcome.
Step 4
Pick an isolation level
Higher levels (REPEATABLE_READ, SERIALIZABLE) block more anomalies at the cost of concurrency.
Step 5
Know the anomalies
Map dirty read, non-repeatable read, and phantom read to the level that prevents each.
What Interviewer Expects
- Clear distinction between propagation and isolation
- Knowing REQUIRED is the default propagation
- REQUIRES_NEW suspends the existing transaction
- Naming the four standard isolation levels
- Mapping anomalies (dirty, non-repeatable, phantom) to levels
Common Mistakes
- Confusing propagation with isolation
- Thinking REQUIRES_NEW just continues the same transaction
- Assuming SERIALIZABLE is free of performance cost
- Believing isolation is set per method without DB support
- Forgetting NESTED depends on savepoint support
Best Answer (HR Friendly)
“Propagation decides what happens when one transactional method calls another, whether they share one save-or-undo unit or run as separate ones. Isolation decides how much unfinished work from one operation another can see, trading speed for safety. Together they control how Spring coordinates concurrent database work.”
Code Example
@Service
public class OrderService {
// Joins caller's transaction (default)
@Transactional(propagation = Propagation.REQUIRED,
isolation = Isolation.READ_COMMITTED)
public void placeOrder(Order order) {
saveOrder(order);
writeAuditLog(order);
}
// Always its own transaction, persists even if placeOrder rolls back
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void writeAuditLog(Order order) {
// audit insert
}
}Follow-up Questions
- What is the difference between REQUIRES_NEW and NESTED?
- Which isolation level prevents phantom reads?
- What is a dirty read and how is it avoided?
- Why can SERIALIZABLE hurt throughput?
- Does the database or Spring enforce the isolation level?
MCQ Practice
1. What is the default propagation in Spring?
REQUIRED joins an existing transaction or creates a new one if none exists, the default.
2. Which isolation level prevents dirty, non-repeatable, and phantom reads?
SERIALIZABLE is the strictest level and prevents all three anomalies at the highest concurrency cost.
3. What does Propagation.REQUIRES_NEW do when a transaction is active?
REQUIRES_NEW suspends the current transaction and runs in an independent new one.
Flash Cards
What does propagation control? — How a method behaves toward an existing transaction: join, suspend, or create a new one.
What does isolation control? — How visible one transaction's uncommitted changes are to concurrent transactions.
What does REQUIRES_NEW do? — Suspends any active transaction and runs in a brand-new independent one.
Which anomalies does REPEATABLE_READ still allow? — Phantom reads, only SERIALIZABLE fully prevents them.
Continue Learning
Related Interview Questions
How does transaction management work in Spring with @Transactional?
medium
What are Spring bean scopes and how do singleton and prototype differ?
medium
What is the Spring bean lifecycle and what callbacks does it provide?
medium
What is the difference between @Autowired field, constructor, and setter injection?
medium