Understanding Spring Transaction Propagation, Rollback, and @Transactional Usage

This article explains Spring's transaction propagation behaviors, rollback mechanisms, common pitfalls such as self-invocation, and provides solutions using AOP proxies and @EnableAspectJAutoProxy, supplemented with code examples and a brief promotion of a related community.

Top Architect
Top Architect
Top Architect
Understanding Spring Transaction Propagation, Rollback, and @Transactional Usage

The author, a senior architect, shares insights gained from several days of experimenting with Spring's @Transactional annotation, focusing on transaction control and common issues.

Transaction propagation behaviors are defined in TransactionDefinition and include the following constants: TransactionDefinition.PROPAGATION_REQUIRED: Join existing transaction or create a new one (default). TransactionDefinition.PROPAGATION_REQUIRES_NEW: Always start a new transaction, suspending any existing one. TransactionDefinition.PROPAGATION_SUPPORTS: Join existing transaction if present; otherwise execute non‑transactionally. TransactionDefinition.PROPAGATION_NOT_SUPPORTED: Execute non‑transactionally and suspend any existing transaction. TransactionDefinition.PROPAGATION_NEVER: Execute non‑transactionally; throw an exception if a transaction exists. TransactionDefinition.PROPAGATION_MANDATORY: Must join an existing transaction; otherwise throw an exception. TransactionDefinition.PROPAGATION_NESTED: Execute within a nested transaction if a transaction exists; otherwise behaves like PROPAGATION_REQUIRED.

Rollback mechanism : Spring’s declarative transaction management rolls back on unchecked (runtime) exceptions. The transaction boundary starts before the business method and commits or rolls back after the method finishes, depending on whether a RuntimeException was thrown.

If a method catches an exception with try { } catch (Exception e) { }, the caught block runs outside the transaction. To trigger rollback, the catch block must re‑throw a RuntimeException, a scenario often asked in interviews.

Self‑invocation problem : When a @Transactional method calls another method of the same class (e.g., this.method2()), the second method’s annotation is ignored because the call bypasses the proxy that provides the transaction advice.

Solution : Obtain the current proxy via AopContext.currentProxy() and invoke the target method through that proxy. Enable proxy exposure with @EnableAspectJAutoProxy(exposeProxy = true) on the Spring Boot application class. The called method must be public for the proxy to apply.

Example controller and service code:

@RestController
public class TransactionalController {
    @Autowired
    private TransactionalService transactionalService;

    @PostMapping("transactionalTest")
    public void transacionalTest() {
        transactionalService.transactionalMethod();
    }
}
public interface TransactionalService {
    void transactionalMethod();
}

The service implementation (not shown in full) demonstrates various scenarios where the transaction either succeeds or rolls back, including the use of nested calls, proxy‑based calls, and the effect of different propagation settings.

At the end of the technical discussion, the author adds a brief promotion for a ChatGPT‑focused community, offering free accounts, learning resources, and limited‑time discount coupons.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend Developmentspringtransactionaltransaction-management
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.