Databases 6 min read

Issues When Using Transactions with Database Read/Write Splitting

This article examines the challenges of using transactions with database read/write splitting, including potential master‑slave switching errors, connection‑pool deadlocks caused by isolation levels and propagation, and presents Spring‑based dynamic data source and transaction management solutions.

JD Tech
JD Tech
JD Tech
Issues When Using Transactions with Database Read/Write Splitting

The article discusses the problems that may arise when using transactions in a read/write‑separated database architecture, focusing on two main questions: whether a transaction can mistakenly operate on a slave database and whether transaction isolation levels and propagation can cause connection‑pool deadlocks.

For the first question, it explains that a thread may select a slave for a SELECT operation, but when the transaction starts, Spring’s AOP clears the ThreadLocal holding the slave identifier, ensuring the transaction switches to the master and avoiding master‑slave switching errors.

For the second question, it shows that if both the main transaction and a secondary operation (such as GetSeq) share the same DataSource/connection pool, exhausting the pool can lead to deadlock; the recommended fix is to use a separate pool or set PROPAGATION_REQUIRES_NEW.

The implementation of dynamic data source routing is described using Spring’s AbstractRoutingDataSource, a singleton DynamicDataSource, and a ThreadLocal‑based DynamicDataSourceHolder to store the current lookup key (master or slave).

Transaction routing is achieved via Spring AOP, where proxies intercept method calls, clear the ThreadLocal before transaction interception, and ensure the correct master is used during transactional operations.

Transaction management details include the use of @Transactional, the handling of multiple transaction managers, and XML configuration examples that bind the appropriate DataSource and transaction manager beans.

The article also briefly reviews MySQL’s transaction isolation levels, noting that InnoDB (the default engine) uses REPEATABLE READ, and explains how isolation and locking provide atomicity, consistency, and durability.

DatabaseSpringRead-Write SplittingDynamic Data SourceTransaction ManagementIsolation Level
JD Tech
Written by

JD Tech

Official JD technology sharing platform. All the cutting‑edge JD tech, innovative insights, and open‑source solutions you’re looking for, all in one place.

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.