SpringBoot 11 jta+atomikos解决分布式事务
程序员文章站
2022-05-23 13:05:13
...
1:JTA
JTA(java Transaction API)是JavaEE 13 个开发规范之一。java 事务API,允许应用程序执行分布式事务处理——在两个或多个网络计算机资源*问并且更新数据。JDBC驱动程序的JTA支持极大地增强了数据访问能力。事务最简单最直接的目的就是保证数据的有效性,数据的一致性。
2:atomikos
Atomikos TransactionsEssentials是实现JTA事务管理的第三方管理工具, 是一个为Java平台提供增值服务的并且开源类事务管理器。
3:修改pom.xml文件,添加依赖
<!-- 整合jta-atomikos -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
4:修改application.yml,配置数据库连接信息
# Mysql 1
mysql:
datasource:
datasourceipp1:
url: jdbc:mysql://localhost:3306/ipp1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: root
minPoolSize: 3
maxPoolSize: 25
maxLifetime: 20000
borrowConnectionTimeout: 30
loginTimeout: 30
maintenanceInterval: 60
maxIdleTime: 60
datasourceipp2:
url: jdbc:mysql://localhost:3306/ipp2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: root
minPoolSize: 3
maxPoolSize: 25
maxLifetime: 20000
borrowConnectionTimeout: 30
loginTimeout: 30
maintenanceInterval: 60
maxIdleTime: 60
5:使用@ConfigurationProperties
读取数据库连接信息并自动封装成实体类。
package com.zhaoy.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import lombok.Data;
@Data
@ConfigurationProperties(prefix = "mysql.datasource.datasourceipp1")
public class DBConfig1 {
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;
}
package com.zhaoy.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import lombok.Data;
@Data
@ConfigurationProperties(prefix = "mysql.datasource.datasourceipp2")
public class DBConfig2 {
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;
}
5:读取数据源,配置数据库信息
package com.zhaoy.datasource;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.cj.jdbc.MysqlXADataSource;
import com.zhaoy.config.DBConfig1;
/**
* 读取数据源
* @author zhaoyu
*
*/
@Configuration
@MapperScan(basePackages = "com.zhaoy.datasourceipp1", sqlSessionFactoryRef="ipp1SqlSessionFactory")
public class DataSourceIpp1Config {
/**
* 配置数据库信息
* @return
* @throws SQLException
*/
@Bean(name = "ipp1DataSource")
public DataSource ipp1DataSource(DBConfig1 testConfig) throws SQLException {
MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
mysqlXaDataSource.setUrl(testConfig.getUrl());
mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
mysqlXaDataSource.setPassword(testConfig.getPassword());
mysqlXaDataSource.setUser(testConfig.getUsername());
mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
// 将本地事务注册到创 Atomikos全局事务
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(mysqlXaDataSource);
xaDataSource.setUniqueResourceName("ipp1DataSource");
xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());
xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());
xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());
xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());
xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());
xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());
xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());
xaDataSource.setTestQuery(testConfig.getTestQuery());
return xaDataSource;
//return DataSourceBuilder.create().build();
}
@Bean(name = "ipp1SqlSessionFactory")
public SqlSessionFactory ipp1SqlSessionFactory(@Qualifier("ipp1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "ipp1SqlSessionTemplate")
public SqlSessionTemplate ipp1SqlSessionTemplate(@Qualifier("ipp1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
package com.zhaoy.datasource;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.cj.jdbc.MysqlXADataSource;
import com.zhaoy.config.DBConfig2;
/**
* 读取数据源
* @author zhaoyu
*
*/
@Configuration
@MapperScan(basePackages = "com.zhaoy.datasourceipp2", sqlSessionFactoryRef="ipp2SqlSessionFactory")
public class DataSourceIpp2Config {
/**
* 配置数据库信息
* @return
* @throws SQLException
*/
@Bean(name = "ipp2DataSource")
public DataSource ipp2DataSource(DBConfig2 testConfig) throws SQLException {
MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
mysqlXaDataSource.setUrl(testConfig.getUrl());
mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
mysqlXaDataSource.setPassword(testConfig.getPassword());
mysqlXaDataSource.setUser(testConfig.getUsername());
mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
// 将本地事务注册到创 Atomikos全局事务
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(mysqlXaDataSource);
xaDataSource.setUniqueResourceName("ipp2DataSource");
xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());
xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());
xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());
xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());
xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());
xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());
xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());
xaDataSource.setTestQuery(testConfig.getTestQuery());
return xaDataSource;
//return DataSourceBuilder.create().build();
}
@Bean(name = "ipp2SqlSessionFactory")
public SqlSessionFactory ipp1SqlSessionFactory(@Qualifier("ipp2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "ipp2SqlSessionTemplate")
public SqlSessionTemplate ipp1SqlSessionTemplate(@Qualifier("ipp2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
6:编写service进行验证
package com.zhaoy.datasourceipp1.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zhaoy.datasourceipp1.mapper.CommentMapper1;
import com.zhaoy.datasourceipp2.mapper.CommentMapper2;
@Service
public class CommentService1 {
@Autowired
private CommentMapper1 commentMapper1;
@Autowired
private CommentMapper2 commentMapper2;
@Transactional
public int insertComment1AndComment2(String id, String productId, String commentTime, Boolean isDeleted) {
int insertComment1 = commentMapper1.insertComment(id,productId,commentTime,isDeleted);
int insertComment2 = commentMapper2.insertComment(id,productId,commentTime,isDeleted);
int sum = insertComment1 + insertComment2;
int i = 1/0;
return sum;
}
}