🌿 Spring Boot Transaction Propagation

📌 1. Introduction

Transaction propagation defines how transactions behave when one method calls another method.

🔁 2. What is Transaction Propagation?

It controls whether a method should join an existing transaction or create a new one.

@Transactional(propagation = Propagation.REQUIRED)

⚙️ 3. Propagation Types

TypeDescription
REQUIREDJoin existing or create new
REQUIRES_NEWAlways create new transaction
SUPPORTSJoin if exists, else non-transactional
NOT_SUPPORTEDRun without transaction
MANDATORYMust have existing transaction
NEVERMust not have transaction
NESTEDNested transaction (savepoint)

📊 4. REQUIRED (Default)

ServiceA (TX)
   ↓
ServiceB (joins same TX)

📊 5. REQUIRES_NEW

ServiceA (TX1)
   ↓
ServiceB (TX2 - new)

📊 6. SUPPORTS

If TX exists → join
Else → run without TX

📊 7. MANDATORY & NEVER

MANDATORY → throws exception if no TX
NEVER → throws exception if TX exists

📊 8. NESTED

Outer TX
   ↓
Inner TX (Savepoint)
   ↓
Rollback inner only if needed

⚠️ 9. Common Pitfalls

🧠 10. Best Practices

🏁 Summary

REQUIRED → Join or create
REQUIRES_NEW → Always new
SUPPORTS → Optional