Skip to main content
Version: 4.6

Full Configuration Reference

This page lists every framework-level property under spring.datasource.dynamic.*. Parameters that belong to the connection pool itself (druid.* / hikari.*, …) are in the connection pool integration chapter, which also maps them to their native names.

The prefix constant is DynamicDataSourceProperties.PREFIX = "spring.datasource.dynamic".

Global configuration​

spring:
datasource:
dynamic:
enabled: true # enable the dynamic datasource
primary: master # the default datasource / datasource group
strict: false # strict mode
lazy: false # global lazy initialization
p6spy: false # enable p6spy output globally
seata: false # enable Seata globally
seata-mode: AT # Seata mode: AT | XA
grace-destroy: false # graceful shutdown
public-key: <built-in default public key> # the public key used to decrypt ENC()
strategy: com.baomidou.dynamic.datasource.strategy.LoadBalanceDynamicDataSourceStrategy
datasource: {} # datasource definitions, see the next section
PropertyTypeDefaultDescription
enabledbooleantruethe master switch. Driven by @ConditionalOnProperty(matchIfMissing = true) on the auto-configuration; set it to false to disable the framework entirely.
primaryStringmasterthe datasource or group used when none is specified explicitly.
strictbooleanfalsestrict mode. When true, a name in @DS that matches nothing throws CannotFindDataSourceException; when false it falls back to primary. Recommended in production, since a mistyped datasource name surfaces on the very first call.
lazybooleanfalseglobal lazy initialization. When true a datasource only builds its pool on first use. Can be overridden per datasource.
p6spybooleanfalseenable p6spy SQL logging globally. Can be overridden per datasource.
seatabooleanfalseenable the Seata proxy globally. Turning it on automatically disables the local transaction aspect (@DSTransactional), leaving transactions to Seata.
seata-modeAT / XAATthe Seata proxy mode, deciding whether datasources are wrapped in DataSourceProxy or DataSourceProxyXA.
grace-destroybooleanfalsegraceful shutdown. When true, the container waits for active connections to be returned during shutdown, for at most 10 seconds, before destroying datasources forcibly.
public-keyStringbuilt-in default public keythe public key used to decrypt ENC(...). Must be replaced in production, see database password encryption.
strategyClass<? extends DynamicDataSourceStrategy>LoadBalanceDynamicDataSourceStrategythe strategy that picks a member within a group. Round-robin and random are built in, and it is customizable.
datasourceMap<String, DataSourceProperty>{}the datasource definitions.

Per datasource​

Configuration under spring.datasource.dynamic.datasource.<datasource name>:

spring:
datasource:
dynamic:
datasource:
slave_1:
# ---- connection information ----
url: jdbc:mysql://host:3307/db
username: root
password: ENC(xxxxx)
driver-class-name: com.mysql.cj.jdbc.Driver
# ---- connection pool ----
type: com.zaxxer.hikari.HikariDataSource
pool-name: slave-1-pool
# ---- per-datasource switches (inherit the global value when omitted) ----
lazy: true
p6spy: false
seata: true
public-key: MFwwDQYJKoZI...
# ---- pool parameter overrides (this datasource only) ----
hikari:
maximum-pool-size: 10
# ---- init scripts ----
init:
schema: classpath:db/slave1-schema.sql
data: classpath:db/slave1-data.sql
continue-on-error: true
separator: ";"
PropertyTypeDefaultDescription
urlString—the JDBC URL. Can be omitted when jndi-name is set.
username / passwordString—credentials, ENC(...) supported.
driver-class-nameStringinferred via SPIthe JDBC driver class name. Since 3.2.0 it can be inferred through SPI, so it can usually be omitted.
typeClass<? extends DataSource>auto-detectedthe pool type. When unset, the first available one is used, in the order Druid > HikariCP > BeeCP > DBCP2 > Basic.
pool-nameStringthe datasource name in the configurationthe pool's name identifier, used to tell datasources apart in logs and metrics.
jndi-nameString—the JNDI datasource name. Setting it enables JNDI mode, in which case url is not needed.
lazyBooleaninherits the global valuewhether this datasource is lazily initialized.
p6spyBooleaninherits the global valuewhether this datasource is wrapped with p6spy.
seataBooleantruewhether this datasource is wrapped with the Seata proxy. Only meaningful when the framework-level seata=true.
public-keyStringinherits the global valuea decryption public key dedicated to this datasource.
druid.* hikari.* beecp.* dbcp2.* atomikos.* c3p0.* oracle-ucp.*—inherits the global valuepool parameters that override the global ones for this datasource only.

Init scripts init​

init lets each database run its own schema and data scripts; supported since 3.5.0:

PropertyTypeDefaultDescription
init.schemaString—path of the schema script, classpath: prefix supported.
init.dataString—path of the data script.
init.continue-on-errorbooleantruewhether to keep going when a single statement fails.
init.separatorString;the statement separator.

See startup init scripts.

AOP configuration​

spring:
datasource:
dynamic:
aop:
enabled: true # enable the @DS aspect
order: -2147483648 # aspect order, defaults to Ordered.HIGHEST_PRECEDENCE
allowed-public-only: true # intercept public methods only
allowed-spel-type-access: false # allow SpEL type references
PropertyTypeDefaultDescription
aop.enabledbooleantruewhen disabled, @DS stops working entirely (used in purely manual switching setups).
aop.orderintOrdered.HIGHEST_PRECEDENCEthe aspect priority. It must be higher than the transaction aspect, otherwise @DS runs after the transaction has started and the switch has no effect.
aop.allowed-public-onlybooleantruewhen true, @DS / @DSTransactional on non-public methods are silently ignored.
aop.allowed-spel-type-accessbooleanfalsewhether SpEL type references such as T(java.lang.Runtime) are allowed. Disabled by default to prevent SpEL injection.
warning

Do not change aop.order casually. The default HIGHEST_PRECEDENCE exists to guarantee "switch the datasource first, then open the transaction"; lowering it makes @DS run after @Transactional, so the thread ends up with the connection the transaction already bound.

Metrics and the operations endpoint​

PropertyTypeDefaultDescription
hikari-metrics.enabledbooleantruewhether to register Hikari pool metrics with Micrometer (requires Micrometer and HikariCP on the classpath).

The Actuator endpoint follows Actuator's own rules and is not configured through this framework's prefix:

management:
endpoints:
web:
exposure:
include: dynamic-datasource # expose as needed; not exposed by default

GET /actuator/dynamic-datasource returns the state of the four switches primary / strict / p6spy / seata, plus every datasource and its group composition. It deliberately does not return the URL or the username — a URL may carry credentials, and a status overview does not need them. See operations endpoint and metrics.

How global parameters are merged into a single datasource​

ConfigMergeCreator merges "global pool parameters" with "per-datasource pool parameters":

  1. take the global parameters (for example spring.datasource.dynamic.hikari.*) as the baseline
  2. override them field by field with the per-datasource parameters (for example ...datasource.slave_1.hikari.*)
  3. lazy and public-key are special: they only inherit the global value when the datasource does not set them explicitly (null / empty string)

So "set it globally once, fine-tune a few databases" is safe — you do not have to copy the parameters into every datasource.

Programmatic configuration​

Besides YAML, you can modify the configuration object at startup by implementing DynamicDataSourcePropertiesCustomizer:

@Bean
public DynamicDataSourcePropertiesCustomizer customizer() {
return properties -> properties.setStrict(true);
}

When you need to take over completely (for instance because the datasource list comes from an external config center), implement DynamicDataSourceProvider — see custom datasource provider.

Further reading​