Databases 5 min read

Master ShardingJDBC: Simplify Database Sharding, Read/Write Splitting & Transactions

This article introduces ShardingJDBC, a Java middleware that simplifies sharding, read/write separation, and distributed transactions, and provides a step‑by‑step Spring Boot example with configuration, code snippets, and verification of its core features.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Master ShardingJDBC: Simplify Database Sharding, Read/Write Splitting & Transactions

ShardingJDBC Overview

ShardingJDBC (Apache ShardingSphere JDBC) is a relational database middleware that simplifies operations on sharded and read‑write separated databases.

Key Features

Zero migration cost by directly wrapping the JDBC API.

Compatible with any Java ORM such as Hibernate or MyBatis.

Works with any third‑party connection pool (DBCP, C3P0, BoneCP, Druid, etc.).

Provided as a jar without a proxy layer; no extra deployment required.

Flexible sharding strategies supporting equality, BETWEEN, IN, multi‑key sharding.

Comprehensive SQL parsing (aggregation, grouping, ordering, LIMIT, OR, etc.).

Core Functions

The main functions are data sharding and read‑write separation, allowing applications to access multiple data sources transparently.

Data Sharding Capabilities

Database and table partitioning.

Read‑write separation.

Customizable sharding strategies.

Distributed primary key generation using Snowflake algorithm.

Distributed Transaction Support

Standardized transaction API.

Two‑phase commit.

Flexible (soft) transactions.

Database Governance

Dynamic configuration.

Orchestration and governance.

Data masking.

Visual traceability.

Architecture Diagram

Practical Example

The following Spring Boot project demonstrates read‑write separation using MyBatis, Druid, and ShardingJDBC with MySQL.

pom.xml Dependencies

Include spring-boot-starter-web, mybatis-spring-boot-starter, druid-spring-boot-starter, and sharding-jdbc-spring-boot-starter.

application.yml Configuration

Configure multiple data sources, master‑slave load‑balance algorithm (round_robin or random), and other ShardingJDBC settings.

Sample Controller

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public Object list() {
        return userService.list();
    }

    @GetMapping("/add")
    public Object add(@RequestParam Integer id,
                      @RequestParam String username,
                      @RequestParam String password) {
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        return userService.addUser(user);
    }
}

Verification

After invoking the two APIs, insert logs appear in the master database directory and query logs appear in the slave directory, confirming that ShardingJDBC has achieved read‑write separation.

Query SELECT u.* FROM user u
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.

Spring BootMyBatisread/write splittingDistributed TransactionsShardingJDBC
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

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.