Skip to content

feat: 新增 tx-atomikos-sample(Atomikos XA 分布式事务示例) - #62

Open
huayanYu wants to merge 2 commits into
masterfrom
feat/tx-atomikos-sample
Open

huayanYu wants to merge 2 commits into
masterfrom
feat/tx-atomikos-sample

Conversation

@huayanYu

@huayanYu huayanYu commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

背景

dynamic-datasource 很早就支持 Atomikos XA,但文档与示例一直空缺:

  • 文档站(doc.xiuceyun.cn)只在功能列表里提过一次 Atomikos,没有专属页面;
  • 项目 Wiki 已关闭(has_wiki=false),内容不可见,仓库内也没有 Atomikos 说明;
  • 示例仓库有 tx-local-sample 与 tx-seata-sample,唯独没有 Atomikos;
  • 主仓库 README 的 Atomikos 片段还漏了关键的 type: com.atomikos.jdbc.AtomikosDataSourceBean(缺省时 Hikari 创建器会接管并把 XA 数据源类当作 java.sql.Driver 加载,启动直接失败)。

本 PR 补齐示例:新增 spring-boot-4/tx-atomikos-sample,并把实践中的四个坑写进模块 README(依赖版本窗口、Boot 4 的 JTA 手工装配、@Transactional 与多数据源 MyBatis 的冲突、XA 建表注意点),模块 README 自成体系。

示例内容

Boot 4.1.1 + Atomikos 6.0.1(jakarta)+ 两个 H2 XA 数据源(order / account),
OrderService#placeOrder 在一个 JTA 事务里跨库写入,3 个测试覆盖:XA 数据源与 JTA 装配、
两阶段提交成功、余额不足时两个库一起回滚。无需 Docker,本地 ./mvnw -pl spring-boot-4/tx-atomikos-sample test 即可跑。

README 里记录了四个非显然的装配点:

  1. Boot 4 起 JTA 自动配置只剩 JNDI 变体,需自行装配 UserTransactionManager + JtaTransactionManager;
    且 transactions-jta 的 jakarta.transaction-api 是 provided 作用域,不显式引入会回退成 JdbcTransactionManager;
  2. 多数据源 + MyBatis 不能用 Spring @Transactional:MyBatis-Spring 会把整个事务复用同一个 SqlSession/连接,
    事务内 @DS 切换失效,故用编程式 JTA;
  3. MyBatis 侧配 AtomikosTransactionFactory;
  4. XA 数据源不要用 datasource.*.init.schema 建表(Atomikos 非事务连接关闭时回滚未提交 DDL)。

版本依赖说明(重要)

模块内覆盖 ds.version=4.7.0:dynamic-datasource 4.6.0 在 Atomikos 6 下启动即抛
NoSuchMethodError: setReapTimeout(int)(baomidou/dynamic-datasource#822,修复 PR #823 已合入 dev),
该修复预计随 4.7.0 发布。4.7.0 发布前本模块的 Test CI 会因依赖不存在而失败,发布后转绿;
届时根 pom 的 ds.version 统一升级到 4.7.0 后,模块内的覆盖可删除。其余 15 个示例仍固定 4.6.0,不受影响。

已本地验证:以 dev(含 #823)本地发布 4.7.0 后,模块 3/3 测试通过;spotless / license / checkstyle 均 0 违规。

其他变更

  • spring-boot-4/pom.xml:集中声明 atomikos.version(6.0.1,jakarta classifier)并注册模块;
  • .github/workflows/ci.yml:native 任务排除说明补充 tx-atomikos-sample(Atomikos JTA/XA 需要额外 reachability metadata);
  • .gitignore:忽略 Atomikos 事务日志 tmlog*.log / transaction-logs/;
  • 根 README.md:Spring Boot 4 模块清单补充条目。

dynamic-datasource supports Atomikos XA since long ago, but neither the
documentation nor the samples ever covered it: the docs site only mentions
Atomikos in a feature list, the wiki has no page, and the samples repo has
tx-local / tx-seata but nothing for Atomikos. This adds the missing piece.

- new spring-boot-4/tx-atomikos-sample: Boot 4.1.1 + Atomikos 6.0.1 (jakarta)
  + two H2 XA datasources, placeOrder spans order/account in one JTA
  transaction with 2PC commit and rollback, verified by 3 tests (no Docker).
- pins ds.version 4.7.0 in the module only: 4.6.0 throws
  NoSuchMethodError: setReapTimeout(int) under Atomikos 6
  (baomidou/dynamic-datasource#822); drop the override once 4.7.0 ships.
- atomikos.version + jakarta-classifier dependencyManagement in
  spring-boot-4-samples.
- documents the four non-obvious wiring points in the module README:
  Boot 4 no longer auto-configures the Atomikos transaction manager,
  jakarta.transaction-api is provided-scope, Spring @transactional breaks
  @ds routing with MyBatis (one SqlSession per transaction), and XA pools
  never commit init scripts.
- native image opt-out with rationale in ci.yml, following tx-seata-sample.
@huayanYu

huayanYu commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

更新:#823(Atomikos 6 setReapTimeout 修复)已合入主仓库 dev(commit 2ea99ea)。本示例在模块内覆盖 ds.version=4.7.0,等该版本发布后 CI 即转绿;已用 dev 最新代码本地发布 4.7.0 复跑,3/3 测试通过。4.7.0 发布后根 pom 的 ds.version 统一升级时,模块内的版本覆盖可删除。

Adds the XA datasource class table, the Atomikos pool parameter table and the
programmatic JTA snippet, so the module no longer needs a separate wiki page
to be complete.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant