Section 11 Β· 22 questions

Transactions

22 interview questions and answers in the Transactions section.

English Transactions Source Markdown
Language versions: English Russian Ukrainian

Questions in this section

  1. Decode Each Letter of ACID
  2. What Transaction Isolation Levels Exist?
  3. What is Read Uncommitted?
  4. What is Read Committed?
  5. What is Repeatable Read?
  6. What is Serializable?
  7. What is Dirty Read?
  8. What is Non-Repeatable Read?
  9. What is Phantom Read?
  10. What is Lost Update?
  11. What Is the Default Isolation Level in PostgreSQL
  12. What is the default isolation level in MySQL
  13. What is Propagation in Spring
  14. What does Propagation.NESTED do
  15. What is the difference between REQUIRED and REQUIRES_NEW
  16. What is @Transactional annotation?
  17. At what level can you use @Transactional?
  18. What is rollback in transactions?
  19. Which exceptions cause rollback by default?
  20. How to configure rollback for checked exceptions?
  21. What is readonly transaction?
  22. 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

# Question Difficulty
1 Decode each letter of ACID ⭐⭐
2 What transaction isolation levels exist ⭐⭐
3 What is Read Uncommitted ⭐⭐
4 What is Read Committed ⭐⭐
5 What is Repeatable Read ⭐⭐⭐
6 What is Serializable ⭐⭐⭐
7 What is Dirty Read ⭐⭐
8 What is Non-Repeatable Read ⭐⭐
9 What is Phantom Read ⭐⭐⭐
10 What is Lost Update ⭐⭐⭐
11 What is the default isolation level in PostgreSQL ⭐⭐
12 What is the default isolation level in MySQL ⭐⭐
13 What is Propagation in Spring ⭐⭐⭐
14 What does Propagation.NESTED do ⭐⭐⭐
15 What is the difference between REQUIRED and REQUIRES_NEW ⭐⭐⭐
16 What is the @Transactional annotation ⭐⭐
17 At what level can you use @Transactional ⭐⭐
18 What is rollback in transactions ⭐⭐
19 Which exceptions trigger rollback by default ⭐⭐
20 How to configure rollback for checked exceptions ⭐⭐
21 What is a readonly transaction ⭐⭐
22 What happens when you call a @Transactional method from another method of the same class ⭐⭐⭐

πŸ—ΊοΈ 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    β”‚        β”‚                β”‚        β”‚                    β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🟒 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