Mastering Spring Transaction Propagation: When and How Transactions Flow
This article explains Spring's seven transaction propagation modes, how they affect nested @Transactional method calls, and the resulting commit or rollback behavior in various scenarios, providing clear examples and diagrams for each propagation type.
1. What Is Spring Transaction Propagation?
In a Spring application, transaction propagation defines how transactions are handled when a transactional method calls another transactional method. It determines whether the called method joins the existing transaction, starts a new one, or executes without a transaction.
2. The Seven Propagation Types
PROPAGATION_REQUIRED : Join the current transaction if one exists; otherwise start a new transaction.
PROPAGATION_SUPPORTS : Execute within the current transaction if present; otherwise run non‑transactionally.
PROPAGATION_MANDATORY : Must run inside a transaction; throws an exception if none exists.
PROPAGATION_REQUIRES_NEW : Always start a new transaction, suspending any existing one.
PROPAGATION_NOT_SUPPORTED : Suspend any existing transaction and run non‑transactionally.
PROPAGATION_NEVER : Throw an exception if a transaction is present; otherwise run non‑transactionally.
PROPAGATION_NESTED : Execute within a nested transaction (savepoint) if a transaction exists; otherwise behaves like REQUIRED.
3. How Each Propagation Works
3.1 REQUIRED
The default mode. All nested transactional methods share the same transaction, so they commit or roll back together.
Example: Method A (REQUIRED) calls Method B (REQUIRED). Both operations are performed in a single transaction; if B throws an exception, the whole transaction rolls back.
3.2 REQUIRES_NEW
A new, independent transaction is started for the called method, suspending the outer transaction.
When Method A (REQUIRED) invokes Method B (REQUIRES_NEW), A's transaction is paused, B runs in its own transaction, and after B commits, A's transaction resumes.
3.3 SUPPORTS
If a transaction exists, the method joins it; otherwise it runs non‑transactionally.
Example 1: Neither A nor B has @Transactional; B (SUPPORTS) runs without a transaction. Example 2: A (REQUIRED) starts a transaction, B (SUPPORTS) joins it.
3.4 NOT_SUPPORTED
The method always runs non‑transactionally, suspending any existing transaction.
Example: A (REQUIRED) starts a transaction, B (NOT_SUPPORTED) suspends it, runs non‑transactionally, then A's transaction resumes and commits.
3.5 NEVER
The method must not run inside a transaction; an exception is thrown if a transaction is present.
When A (REQUIRED) calls B (NEVER), B throws an exception, causing A's transaction to roll back.
3.6 MANDATORY
The method must run inside an existing transaction; otherwise an exception is thrown.
If A has no transaction, B (MANDATORY) throws an exception. If A already has a transaction, B joins it.
3.7 NESTED
Creates a savepoint within the existing transaction, allowing the inner transaction to roll back independently.
Example: A (REQUIRED) starts a transaction, B (NESTED) creates a savepoint. If B fails, only B's changes roll back to the savepoint while A can still commit.
4. Summary
The article clarifies how Spring handles transaction propagation across multiple service methods. Understanding the seven propagation levels—REQUIRED, SUPPORTS, MANDATORY, REQUIRES_NEW, NOT_SUPPORTED, NEVER, and NESTED—helps developers choose the appropriate strategy for their business scenarios, ensuring correct commit and rollback behavior in enterprise applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
