Jta 多数据源事务

动态数据源JTA实现 (atomikos)

使用atomikos实现动态数据源事务管理

数据源配置

默认数据源配置,使用spring的jta配置即可:
1
spring:
2
jta:
3
status: true
4
atomikos:
5
datasource:
6
xa-data-source-class-name: com.alibaba.druid.pool.xa.DruidXADataSource
7
xa-properties:
8
url : jdbc:h2:mem:core;DB_CLOSE_ON_EXIT=FALSE
9
username : sa
10
password :
11
max-pool-size: 20
12
borrow-connection-timeout: 1000
13
connectionfactory:
14
max-pool-size: 20
15
local-transaction-mode: true
Copied!
动态数据源配置,默认提供一个 InMemoryAtomikosDataSourceRepository,在application.yml 中进行配置即可:
1
hsweb:
2
datasource:
3
jta:
4
test_ds: # 数据源ID
5
xa-data-source-class-name: com.alibaba.druid.pool.xa.DruidXADataSource
6
xa-properties: # 数据源的配置属性
7
url: jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE
8
username: sa
9
password:
10
max-pool-size: 20
11
borrow-connection-timeout: 1000
12
test_ds2: # 数据源ID
13
xa-data-source-class-name: com.alibaba.druid.pool.xa.DruidXADataSource
14
xa-properties: # 数据源的配置属性
15
url: jdbc:mysql://localhost:3306/hsweb?pinGlobalTxToPhysicalConnection=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false
16
# url: jdbc:h2:mem:test2;DB_CLOSE_ON_EXIT=FALSE
17
username: root
18
password: "123456" # 纯数字密码要加上双引号,不然启动会报Cannot initialize AtomikosDataSourceBean
19
max-pool-size: 20
20
borrow-connection-timeout: 1000
21
init-timeout: 20
Copied!
自定义,将数据源配置放到数据库中,实现 DynamicDataSourceConfigRepository<AtomikosDataSourceConfig> 接口并注入到spring容器即可
Last modified 2yr ago