Repository navigation
Description
使用的 starter 模块 / Starter artifact
其他 / 不确定 (other / unsure)
The problem is in the shared test fixtures (dynamic-datasource-test-common), so it affects the test suites of all three starters. Published artifacts are not affected.
dynamic-datasource 版本 (required)
4.6.0 (dev @ 10f90e3, main @ dfd8caa)
Spring Boot 版本 (required)
2.7.18 / 3.5.16 / 4.0.8 (the versions the three starters' test suites run against)
JDK 版本 (required)
openjdk version "25.0.4.1.1" 2026-08-18
OpenJDK Runtime Environment GraalVM CE 25.4.4.1.1+1.1 (build 25.0.4.1.1+1-jvmci-25.4-b23)
OpenJDK 64-Bit Server VM GraalVM CE 25.4.4.1.1+1.1 (build 25.0.4.1.1+1-jvmci-25.4-b23, mixed mode, sharing)
That JDK runs Gradle. The tests run on the build's toolchains: JDK 8 for the Boot 2 starter and JDK 17 for the Boot 3/4 starters.
连接池与版本 (optional)
Not pool-specific; observed with both Druid and HikariCP.
问题描述 / Describe the bug
Each shared scenario in dynamic-datasource-test-common/src/testFixtures/java/com/baomidou/dynamic/datasource/common/base/ boots its context from a helper @SpringBootApplication(scanBasePackages = "com.baomidou.dynamic.datasource.common") declared next to the abstract test. @SpringBootApplication is itself a @Configuration, and Spring Boot's TestTypeExcludeFilter only excludes test classes and @TestComponents. As a result, every scenario context also registers all the other helper applications in that package.
One of them, LoadDatasourceFromJDBCApplication (in AbstractLoadDatasourceFromJDBCTest.java), declares a DynamicDataSourceProvider bean. It loads master, db1, db2 and db3, with master pointing at jdbc:h2:~/test. The bean has no @Order, so it sorts after YmlDynamicDataSourceProvider (@Order(0)). DynamicRoutingDataSource#afterPropertiesSet then merges the providers' maps with putAll, so the JDBC provider's master replaces the master each scenario configures in its own properties.
Why this is a problem:
- Assertions about the configured
masteractually check a different datasource. For example,AbstractAutoConfigurationBindingTest#routingDataSourceShouldApplyPrimaryAndStrictresolves the JDBC provider'smaster, notjdbc:h2:mem:ac_master.AbstractDynamicDataSourceEndpointTestconfiguresmasteras aHikariDataSource, but it already works around the problem by asserting only onslave_1("slave_1 不会被其他 provider 覆盖"). - Any test that opens a connection on
mastercreates or writes the file database~/test.mv.dbin the developer's home directory, plus~/test.trace.dbwhen H2 traces an error. These files are outside the build directory and persist across runs. - The
masterpool built from the scenario's own properties is still created (Druid logs{dataSource-1,master} inited), butputAlldrops it before it reaches the routing map, soDynamicRoutingDataSource#destroynever closes it. - It is easy to trip over when adding tests. While the
init.*checks for [Bug] init.* nested config (DatasourceInitProperties) silently not bound in GraalVM native image; missing @NestedConfigurationProperty #809 were being written, a query placed onmasterfailed withTable "INIT_PROBE" not found (this database is empty)because it went to~/test.
One possible direction: keep scenario-specific beans out of component scanning, e.g. @Import the JDBC provider only into the scenario that needs it, or narrow scanBasePackages.
期望结果 / Expected behavior
A scenario's context contains only the providers and datasources that the scenario declares. Its configured master is then the datasource under test, and the suite writes nothing outside the build directory.
实际结果 / Actual behavior
Every scenario context also contains LoadDatasourceFromJDBCApplication's provider. AbstractDynamicDataSourceEndpointTest configures only master and slave_1, yet:
- the log reports
dynamic-datasource initial loaded [5] datasource; DynamicDataSourceEndpoint#summary()returnsdataSources={db1=SimpleDriverDataSource, db3=SimpleDriverDataSource, db2=SimpleDriverDataSource, master=SimpleDriverDataSource, slave_1=HikariDataSource};- the real datasource behind
masteris aSimpleDriverDataSourceforjdbc:h2:~/test, not the configured Hikari pool onjdbc:h2:mem:ep_master.
AbstractAutoConfigurationBindingTest configures three datasources but reports initial loaded [6] datasource. The Boot 2, 3 and 4 test logs all show the same counts.
相关配置 / Relevant configuration
# AbstractDynamicDataSourceEndpointTest (@SpringBootTest properties)
spring.datasource.dynamic.primary: master
spring.datasource.dynamic.datasource.master.type: com.zaxxer.hikari.HikariDataSource
spring.datasource.dynamic.datasource.master.driver-class-name: org.h2.Driver
spring.datasource.dynamic.datasource.master.url: jdbc:h2:mem:ep_master;DB_CLOSE_DELAY=-1
spring.datasource.dynamic.datasource.master.username: sa
spring.datasource.dynamic.datasource.slave_1.type: com.zaxxer.hikari.HikariDataSource
spring.datasource.dynamic.datasource.slave_1.driver-class-name: org.h2.Driver
spring.datasource.dynamic.datasource.slave_1.url: jdbc:h2:mem:ep_slave1;DB_CLOSE_DELAY=-1
spring.datasource.dynamic.datasource.slave_1.username: sa
# Added by LoadDatasourceFromJDBCApplication's provider in every context (type SimpleDriverDataSource):
# master -> jdbc:h2:~/test, db1..db3 -> jdbc:h2:mem:test2..test4复现步骤 / Minimal reproducer
-
Check out
dev. -
In
AbstractDynamicDataSourceEndpointTest#endpointShouldBeRegisteredAndSummarizeRouting, add the following assertion. The scenario configuresmaster.type=com.zaxxer.hikari.HikariDataSource:assertThat(dataSources.get("master")).isEqualTo("HikariDataSource");
-
Run
./gradlew :dynamic-datasource-spring-boot-starter:test --tests '*DynamicDataSourceEndpointTest'. -
The assertion fails because the summary reports
SimpleDriverDataSourceformaster. The test log showsinitial loaded [5] datasourcefor a scenario that configures two.
异常栈 / Stack trace
There is no exception: the replacement is silent. The only trace is from the query that went to ~/test (from ~/test.trace.db):
org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "INIT_PROBE" not found (this database is empty); SQL statement:
SELECT tag FROM init_probe WHERE id = 1 [42104-224]
提交前确认 / Preflight
- 我已搜索过 open/closed issues 与 Discussions,没有重复
- 我已确认 JDK / Spring Boot 与 starter 模块的版本是匹配的(见 README 版本选择表)
- 我已对配置内容脱敏(不含真实密码/密钥)
Activity
linghengqian commentedon Oct 17, 2026
linghengqian commentedon Oct 17, 2026
test: keep the JDBC provider fixture's master in memory
linghengqian commentedon Oct 17, 2026
Fixesstatement serves no purpose.