Skip to main content

Spring Boot multi-datasource framework · Apache-2.0 · since 2018

dynamic-datasource

One @DS annotation to switch datasources · local multi-DB transactions · dynamic datasource management · read-write splitting

How does a single request reach the right database?

Once a connection comes in, @DS resolves the target database or group, the router hands the statement to the matching datasource and returns the result the same way — none of this leaks into your business code.

Live routing preview
dynamic-datasource request routing diagramSpring Boot appService methodDynamic routingRoutingDataSource@DS("master")
🔀

One-line datasource switching

Annotate a service method with @DS("slave") to route to a database or group; SpEL keys, no-annotation approaches and the read-write splitting plugin included.

🧩

Non-invasive pool integration

First-class adapters for Druid / HikariCP / BeeCP / DBCP2 / JNDI with global and per-datasource configuration.

🧾

Local multi-DB transactions

@DSTransactional binds connections across databases under one transaction with unified commit/rollback; Seata AT/XA and Atomikos recipes included.

🧵

Dynamic datasource management

Add/remove datasources at runtime, lazy startup, no-datasource startup, inventories loaded from a bootstrap DB — the foundation for multi-tenancy.

30-Second Quick Start

Add the starter, configure your databases, annotate your service methods — done.

1. Configure datasources (the part before the first _ is the group name)

spring:
datasource:
dynamic:
primary: master
datasource:
master:
url: jdbc:mysql://host1:3306/db
username: root
password: ENC(...)
slave_1:
url: jdbc:mysql://host2:3306/db
username: root
password: ENC(...)

2. Switch with @DS

@Service
public class UserServiceImpl implements UserService {

@DS("master")
public void save(User user) {
// 走主库
}

@DS("slave") // slave_1 / slave_2 组内负载均衡
public List<User> listUsers() {
// 走从库组
}
}

Dependency coordinates (choose one per Spring Boot generation) and the full configuration reference: Getting Started

AI Era: put this documentation into your AI assistant

This repository ships an open-format Agent Skill. Once installed, AI coding assistants can answer questions like “why is datasource switching failing” directly in your project — installation, configuration and troubleshooting.

npx skills add baomidou/dynamic-datasource
🤖

Stuck on something?

The most frequently reported issues (@DS not working, startup errors, no switching inside transactions) are distilled into troubleshooting checklists.