Return to site

💾⚙️ SPRING @TRANSACTIONAL PROPAGATION — WHAT REALLY HAPPENS?

· springboot,spring,java,programmmer

🔸 TL;DR

▪️ A transaction = all-or-nothing unit of work (commit everything or rollback everything) 💣➡️✅

▪️ REQUIRED is the default and joins/creates a transaction.

▪️ REQUIRES_NEW always suspends current and starts a brand-new transaction.

▪️ SUPPORTS is “transaction optional”.

▪️ MANDATORY requires an existing transaction.

▪️ NOT_SUPPORTED suspends any transaction and runs without one.

▪️ NEVER fails if there’s already a transaction.

▪️ NESTED creates a savepoint inside the parent transaction.

Section image

🔸 WHAT IS A TRANSACTION?

▪️ A transaction groups multiple operations into a single logical unit:

All succeed ➜ COMMIT ✅

One fails ➜ ROLLBACK everything ❌

▪️ Spring uses @Transactional + a PlatformTransactionManager (e.g. JPA, JDBC) to manage this automatically.

🔸 REQUIRED (DEFAULT)

▪️ If a transaction exists ➜ join it.

▪️ If none exists ➜ create a new one.

▪️ Good general default for service methods.

🔸 REQUIRES_NEW

▪️ Always suspends any existing transaction.

▪️ Starts a completely independent transaction.

▪️ Useful for logging/audit or mail sending that must commit even if the caller rolls back. ✉️

🔸 SUPPORTS

▪️ If a transaction exists ➜ join it.

▪️ If none exists ➜ run non-transactional.

▪️ Good for read-only operations that can but don’t need a transaction.

🔸 MANDATORY

▪️ Requires an existing transaction.

▪️ If called without a transaction ➜ throws TransactionRequiredException.

▪️ Use when a method only makes sense inside a bigger business transaction.

🔸 NOT_SUPPORTED

▪️ Suspends any existing transaction.

▪️ Runs the method non-transactionally.

▪️ Useful for long-running or external calls (e.g. remote API) that must not lock DB. 🌐

🔸 NEVER

▪️ Must never be called inside a transaction.

▪️ If there is a transaction ➜ throws an exception.

▪️ Very rare in practice; use when a method would be dangerous inside a transaction.

🔸 NESTED

▪️ Runs inside an existing transaction with a savepoint.

▪️ If nested part fails ➜ can rollback only to its savepoint, parent can still commit.

▪️ Requires a DataSourceTransactionManager + JDBC savepoints support.

🔸 TAKEAWAYS

▪️ Choose REQUIRED by default, then change only when you have a clear business reason.

▪️ Use REQUIRES_NEW for independent work that must commit even on outer rollback.

▪️ Keep MANDATORY, NOT_SUPPORTED, NEVER, NESTED for advanced use cases with well-defined boundaries.

▪️ Always design propagation around business invariants, not “because it compiles” 😄

#Java #Spring #SpringBoot #Transactional #Database #CleanArchitecture #JavaDeveloper #BackendDevelopment

Find this lesson on page 66 of "Spring Certification" book:

🍃📗 Grab your Spring cert Book: https://bit.ly/springtify