Skip to content

refactor: 将数据源解包逻辑抽成有序 SPI(承接 #804) - #808

Merged
huayanYu merged 2 commits into
devfrom
refactor/datasource-unwrapper-spi
Oct 15, 2026
Merged

huayanYu merged 2 commits into
devfrom
refactor/datasource-unwrapper-spi

Conversation

@huayanYu

@huayanYu huayanYu commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

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

  • Refactor

描述 / The description of the PR

Fixes #792。承接 #804(原作者 @Pika-Yalei)。

DynamicRoutingDataSource.closeDataSource 原本把 P6Spy 与 Seata 的可选代理知识硬编码在自身(instanceof + 反射读 P6DataSource.realDataSource)。本 PR 把这部分知识抽成 DataSourceUnwrapper SPI 与一条有序链,使新增代理式集成只需注册 Java service provider,无需改动路由数据源。

代码内容与 #804 一致,本 PR 额外只做了一件事——把 #804 的两个提交线性重放到当前 dev 上并解决冲突:

  • 贡献者的 fork 未开启 maintainerCanModify,维护者无法直接更新其分支,因此在此重放同一组提交,并完整保留原作者信息(两个 commit 的 author 均为 YaLei <pikapi010410@gmail.com>,已关联其 GitHub 账号)。
  • 唯一冲突在 DynamicRoutingDataSource:dev 侧(aed8872)把 getPrimary() 由 protected 改为 public 并补了 javadoc,而 refactor: extract datasource unwrapping into an ordered SPI #804 的第二个提交在同一锚点上方插入了 setP6spy / setSeata / getDataSourceUnwrapperChain。两处互不干涉,取并集;冲突在第二个提交内解决。
  • 分支为线性历史(2 个提交,无 merge commit),因此 Rebase and merge / Squash / Merge commit 三种方式均可用。
  • 相对 dev 的差异仍为 19 文件 +1078/-18,与 refactor: extract datasource unwrapping into an ordered SPI #804 原始改动集一致(ahead_by: 2,behind_by: 0)。

实现要点:

  • 通过 META-INF/services/com.baomidou.dynamic.datasource.unwrapper.DataSourceUnwrapper 发现扩展,按 Ordered 排序
  • P6Spy / Seata 解包移入 optional-aware 的内置实现(order 0/1,复刻原有"先 seata 后 p6spy"顺序)
  • 保留既有的 opt-in 开关与 ItemDataSource 真实池快捷路径
  • 只剥一层,不递归;解包失败仍冒泡到原有 catch 打 warn;immediate / graceful 销毁语义不变
  • 链按路由数据源懒加载缓存,setP6spy / setSeata 变更时失效重建
  • 采用 ClassUtils.getDefaultClassLoader()(TCCL 优先、Spring 兜底)的类加载器策略

影响面 / Impact on the core contracts

  • 以上均不涉及

这是数据源销毁路径的重构,不改变数据源创建、事务行为、路由、配置项名称或生产依赖作用域。P6Spy / Seata 仍是 compileOnly,仅在测试作用域复用。核心新增字节码仍为 class-file major version 52(Java 8)。

自检清单 / Checklist

  • 本地 JDK 21 下 ./gradlew :dynamic-datasource-spring:test 通过(100 tests, 0 failures, 0 skipped)
  • 完整托管矩阵在 refactor: extract datasource unwrapping into an ordered SPI #804 与本次 head 上均已运行:Test CI JDK 17/21/25/27、NativeTest CI boot3/boot4、CodeQL、Dependency review
  • 回归测试由原 PR 提供(45 例:15 个 SPI 契约/生命周期/类加载器用例 + 每个 starter 线 10 个真实池生命周期用例)
  • 新文件均带 Apache 2.0 license header(Copyright © 2018 organization baomidou)
  • 未引入新的三方依赖
  • GraalVM 影响已处理:新增 HikariDataSource#getHikariPoolMXBean() 与 HikariPool#getActiveConnections() 的条件化反射元数据,修复了 4 个优雅销毁用例在 native 下缺少 invocation metadata 的失败
  • commit message 采用 conventional 前缀,一个 commit 做一件事

其他信息 / Other information

#804 与本 PR 内容等价,保留一个即可:合入本 PR 后请关闭 #804;若 @Pika-Yalei 更希望自行 rebase 原分支,也可以关掉本 PR。

评审要点(我在 #804 上的两条评论已记录,其中一条为自我更正):

  • 该 SPI 是本仓库首个运行时 ServiceLoader。经核实 Native Image 会自动注册构建期类路径上的 provider,且 reachability metadata 并无 services 类型,因此无需额外注册——这一点我最初判断有误,已在 refactor: extract datasource unwrapping into an ordered SPI #804 更正。
  • 链现在只构建一次,使用的是首次关闭数据源时的 TCCL;该行为已写入接口 Javadoc。

@github-actions github-actions Bot added core dynamic-datasource core modules tests Test suites and fixtures labels Oct 15, 2026
@huayanYu
huayanYu force-pushed the refactor/datasource-unwrapper-spi branch from 47e663e to 90dfdec Compare October 15, 2026 15:45
@huayanYu
huayanYu merged commit dada706 into dev Oct 15, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core dynamic-datasource core modules tests Test suites and fixtures

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants