Backend Development 14 min read

Fundamentals and Advanced Concepts of Spring Transaction Management

This article explains the core principles of Spring transaction management, including JDBC transaction steps, declarative @Transactional usage, AOP proxy mechanisms, propagation behaviors, isolation levels, nested transactions, and Spring Boot support, while also providing code examples and practical guidance for developers.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Fundamentals and Advanced Concepts of Spring Transaction Management

Basic Principles of Spring Transactions

Spring transactions fundamentally rely on the underlying database's transaction support; without it Spring cannot provide transaction functionality. For pure JDBC operations, the steps are:

Obtain a connection: Connection con = DriverManager.getConnection();

Start a transaction: con.setAutoCommit(false);

Execute CRUD operations.

Commit or roll back: con.commit(); // or con.rollback();

Close the connection: con.close();

When using Spring's transaction management, steps 2 and 4 are handled automatically by the framework.

Spring Transaction Mechanism

All data‑access technologies provide their own transaction APIs. Spring unifies these mechanisms through the PlatformTransactionManager interface, with specific implementations for each technology.

Data‑access technologies and implementations

Define a transaction manager bean in code:

@Bean
public PlatformTransactionManager transactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setDataSource(dataSource());
    return transactionManager;
}

Declarative Transactions

Spring supports declarative transaction management using the @Transactional annotation, which is implemented via AOP.

@Transactional
public void saveSomething(Long id, String name) {
    // database operations
}

Note that this annotation comes from org.springframework.transaction.annotation , not javax.transaction .

AOP Proxy Implementations

JDK dynamic proxy works on interfaces; private methods are not intercepted.

CGLIB creates a subclass; private methods are also not intercepted.

Java Dynamic Proxy

The process involves four steps:

Implement InvocationHandler to define custom handling.

Use Proxy with a class loader and interfaces to create the proxy class.

Obtain the proxy class constructor via reflection.

Instantiate the proxy, passing the handler to the constructor.

CGLIB Proxy

CGLIB (Code Generation Library) is a high‑performance library that can generate subclasses at runtime, allowing AOP without requiring interfaces.

Encapsulates ASM to create new classes dynamically.

Used for AOP when JDK proxies are unsuitable.

Principle Differences

Java dynamic proxy uses reflection to create a class implementing the target interfaces, while CGLIB modifies bytecode to generate a subclass of the target class.

If the target implements an interface, Spring defaults to JDK proxy.

You can force CGLIB even when interfaces exist.

If the target has no interfaces, Spring automatically uses CGLIB.

Spring Transaction Propagation Attributes

Propagation attributes define how Spring handles multiple concurrent transactions. The table below (image) lists the constants defined in TransactionDefinition :

Database Isolation Levels

Isolation levels control phenomena such as dirty reads, non‑repeatable reads, and phantom reads (see image).

Higher isolation guarantees data integrity but reduces concurrency performance. Most databases default to READ_COMMITTED ; MySQL InnoDB defaults to REPEATABLE_READ .

Nested Transactions

Understanding propagation helps manage nested transaction scenarios. Examples:

PROPAGATION_REQUIRED (default)

If an outer transaction is active, the inner method joins it; otherwise, a new transaction is started.

PROPAGATION_REQUIRES_NEW

The inner method always starts a new transaction, suspending the outer one.

PROPAGATION_SUPPORTS

The inner method participates in an existing transaction if present; otherwise, it runs without a transaction.

PROPAGATION_NESTED

The inner method runs within a savepoint, allowing independent rollback while the outer transaction can still commit.

void methodA() {
    try {
        ServiceB.methodB();
    } catch (SomeException) {
        // handle other business logic
    }
}

When the nested transaction rolls back to its savepoint, the outer transaction can decide to commit or roll back based on its own outcome.

Summary

For transactional needs, prefer using Spring's TransactionCallback interface; if you use annotations, be sure to understand propagation and isolation settings to avoid unexpected behavior.

Spring Boot Transaction Support

Spring Boot automatically enables annotation‑driven transactions via org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration .

Read‑Only Transactions (@Transactional(readOnly = true))

A read‑only transaction hides changes committed by other transactions after the transaction starts, providing a consistent snapshot for queries and allowing certain JDBC optimizations.

Use cases:

Single‑statement queries usually do not need explicit transactions.

Multiple‑statement queries (e.g., reports) require a read‑only transaction to ensure consistent results.

References:

http://www.codeceo.com/article/spring-transactions.html http://www.cnblogs.com/fenglie/articles/4097759.html https://www.zhihu.com/question/39074428/answer/88581202 http://blog.csdn.net/andyzhaojianhui/article/details/51984157

Promotional Section: "Second Edition: Internet Company Interview Questions"

The author offers a PDF collection of 96 interview question sets (totaling 3,625 pages) covering Java, Spring, databases, middleware, big data, and more. Readers can obtain the material by scanning the QR code provided in the article.

JavatransactionAOPSpringReadOnlyisolationPropagation
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

0 followers
Reader feedback

How this landed with the community

login 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.