开发项目遇到需求对接多个数据源,这时就需要spring 对分布式事务支持,一般采用Atomikos分布式事务管理器方式实现。
引入包:
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
数据源配置获取AtomikosDataSourceBean
public AtomikosDataSourceBean getAtomikosDataSourceBean(MysqlXADataSource mysqlXADataSource, String flag) {
AtomikosDataSourceBean dataSourceBean = new AtomikosDataSourceBean();
dataSourceBean.setUniqueResourceName(flag);
dataSourceBean.setXaDataSource(mysqlXADataSource);
dataSourceBean.setMinPoolSize(env.getProperty("db." + flag + ".minIdle", Integer.class));
dataSourceBean.setPoolS