Skip to content

fix: let pool creators built with the no-arg constructor create data sources - #825

Merged
huayanYu merged 2 commits into
baomidou:devfrom
linghengqian:fix/config-merge-null-global
Oct 20, 2026
Merged

huayanYu merged 2 commits into
baomidou:devfrom
linghengqian:fix/config-merge-null-global

Conversation

@linghengqian

Copy link
Copy Markdown
Contributor

目标分支 / Target branch:请选 dev(main 只接收来自 dev 的发版合并,误选会被自动提醒)。
Please choose dev as the base branch — main only receives release merges from dev.

变更类型 / What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style (formatting, no behaviour change)
  • Refactor
  • Build / CI
  • Doc
  • Other, please describe:

描述 / The description of the PR

说明为什么需要这个改动,而不只是改了什么;关联对应的 issue(Fixes #123)。

Fixes #821

HikariDataSourceCreator, BeeCpDataSourceCreator, Dbcp2DataSourceCreator, C3p0DataSourceCreator and OracleUcpDataSourceCreator leave their global pool config null when they are built with the no-arg constructor. ConfigMergeCreator#create then invoked the getters on that null config, so every createDataSource call threw a NullPointerException. The starters always pass the global config from DynamicDataSourceProperties, so only code that wires dynamic-datasource by hand ran into it, such as dynamic-datasource/dynamic-datasource-samples#61.

This PR has two commits:

  1. build: declare the Oracle ucp version in the version catalog: dynamic-datasource-creator and dynamic-datasource-spring-boot-common both hardcoded com.oracle.database.jdbc:ucp:23.4.0.24.05. The version now lives in gradle/libs.versions.toml, so that the tests in the next commit can use the same entry. The compileClasspath dependency trees of both modules are identical before and after.
  2. fix: let pool creators built with the no-arg constructor create data sources
    • ConfigMergeCreator treats a null global config as "no global defaults", as DruidConfigUtil#merge already does: mergeBasic and mergeProperties skip the global config when it is null, so only the per-datasource settings are applied. None of the five config classes has defaults of its own, so new HikariDataSourceCreator() now gives the same result as new HikariDataSourceCreator(new HikariCpConfig()), which is the expected behaviour described in the issue. A non-null global config is merged as before.
    • ConfigMergeCreatorTest covers both merge paths with a null global config, compares the result with an empty global config, and keeps the merge order for a non-null one.
    • NoArgsDataSourceCreatorTest is the reproducer from the issue for all five creators. Each case sets one per-datasource option that differs from the pool's default and checks that it reaches the data source.
    • The pools are compileOnly, so the creator module declares them again for its tests. Two more test runtime dependencies are needed. spring-jcl: the Boot 2 BOM excludes commons-logging from commons-dbcp2, and a Boot application gets LogFactory from spring-jcl in the same way. ojdbc8: the method signatures of PoolDataSourceImpl reference ojdbc types, so without ojdbc the introspection in ConfigMergeCreator fails with NoClassDefFoundError: oracle/jdbc/OracleShardingKeyBuilder, and the ojdbc11 that oracle-spring-boot-starter-ucp brings cannot load on the JDK 8 toolchain. Both are test scope only, so the published POMs do not change. The alternative was to leave UCP out of NoArgsDataSourceCreatorTest and rely on ConfigMergeCreatorTest alone, but then one of the five creators from the issue would have no test of its own.

The tests are in dynamic-datasource-creator rather than in the shared Boot 2/3/4 scenarios. The starters always pass a non-null global config, so their auto-configuration never reaches this path. Of these five pools, the starters' test classpaths only carry HikariCP, and adding the others there would change which creators the auto-configuration registers.

影响面 / Impact on the core contracts

本框架的核心契约很窄,改动通常会波及其中之一,请勾选并说明:

  • @DS 解析 / DynamicDataSourceContextHolder 的切换与栈语义(含多层嵌套、同类内部调用)
  • 数据源创建与连接池参数(creator、Druid/HikariCP/beecp/c3p0/dbcp2/UCP)
  • 事务语义(@DSTransactional、savepoint、XA/Atomikos、Seata)
  • 分组与负载均衡(GroupDataSource、strategy)
  • ENC(...) 加解密 / 配置属性(新增或改名配置项)
  • 构建产物(模块划分、POM 依赖范围 api/compileOnly、JDK 基线)
  • 以上均不涉及

新增/改名配置项时,请同步文档 https://doc.xiuceyun.cn 与 DynamicDataSourceProperties 的 Javadoc。

自检清单 / Checklist

  • 本地在 JDK 17+ 下 ./gradlew clean test 通过(Gradle 客户端要求 17+;Boot 2 线模块由 JDK 8 toolchain 编译测试)
  • 涉及 Boot 3/4 特有 API 时,对应 starter 模块也构建通过:./gradlew build
  • 为行为变更补充/调整了单元测试
  • 新文件带有 license header(Copyright © 2018 organization baomidou)
  • 没有引入新的三方依赖;若必须引入,已在 PR 描述中说明理由与替代方案对比
  • 考虑了 GraalVM Native Image 影响(反射/代理注册),必要时说明为何 nativeTest 可能失败
  • commit message 采用 conventional 前缀(fix: / feat: / build: / docs: …),一个 commit 做一件事

其他信息 / Other information

Verified locally with GraalVM CE 25.4.4.1 (JDK 25.0.4.1) running Gradle, on dev @ 7c321c1 plus this change:

  • Without the fix (only the new tests), all five NoArgsDataSourceCreatorTest cases fail with the NullPointerException from the issue (ConfigMergeCreator.mergeBasic). So do the two ConfigMergeCreatorTest cases with a null global config, one in mergeBasic and one in mergeProperties. The case with a non-null global config passes.
  • The issue's Repro.java on JDK 17.0.20.1 with the pool versions listed in the issue: with the released dynamic-datasource-creator 4.6.0 it prints the output shown in the issue. With the creator jar built from this branch, all ten lines print a data source class and the final call no longer throws.
  • ./gradlew clean build --no-build-cache: all modules pass.
  • nativeTest for dynamic-datasource-spring-boot3-starter (52 found, 46 passed, 6 skipped by @DisabledInNativeImage) and dynamic-datasource-spring-boot4-starter (48 found, 46 passed, 2 skipped): pass. No reachability metadata is needed: the fix only skips the reflective reads on a null global config, and a non-null one goes through the same reflection as before.
  • The Javadoc added here passes javadoc -Xdoclint:all,-missing with JDK 8 (-package for the tests).

dynamic-datasource-creator and dynamic-datasource-spring-boot-common both
hardcoded com.oracle.database.jdbc:ucp:23.4.0.24.05. The version now lives in
gradle/libs.versions.toml with the other dependency versions, so that the
creator tests can use the same entry. The compileClasspath dependency trees
of both modules are unchanged.
…sources

HikariDataSourceCreator, BeeCpDataSourceCreator, Dbcp2DataSourceCreator,
C3p0DataSourceCreator and OracleUcpDataSourceCreator leave their global pool
config null when they are built with the no-arg constructor, and
createDataSource passes it to ConfigMergeCreator#create as is. For these five
creators the config class and the target class differ, so the merge went on
and invoked the getters on the null config. Every createDataSource call threw
a NullPointerException, whatever the DataSourceProperty contained. The
starters always pass the global config from DynamicDataSourceProperties, so
only code that wires dynamic-datasource by hand ran into it.

ConfigMergeCreator now treats a null global config as "no global defaults",
as DruidConfigUtil#merge already does, and applies only the per-datasource
settings. None of the five config classes has defaults of its own, so this
is the same result as passing an empty global config. A non-null global
config is merged as before.

ConfigMergeCreatorTest covers both merge paths with a null global config,
compares the result with an empty global config and keeps the merge order
for a non-null one. NoArgsDataSourceCreatorTest runs the reproducer from baomidou#821
for all five creators, and each case checks that one per-datasource option
reaches the data source. The pools are compileOnly, so the creator module
declares them again for its tests. Two more test runtime dependencies are
needed: spring-jcl, because the Boot 2 BOM excludes commons-logging from
commons-dbcp2, and ojdbc8, because the method signatures of
PoolDataSourceImpl reference ojdbc types and the ojdbc11 that
oracle-spring-boot-starter-ucp brings cannot load on JDK 8.
@github-actions github-actions Bot added core dynamic-datasource core modules starters Spring Boot starter modules tests Test suites and fixtures ci CI, build and release labels Oct 19, 2026
@huayanYu
huayanYu merged commit 20e8e05 into baomidou:dev Oct 20, 2026
12 checks passed
@linghengqian
linghengqian deleted the fix/config-merge-null-global branch October 20, 2026 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci CI, build and release core dynamic-datasource core modules starters Spring Boot starter modules tests Test suites and fixtures

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants