Skip to content

[Bug] Shared test scenarios are not isolated: a scanned-in JDBC provider replaces the configured master with jdbc:h2:~/test #812

Closed

Description

@linghengqian

使用的 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 master actually check a different datasource. For example, AbstractAutoConfigurationBindingTest#routingDataSourceShouldApplyPrimaryAndStrict resolves the JDBC provider's master, not jdbc:h2:mem:ac_master. AbstractDynamicDataSourceEndpointTest configures master as a HikariDataSource, but it already works around the problem by asserting only on slave_1 ("slave_1 不会被其他 provider 覆盖").
  • Any test that opens a connection on master creates or writes the file database ~/test.mv.db in the developer's home directory, plus ~/test.trace.db when H2 traces an error. These files are outside the build directory and persist across runs.
  • The master pool built from the scenario's own properties is still created (Druid logs {dataSource-1,master} inited), but putAll drops it before it reaches the routing map, so DynamicRoutingDataSource#destroy never 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 on master failed with Table "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() returns dataSources={db1=SimpleDriverDataSource, db3=SimpleDriverDataSource, db2=SimpleDriverDataSource, master=SimpleDriverDataSource, slave_1=HikariDataSource};
  • the real datasource behind master is a SimpleDriverDataSource for jdbc:h2:~/test, not the configured Hikari pool on jdbc: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

  1. Check out dev.

  2. In AbstractDynamicDataSourceEndpointTest#endpointShouldBeRegisteredAndSummarizeRouting, add the following assertion. The scenario configures master.type=com.zaxxer.hikari.HikariDataSource:

    assertThat(dataSources.get("master")).isEqualTo("HikariDataSource");
  3. Run ./gradlew :dynamic-datasource-spring-boot-starter:test --tests '*DynamicDataSourceEndpointTest'.

  4. The assertion fails because the summary reports SimpleDriverDataSource for master. The test log shows initial loaded [5] datasource for 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

  1. linghengqian commented on Oct 17, 2026

    @linghengqian
    ContributorAuthor
    • I am working on the current issue.
  2. linghengqian commented on Oct 17, 2026

    @linghengqian
    ContributorAuthor
  3. linghengqian commented on Oct 17, 2026

    @linghengqian
    ContributorAuthor
    • Out of curiosity, I would expect a GitHub bot to automatically close the relevant issue once a PR is merged; otherwise, the Fixes statement serves no purpose.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions