Transactions
22 interview questions and answers in the Transactions section.
Questions in this section
- Decode Each Letter of ACID
- What Transaction Isolation Levels Exist?
- What is Read Uncommitted?
- What is Read Committed?
- What is Repeatable Read?
- What is Serializable?
- What is Dirty Read?
- What is Non-Repeatable Read?
- What is Phantom Read?
- What is Lost Update?
- What Is the Default Isolation Level in PostgreSQL
- What is the default isolation level in MySQL
- What is Propagation in Spring
- What does Propagation.NESTED do
- What is the difference between REQUIRED and REQUIRES_NEW
- What is @Transactional annotation?
- At what level can you use @Transactional?
- What is rollback in transactions?
- Which exceptions cause rollback by default?
- How to configure rollback for checked exceptions?
- What is readonly transaction?
- What happens when calling @Transactional method from another method of the same class?
Study navigator
22 questions for Middle Java Developer interview preparation.
π All Questions
πΊοΈ Topic Dependency Map
ββββββββββββββββββββββββββββββββββββββββββββ
β TRANSACTION BASICS (1-2) β
β 1. ACID β
β 2. Isolation levels overview β
ββββββββββββββββββββ¬ββββββββββββββββββββββββ
β
ββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
β ISOLATION β β ANOMALIES β β DB-SPECIFIC β
β LEVELS (3-6) β β (7-10) β β (11-12) β
β 3. Read Uncom. β β 7. Dirty Read β β 11. PostgreSQL β
β 4. Read Comm. β β 8. Non-rep. β β 12. MySQL β
β 5. Repeat. R. β β 9. Phantom β β β
β 6. Serializableβ β 10. Lost Update β β β
βββββββββ¬ββββββββ βββββββββ¬ββββββββ ββββββββββ¬ββββββββββββ
β β β
ββββββββββββββββββββββββββΌβββββββββββββββββββββββββ
βΌ
ββββββββββββββββββββββββββββββββββββββββββββ
β SPRING TRANSACTION (13-17) β
β 13. Propagation overview β
β 14. Propagation.NESTED β
β 15. REQUIRED vs REQUIRES_NEW β
β 16. @Transactional β
β 17. @Transactional levels β
ββββββββββββββββββββββββββββββββββββββββββββ
β
ββββββββββββββββββββββββββΌβββββββββββββββββββββββββ
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
β ROLLBACK β β SPECIAL β β SELF-INVOCATION β
β (18-20) β β (21) β β (22) β
β 18. Rollback β β 21. ReadOnly β β 22. Self-invocationβ
β 19. Default β β β β proxy bypass β
β rollback β β β β β
β 20. Custom β β β β β
β rollback β β β β β
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
π― Recommended Study Order
π’ Junior Level (weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | ACID | Q1 | Atomicity, Consistency, Isolation, Durability |
| 2 | Isolation levels | Q2 | 4 levels, what they protect against |
| 3 | Anomalies | Q7, Q8, Q9, Q10 | Dirty Read, Non-repeatable, Phantom, Lost Update |
| 4 | @Transactional basics | Q16, Q18, Q19 | What it does, default rollback |
| 5 | PostgreSQL vs MySQL | Q11, Q12 | Default levels, differences |
π‘ Middle Level (weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Read Committed vs Repeatable Read | Q4, Q5 | MVCC vs Next-Key Lock, snapshot timing |
| 2 | Serializable | Q6 | SSI, dependency graph, serialization failure |
| 3 | Propagation | Q13, Q15 | REQUIRED, REQUIRES_NEW, rollback-only mark |
| 4 | NESTED | Q14 | Savepoint, JTA fallback, L1 cache trap |
| 5 | @Transactional levels | Q17 | Class vs Method vs Interface |
| 6 | Checked exceptions rollback | Q20 | rollbackFor, noRollbackFor |
| 7 | ReadOnly | Q21 | Dirty checking, replica routing, MVCC hint |
π΄ Senior Level (weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Isolation internals | Q5, Q6 (Senior) | Gap Lock, predicate lock, SSI implementation |
| 2 | Lost Update deep dive | Q10 (Senior) | Write skew, EvalPlanQual, atomic SQL |
| 3 | Propagation mechanics | Q13, Q14, Q15 (Senior) | ThreadLocal, suspend/resume, connection handling |
| 4 | Deadlock scenarios | Q15 (Senior) | REQUIRED + REQUIRES_NEW deadlock timeline |
| 5 | Self-invocation | Q22 | Proxy bypass, exposeProxy, AspectJ |
| 6 | Production scenarios | Q9, Q16 (Senior) | Phantom in batch jobs, checked exception war stories |
π Key Topic Connections
Topic: Isolation Levels and Anomalies
Q1 (ACID) β Q2 (Levels overview) β Q3 (Read Uncommitted) β Q4 (Read Committed)
β β β
Q5 (Repeatable Read) β Q6 (Serializable) Q7 (Dirty Read)
β β β
Q8 (Non-repeatable Read) β Q9 (Phantom Read) β Q10 (Lost Update)
Key connections:
- Q2 β Q7-Q10: Each level protects against specific anomalies
- Q4 β Q8: Non-repeatable read occurs specifically on Read Committed
- Q5 β Q9: Phantom read is prevented on Repeatable Read (in MySQL via Gap Lock)
Topic: Spring Transaction
Q13 (Propagation) β Q14 (NESTED) β Q15 (REQUIRED vs REQUIRES_NEW)
β β
Q16 (@Transactional) β Q17 (Levels) β Q22 (Self-invocation)
Key connections:
- Q13 β Q15: Propagation defines behavior when an external transaction exists
- Q14 β Q15: NESTED vs REQUIRES_NEW β savepoint vs independent commit
- Q16 β Q19: @Transactional rollback only for RuntimeException + Error
- Q22 β Q13: Self-invocation bypass β proxy is not involved, propagation doesnβt work
Topic: Rollback and Special Settings
Q18 (Rollback) β Q19 (Default rollback) β Q20 (Custom rollback)
β
Q21 (ReadOnly)
Key connections:
- Q18 β Q20: rollbackFor overrides default behavior
- Q19 β Q20: Checked exceptions = commit by default β rollbackFor = Exception.class
- Q21 β Q18: ReadOnly doesnβt cause rollback, but optimizes reads
π Cheat Sheet: What to Know for Each Level
π’ Junior
- ACID: Atomicity (all or nothing), Consistency (constraints satisfied), Isolation (donβt interfere with each other), Durability (persisted after commit)
- 4 isolation levels: Read Uncommitted, Read Committed, Repeatable Read, Serializable
- Dirty Read = see uncommitted data, Non-repeatable = value changed between SELECTs, Phantom = rows appeared/disappeared
- PostgreSQL default = Read Committed, MySQL default = Repeatable Read
- @Transactional: rollback for RuntimeException, commit for checked exceptions
π‘ Middle
- Read Committed: snapshot per SELECT β non-repeatable read possible
- Repeatable Read: snapshot for entire transaction β phantom read possible (in PostgreSQL via MVCC, in MySQL via Gap Lock β prevented)
- Propagation: REQUIRED (join existing), REQUIRES_NEW (new, external suspended), NESTED (savepoint)
- Self-invocation:
this.method()bypasses proxy β @Transactional doesnβt work - ReadOnly: disables dirty checking, optimizer hint, routing to replica (if configured)
π΄ Senior
- Serializable in PG = SSI (Serializable Snapshot Isolation), doesnβt block reads, but aborts on RW-conflict cycles
- Lost Update: atomic SQL (
SET x = x - 1) solves the problem without locking - REQUIRES_NEW overhead: suspend ~2ms, new connection ~0.5ms, Session suspend ~2ms, resume ~2ms
- NESTED: doesnβt work with JTA β fallback to REQUIRES_NEW. L1 cache trap after savepoint rollback
- Checked exception rollback: TransactionInterceptor sees exception BEFORE catch β setRollbackOnly() β UnexpectedRollbackException
π File Format
Each file contains:
- π’ Junior Level β basic understanding, simple analogies, examples
- π‘ Middle Level β internals, common mistakes, practical examples
- π΄ Senior Level β deep dive, edge cases, production experience, monitoring
- π― Interview Cheat Sheet β key points, common questions, red flags, related topics