Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ jobs:
# tx-seata-sample 依赖的 Seata 在 AOT 处理阶段就会构造 GlobalTransactionScanner
# 并要求 Seata Server 可达,且其应用上下文在 AOT 类型预判定阶段存在 null Advisor
# 问题,故暂不纳入。
# tx-atomikos-sample 依赖的 Atomikos JTA/XA 在 native image 下还需要额外的
# reachability metadata,待专项处理后纳入。
- name: Native test with Maven
run: |
./mvnw -B -e -PnativeTest clean test -pl "${{ matrix.module }}"
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
*/target/
target

### Atomikos transaction logs (tx-atomikos-sample) ###
tmlog*.log
transaction-logs/

### STS ###
.apt_generated
.classpath
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
- tx-local-sample:本地事务示例项目★★★★★★必看★★★★★★
- tx-seata-sample:基于seata的分布式事务集成使用示例,Seata Server 与数据库的准备、关键配置和本地运行步骤见
[模块 README](./spring-boot-4/tx-seata-sample/README.md)
- tx-atomikos-sample:基于 Atomikos XA 的分布式事务集成使用示例,Boot 4 下 JTA 事务管理器的手工装配、
`@Transactional` 与多数据源 MyBatis 的冲突、XA 建表注意点见
[模块 README](./spring-boot-4/tx-atomikos-sample/README.md)

## Spring Boot 3(`spring-boot-3/`)

Expand Down
17 changes: 17 additions & 0 deletions spring-boot-4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<commons-dbcp2.version>2.8.0</commons-dbcp2.version>
<sql-springboot4-starter.version>3.40.0-RELEASE</sql-springboot4-starter.version>
<seata-spring-boot-starter.version>2.6.0</seata-spring-boot-starter.version>
<!--Spring Boot 的 BOM 不管理 Atomikos,版本在此集中声明;
Atomikos 6.0.x 起才有 jakarta 构件(Boot 3/4 必须用 jakarta classifier)。-->
<atomikos.version>6.0.1</atomikos.version>
</properties>

<modules>
Expand All @@ -45,6 +48,7 @@
<module>quartz-sample</module>
<module>shardingsphere-jdbc-5.x-core-sample</module>
<module>spel-sample</module>
<module>tx-atomikos-sample</module>
<module>tx-local-sample</module>
<module>tx-seata-sample</module>
</modules>
Expand Down Expand Up @@ -133,6 +137,19 @@
<artifactId>seata-spring-boot-starter</artifactId>
<version>${seata-spring-boot-starter.version}</version>
</dependency>
<!--jakarta classifier 随依赖声明传递,Boot 3/4 无需再写 classifier。-->
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId>
<version>${atomikos.version}</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jta</artifactId>
<version>${atomikos.version}</version>
<classifier>jakarta</classifier>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
152 changes: 152 additions & 0 deletions spring-boot-4/tx-atomikos-sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# tx-atomikos-sample

基于 `dynamic-datasource-spring-boot4-starter` + MyBatis + Atomikos XA 的跨数据源分布式事务示例。
示例把订单(order)与账户(account)拆到两个数据源中,下单流程在一个 JTA 事务里跨库写入,
任意一步失败时由 Atomikos 以两阶段提交(2PC)保证两个库一起回滚。

本模块暂不支持 GraalVM native image:Atomikos 的 JTA/XA 在 native image 下还需要额外的
reachability metadata,因此未纳入 Native Test CI,与 tx-seata-sample 同属先例,
详见 [GraalVM Native Image](../../docs/graalvm-native-image.md)。

## 技术栈

| 组件 | 版本 |
| --- | --- |
| Spring Boot | 4.1.1 |
| dynamic-datasource-spring-boot4-starter | 4.7.0(取自根 `pom.xml` 的 `ds.version`) |
| Atomikos(`transactions-jdbc` / `transactions-jta`) | 6.0.1 `jakarta` classifier,版本取自父模块 `spring-boot-4-samples` 的 `atomikos.version` |
| H2 | 由 Spring Boot BOM 管理,XA 数据源为 `org.h2.jdbcx.JdbcDataSource` |

## 业务流程

入口是 `OrderService#placeOrder`:

1. `@DS("order")` 切到订单数据源,`UserTransaction.begin()` 开启 JTA 事务,插入 `orders`;
2. `AccountService#deduct` 以 `@DS("account")` 切到账户数据源,余额不足时影响行数为 0,
抛出 `InsufficientBalanceException`;
3. `UserTransaction.commit()` 提交,Atomikos 对 order、account 两个 XA 资源做两阶段提交;
任一步抛异常则 `rollback()`,两个库一起回滚。

## 关键配置

```yaml
spring:
datasource:
dynamic:
primary: order
atomikos: # 全局连接池参数,可在单个数据源下用同样的 key 覆盖
min-pool-size: 1
max-pool-size: 10
borrow-connection-timeout: 30
max-idle-time: 60
maintenance-interval: 60
datasource:
order:
# type 必须显式声明:Hikari 创建器优先级更高,type 缺省会接管并把 XA 数据源类
# 误当作 java.sql.Driver 加载而启动失败。
type: com.atomikos.jdbc.AtomikosDataSourceBean
# driver-class-name 指向 XA 数据源类(不是普通 JDBC Driver)
driver-class-name: org.h2.jdbcx.JdbcDataSource
url: jdbc:h2:mem:order;DB_CLOSE_DELAY=-1
username: sa
password: ""
account:
type: com.atomikos.jdbc.AtomikosDataSourceBean
driver-class-name: org.h2.jdbcx.JdbcDataSource
url: jdbc:h2:mem:account;DB_CLOSE_DELAY=-1
username: sa
password: ""
```

换成生产数据库时只需替换 `driver-class-name` 与连接信息:MySQL 用
`com.mysql.cj.jdbc.MysqlXADataSource`,PostgreSQL 用 `org.postgresql.xa.PGXADataSource`。

内置支持的 XA 数据源类(`XADataSourceEnum`):

| 数据库 | XA 数据源类 |
| --- | --- |
| MySQL 8+ | `com.mysql.cj.jdbc.MysqlXADataSource` |
| MySQL 5.1 | `com.mysql.jdbc.jdbc2.optional.MysqlXADataSource` |
| Oracle | `oracle.jdbc.xa.client.OracleXADataSource` |
| PostgreSQL | `org.postgresql.xa.PGXADataSource` |
| H2 | `org.h2.jdbcx.JdbcDataSource` |

其他数据库的 XA 数据源类不在枚举内,不会被 Atomikos 创建器认领,需要自定义 `DataSourceCreator`。

Atomikos 连接池参数(`spring.datasource.dynamic.atomikos.*`,单数据源下可用同名 key 覆盖):

| 参数 | 默认值 | 说明 |
| --- | --- | --- |
| `min-pool-size` | 1 | 最小连接数 |
| `max-pool-size` | 10 | 最大连接数 |
| `borrow-connection-timeout` | 30 | 池空时等待连接的秒数 |
| `reap-timeout` | 0 | 连接最大借用秒数;**Atomikos 6 已移除该配置**,4/5 生效 |
| `max-idle-time` | 60 | 超出 min-pool-size 的空闲连接保留秒数 |
| `test-query` | `SELECT 1` | 归还连接前的验证语句 |
| `maintenance-interval` | 60 | 池维护线程间隔秒数 |
| `default-isolation-level` | -1 | 默认隔离级别,负值忽略 |
| `max-lifetime` | 60 | 连接最大存活秒数,0 为不限制 |

## 四个必须手工处理的点

1. **Boot 4 不再自动装配 Atomikos 的 JTA 事务管理器**(Boot 3 及以前会自动装配),
需要自行声明 `UserTransactionManager` 与 `JtaTransactionManager`,见
`config/JtaConfig`;同时 Atomikos 的 `transactions-jta` 把 `jakarta.transaction-api`
声明为 provided,不会随依赖传递,需要显式引入,否则事务管理器会回退成
`JdbcTransactionManager`,XA 根本不会发生。
2. **多数据源 + MyBatis 不要用 Spring 的 `@Transactional`**:MyBatis-Spring 在 Spring 事务
同步激活时会把整个事务复用同一个 SqlSession(连同同一个连接),连接按事务内第一条语句的
`@DS` 确定数据源,后续 `@DS` 切换全部失效(表现为第二个库 "Table not found")。
本示例用编程式 JTA:

```java
@DS("order")
public void placeOrder(PlaceOrderRequest request) {
try {
userTransactionManager.begin();
orderMapper.insert(order); // -> order 数据源
accountService.deduct(request.getUserId(), amount); // @DS("account") -> account 数据源
userTransactionManager.commit(); // 两个 XA 资源 2PC
} catch (RuntimeException e) {
userTransactionManager.rollback(); // 两个库一起回滚
throw e;
} catch (Exception e) {
userTransactionManager.rollback();
throw new IllegalStateException("JTA 事务提交失败", e);
}
}
```

完整说明见 `service/OrderService` 的类注释。
3. **MyBatis 侧使用 `AtomikosTransactionFactory`**(`config/MyBatisConfig`):它在每次 MyBatis
事务开始时先按当前 `@DS` 确定真实数据源,再交给 `SpringManagedTransaction`。
4. **XA 数据源不要用 `datasource.*.init.schema` 建表**:脚本在 XA 连接池取出的连接上执行,
Atomikos 在事务管理器未运行时会话不提交,连接关闭时未提交的 DDL 会被回滚。本示例的
H2 建表在 `config/H2SchemaInitializer` 中用普通连接完成;真实项目请直接连库执行 DDL,
或交给 Flyway / Liquibase。

## 运行方式

```bash
# 单元测试(H2 内存库,无需 Docker)
./mvnw -pl spring-boot-4/tx-atomikos-sample test

# 启动应用,然后 curl -X POST http://localhost:8080/order \
# -H 'Content-Type: application/json' -d '{"userId":1,"amount":30.00}'
./mvnw -pl spring-boot-4/tx-atomikos-sample spring-boot:run
```

初始化数据由 `db/h2/schema-*.sql` 预置:`account(user_id = 1)` 的 `balance = 100.00`。

## 常见问题

- **`NoSuchMethodError: setReapTimeout(int)`**:Atomikos 6.0.0 起(jakarta 版本线)移除了
`setReapTimeout`,dynamic-datasource 4.6.0 及更早版本在 Atomikos 6 下启动即失败
([baomidou/dynamic-datasource#822](https://github.com/baomidou/dynamic-datasource/issues/822)),
需升级到包含该修复的版本(预计 4.7.0)。
- **Atomikos 6 与 Spring Boot 3/4**:必须使用 `jakarta` classifier 的
`transactions-jdbc` / `transactions-jta`;Atomikos 5 及更早只有 `javax` 构件。
- **`uniqueResourceName` 冲突**:Atomikos 要求每个 XA 资源名唯一,dynamic-datasource 会自动
取数据源的 key 作为 `uniqueResourceName`,手工添加数据源时注意不要重名。
- **事务日志**:Atomikos 会在工作目录写 `transaction-logs`,异常退出后重启会先做故障恢复,
属预期行为。
89 changes: 89 additions & 0 deletions spring-boot-4/tx-atomikos-sample/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot-4-samples</artifactId>
<groupId>com.baomidou</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>tx-atomikos-sample</artifactId>
<properties>
<!--Atomikos 6(jakarta)下 dynamic-datasource 4.6.0 会抛
NoSuchMethodError: setReapTimeout(int)(baomidou/dynamic-datasource#822),
修复随 4.7.0 发布;4.7.0 上线后本覆盖可删除,随根 pom 的 ds.version 统一升级。-->
<ds.version>4.7.0</ds.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot4-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!--Atomikos 6.0.x 起(jakarta 版本线)才有 jakarta 构件,Spring Boot 3/4 必须使用
jakarta classifier;Boot 的 BOM 不管理 Atomikos 版本,统一在父模块
spring-boot-4-samples 的 dependencyManagement 中声明。-->
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jta</artifactId>
<classifier>jakarta</classifier>
</dependency>
<!--Atomikos 的 transactions-jta 把 jakarta.transaction-api 声明为 provided,
不会随依赖传递;不显式引入时 Spring Boot 的 JtaAutoConfiguration 会因缺少
jakarta.transaction.Transaction 而退让,事务管理器回退成 JdbcTransactionManager,
@Transactional 退化为单库本地事务。-->
<dependency>
<groupId>jakarta.transaction</groupId>
<artifactId>jakarta.transaction-api</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<!--未声明 native-maven-plugin:Atomikos 的 JTA/XA 在 GraalVM native image 下还需要额外的
reachability metadata,本示例暂不纳入 Native Test CI,与 tx-seata-sample 同属先例,
详见 docs/graalvm-native-image.md。-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright © ${project.inceptionYear} organization baomidou
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.samples.atomikos;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Atomikos XA 分布式事务样例的启动类。
*
* <p>{@code @MapperScan} 显式声明 {@code sqlSessionFactoryRef},指向本模块 {@link
* com.baomidou.samples.atomikos.config.MyBatisConfig} 中装配了 {@code AtomikosTransactionFactory} 的
* SqlSessionFactory,同时避免 AOT 下 AUTOWIRE_BY_TYPE 装配丢失(JVM 行为不变)。
*/
@Slf4j
@SpringBootApplication
@MapperScan(
value = "com.baomidou.samples.atomikos.mapper",
sqlSessionFactoryRef = "sqlSessionFactory")
public class AtomikosApplication {

public static void main(String[] args) {
SpringApplication.run(AtomikosApplication.class, args);
log.info("open http://localhost:8080/swagger-ui.html");
}
}
Loading
Loading