使用springboot+jta+atomikos 分布式事物Transactional管理
1.Application启动类加入注解:
OrderConfig,MemberConfig–>配置类名称
@EnableConfigurationProperties({OrderConfig.class, MemberConfig.class})
2.application.yml配置
spring:
datasource:
###会员数据库
member:
url: jdbc:mysql://localhost:3306/user
username: root
password: root
borrowConnectionTimeout: 30
loginTimeout: 30
maintenanceInterval: 60
maxIdleTime: 60
maxLifetime: 20000
maxPoolSize: 25
minPoolSize: 3
uniqueResourceName: orderDatasource
###订单数据库
order:
url: jdbc:mysql://localhost:3306/order
username: root
password: root
borrowConnectionTimeout: 30
loginTimeout: 30
maintenanceInterval: 60
maxIdleTime: 60
maxLifetime: 20000
maxPoolSize: 25
minPoolSize: 3
uniqueResourceName: memberDatasource
3.pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot 整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- springboot 统一事务管理 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
4.创建配置config
4.1:member包下
@ConfigurationProperties(prefix = "spring.datasource.member")
@Data
public class MemberConfig {
private String url;
private String userName;
private String passWord;
private int minPoolSize;
private int maxPoolSize;
private int maxLifetime;
private int borrowConnectionTimeout;
private int loginTimeout;
private int maintenanceInterval;
private int maxIdleTime;
private String testQuery;
private String uniqueResourceName;
}
@Configuration
@MapperScan(basePackages = "com.member.mapper", sqlSessionTemplateRef = "memberSqlSessionTemplate")
public class MemberDataSourceConfig {
//@Configuration xml MemberDataSourceConfig.xml
/**
* 创建我们的DataSource
*
* @return
*/
@Bean("memberDataSource")
public DataSource memberDataSource(MemberConfig memberConfig) throws SQLException {
// 1.创建MysqlXADataSource
MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
mysqlXaDataSource.setUrl(memberConfig.getUrl());
mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
mysqlXaDataSource.setPassword(memberConfig.getPassWord());
mysqlXaDataSource.setUser(memberConfig.getUserName());
mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
// 2.将本地事务注册到创 Atomikos全局事务
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(mysqlXaDataSource);
xaDataSource.setUniqueResourceName(memberConfig.getUniqueResourceName());
xaDataSource.setMinPoolSize(memberConfig.getMinPoolSize());
xaDataSource.setMaxPoolSize(memberConfig.getMaxPoolSize());
xaDataSource.setMaxLifetime(memberConfig.getMaxLifetime());
xaDataSource.setBorrowConnectionTimeout(memberConfig.getBorrowConnectionTimeout());
xaDataSource.setLoginTimeout(memberConfig.getLoginTimeout());
xaDataSource.setMaintenanceInterval(memberConfig.getMaintenanceInterval());
xaDataSource.setMaxIdleTime(memberConfig.getMaxIdleTime());
xaDataSource.setTestQuery(memberConfig.getTestQuery());
return xaDataSource;
}
/**
* 创建我们的SqlSessionFactory
*
* @param dataSource
* @return
* @throws Exception
*/
@Bean(name = "memberSqlSessionFactory")
public SqlSessionFactory memberSqlSessionFactory(@Qualifier("memberDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean.getObject();
}
/**
* 创建订单sqlSesion模版
*
* @param sqlSessionFactory
* @return
* @throws Exception
*/
@Bean(name = "memberSqlSessionTemplate")
public SqlSessionTemplate memberSqlSessionTemplate(
@Qualifier("memberSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
4.2:order包下
@ConfigurationProperties(prefix = "spring.datasource.order")
@Data
public class OrderConfig {
private String url;
private String userName;
private String passWord;
private int minPoolSize;
private int maxPoolSize;
private int maxLifetime;
private int borrowConnectionTimeout;
private int loginTimeout;
private int maintenanceInterval;
private int maxIdleTime;
private String testQuery;
private String uniqueResourceName;
}
@Configuration
@MapperScan(basePackages = "com.order.mapper", sqlSessionTemplateRef = "orderSqlSessionTemplate")
public class OrderDataSourceConfig {
//@Configuration xml orderDataSourceConfig.xml
/**
* 创建我们的DataSource
*
* @return
*/
@Bean("orderDataSource")
public DataSource orderDataSource(OrderConfig orderConfig) throws SQLException {
// 1.创建MysqlXADataSource
MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
mysqlXaDataSource.setUrl(orderConfig.getUrl());
mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
mysqlXaDataSource.setPassword(orderConfig.getPassWord());
mysqlXaDataSource.setUser(orderConfig.getUserName());
mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
// 2.将本地事务注册到创 Atomikos全局事务
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(mysqlXaDataSource);
xaDataSource.setUniqueResourceName(orderConfig.getUniqueResourceName());
xaDataSource.setMinPoolSize(orderConfig.getMinPoolSize());
xaDataSource.setMaxPoolSize(orderConfig.getMaxPoolSize());
xaDataSource.setMaxLifetime(orderConfig.getMaxLifetime());
xaDataSource.setBorrowConnectionTimeout(orderConfig.getBorrowConnectionTimeout());
xaDataSource.setLoginTimeout(orderConfig.getLoginTimeout());
xaDataSource.setMaintenanceInterval(orderConfig.getMaintenanceInterval());
xaDataSource.setMaxIdleTime(orderConfig.getMaxIdleTime());
xaDataSource.setTestQuery(orderConfig.getTestQuery());
return xaDataSource;
}
/**
* 创建我们的SqlSessionFactory
*
* @param dataSource
* @return
* @throws Exception
*/
@Bean(name = "orderSqlSessionFactory")
public SqlSessionFactory orderSqlSessionFactory(@Qualifier("orderDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean.getObject();
}
/**
* 创建订单sqlSesion模版
*
* @param sqlSessionFactory
* @return
* @throws Exception
*/
@Bean(name = "orderSqlSessionTemplate")
public SqlSessionTemplate orderSqlSessionTemplate(
@Qualifier("orderSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}