首先,到底啥是分布式事务呢,比如我们在执行一个业务逻辑的时候有两步分别操作A数据源和B数据源,当我们在A数据源执行数据更改后,在B数据源执行时出现运行时异常,那么我们必须要让B数据源的操作回滚,并回滚对A数据源的操作;这种情况在支付业务时常常出现;比如买票业务在最后支付失败,那之前的操作必须全部回滚,如果之前的操作分布在多个数据源中,那么这就是典型的分布式事务回滚;
了解了什么是分布式事务,那分布式事务在java的解决方案就是JTA(即Java Transaction API);springboot官方提供了 Atomikos or Bitronix的解决思路;
其实,大多数情况下很多公司是使用消息队列的方式实现分布式事务。
本篇文章重点讲解springboot环境下,整合 Atomikos +mysql+mybatis+tomcat/jetty;
一、项目依赖
pom.xml中添加atomikos的springboot相关依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jta-atomikosartifactId>
dependency>
点进去会发现里面整合好了:transactions-jms
、transactions-jta
、transactions-jdbc
、javax.transaction-api
二、把数据源的相关配置项单独提炼到一个application.yml中:
注意:
-
这回我们的
spring.datasource.type
是com.alibaba.druid.pool.xa.DruidXADataSource;
-
spring.jta.transaction-manager-id
的值在你的电脑中是唯一的,这个详细请阅读官方文档;
完整的yml文件如下:
spring:
datasource:
type:com.alibaba.druid.pool.xa.DruidXADataSource
druid:
systemDB:
name:systemDB
url:jdbc//localhost:3306/springboot-mybatis?useUnicode=true&characterEncoding=utf-8
username:root
password:root
#下面为连接池的补充设置,应用到上面所有数据源中
#初始化大小,最小,最大
initialSize:5
minIdle:5
maxActive:20
#配置获取连接等待超时的时间
maxWait:60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis:60000
#配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis:30
validationQuery:SELECT1
validationQueryTimeout:10000
testWhileIdle:true
testOnBorrow:false
testOnReturn:false
#打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements:true
maxPoolPreparedStatementPerConnectionSize:20
filters:stat,wall
#通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties:druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#合并多个DruidDataSource的监控数据
useGlobalDataSourceStat:true
businessDB:
name:businessDB
url:jdbc//localhost:3306/springboot-mybatis2?useUnicode=true&characterEncoding=utf-8
username:root
password:root
#下面为连接池的补充设置,应用到上面所有数据源中
#初始化大小,最小,最大
initialSize:5
minIdle:5
maxActive:20
#配置获取连接等待超时的时间
maxWait:60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis:60000
#配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis:30
validationQuery:SELECT1
validationQueryTimeout:10000
testWhileIdle:true
testOnBorrow:false
testOnReturn:false
#打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements:true
maxPoolPreparedStatementPerConnectionSize:20
filters:stat,wall
#通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties:druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#合并多个DruidDataSource的监控数据
useGlobalDataSourceStat:true
#jta相关参数配置
jta:
log-dir:classpath:tx-logs
transaction-manager-id:txManager
三、在DruidConfig.java中实现多个数据源的注册;分布式事务管理器的注册;druid的注册;
packagecom.zjt.config;
importcom.alibaba.druid.filter.stat.StatFilter;
importcom.alibaba.druid.support.http.StatViewServlet;
importcom.alibaba.druid.support.http.WebStatFilter;
importcom.alibaba.druid.wall.WallConfig;
importcom.alibaba.druid.wall.WallFilter;
importcom.atomikos.icatch.jta.UserTransactionImp;
importcom.atomikos.icatch.jta.UserTransactionManager;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
importorg.springframework.boot.web.servlet.FilterRegistrationBean;
importorg.springframework.boot.web.servlet.ServletRegistrationBean;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.context.annotation.Primary;
importorg.springframework.core.env.Environment;
importorg.springframework.transaction.jta.JtaTransactionManager;
importjavax.sql.DataSource;
importjavax.transaction.UserTransaction;
importjava.util.Properties;
/**
*Druid配置
*
*
*/
@Configuration
publicclassDruidConfig{
@Bean(name="systemDataSource")
@Primary
@Autowired
publicDataSourcesystemDataSource(Environmentenv){
AtomikosDataSourceBeands=newAtomikosDataSourceBean();
Propertiesprop=build(env,"spring.datasource.druid.systemDB.");
ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
ds.setUniqueResourceName("systemDB");
ds.setPoolSize(5);
ds.setXaProperties(prop);
returnds;
}
@Autowired
@Bean(name="businessDataSource")
publicAtomikosDataSourceBeanbusinessDataSource(Environmentenv){
AtomikosDataSourceBeands=newAtomikosDataSourceBean();
Propertiesprop=build(env,"spring.datasource.druid.businessDB.");
ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
ds.setUniqueResourceName("businessDB");
ds.setPoolSize(5);
ds.setXaProperties(prop);
returnds;
}
/**
*注入事物管理器
*@return
*/
@Bean(name="xatx")
publicJtaTransactionManagerregTransactionManager(){
UserTransactionManageruserTransactionManager=newUserTransactionManager();
UserTransactionuserTransaction=newUserTransactionImp();
returnnewJtaTransactionManager(userTransaction,userTransactionManager);
}
privatePropertiesbuild(Environmentenv,Stringprefix){
Propertiesprop=newProperties();
prop.put("url",env.getProperty(prefix+"url"));
prop.put("username",env.getProperty(prefix+"username"));
prop.put("password",env.getProperty(prefix+"password"));
prop.put("driverClassName",env.getProperty(prefix+"driverClassName",""));
prop.put("initialSize",env.getProperty(prefix+"initialSize",Integer.class));
prop.put("maxActive",env.getProperty(prefix+"maxActive",Integer.class));
prop.put("minIdle",env.getProperty(prefix+"minIdle",Integer.class));
prop.put("maxWait",env.getProperty(prefix+"maxWait",Integer.class));
prop.put("poolPreparedStatements",env.getProperty(prefix+"poolPreparedStatements",Boolean.class));
prop.put("maxPoolPreparedStatementPerConnectionSize",
env.getProperty(prefix+"maxPoolPreparedStatementPerConnectionSize",Integer.class));
prop.put("maxPoolPreparedStatementPerConnectionSize",
env.getProperty(prefix+"maxPoolPreparedStatementPerConnectionSize",Integer.class));
prop.put("validationQuery",env.getProperty(prefix+"validationQuery"));
prop.put("validationQueryTimeout",env.getProperty(prefix+"validationQueryTimeout",Integer.class));
prop.put("testOnBorrow",env.getProperty(prefix+"testOnBorrow",Boolean.class));
prop.put("testOnReturn",env.getProperty(prefix+"testOnReturn",Boolean.class));
prop.put("testWhileIdle",env.getProperty(prefix+"testWhileIdle",Boolean.class));
prop.put("timeBetweenEvictionRunsMillis",
env.getProperty(prefix+"timeBetweenEvictionRunsMillis",Integer.class));
prop.put("minEvictableIdleTimeMillis",env.getProperty(prefix+"minEvictableIdleTimeMillis",Integer.class));
prop.put("filters",env.getProperty(prefix+"filters"));
returnprop;
}
@Bean
publicServletRegistrationBeandruidServlet(){
ServletRegistrationBeanservletRegistrationBean=newServletRegistrationBean(newStatViewServlet(),"/druid/*");
//控制台管理用户,加入下面2行进入druid后台就需要登录
//servletRegistrationBean.addInitParameter("loginUsername","admin");
//servletRegistrationBean.addInitParameter("loginPassword","admin");
returnservletRegistrationBean;
}
@Bean
publicFilterRegistrationBeanfilterRegistrationBean(){
FilterRegistrationBeanfilterRegistrationBean=newFilterRegistrationBean();
filterRegistrationBean.setFilter(newWebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
filterRegistrationBean.addInitParameter("profileEnable","true");
returnfilterRegistrationBean;
}
@Bean
publicStatFilterstatFilter(){
StatFilterstatFilter=newStatFilter();
statFilter.setLogSlowSql(true);//slowSqlMillis用来配置SQL慢的标准,执行时间超过slowSqlMillis的就是慢。
statFilter.setMergeSql(true);//SQL合并配置
statFilter.setSlowSqlMillis(1000);//slowSqlMillis的缺省值为3000,也就是3秒。
returnstatFilter;
}
@Bean
publicWallFilterwallFilter(){
WallFilterwallFilter=newWallFilter();
//允许执行多条SQL
WallConfigconfig=newWallConfig();
config.setMultiStatementAllow(true);
wallFilter.setConfig(config);
returnwallFilter;
}
}
四、分别配置每个数据源对应的sqlSessionFactory,以及MapperScan扫描的包:
MybatisDatasourceConfig.java
packagecom.zjt.config;
importcom.zjt.util.MyMapper;
importorg.apache.ibatis.session.SqlSessionFactory;
importorg.mybatis.spring.SqlSessionFactoryBean;
importorg.mybatis.spring.SqlSessionTemplate;
importorg.mybatis.spring.annotation.MapperScan;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.core.io.support.PathMatchingResourcePatternResolver;
importorg.springframework.core.io.support.ResourcePatternResolver;
importjavax.sql.DataSource;
/**
*
*@description
*/
@Configuration
//精确到mapper目录,以便跟其他数据源隔离
@MapperScan(basePackages="com.zjt.mapper",markerInterface=MyMapper.class,sqlSessionFactoryRef="sqlSessionFactory")
publicclassMybatisDatasourceConfig{
@Autowired
@Qualifier("systemDataSource")
privateDataSourceds;
@Bean
publicSqlSessionFactorysqlSessionFactory()throwsException{
SqlSessionFactoryBeanfactoryBean=newSqlSessionFactoryBean();
factoryBean.setDataSource(ds);
//指定mapperxml目录
ResourcePatternResolverresolver=newPathMatchingResourcePatternResolver();
factoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
returnfactoryBean.getObject();
}
@Bean
publicSqlSessionTemplatesqlSessionTemplate()throwsException{
SqlSessionTemplatetemplate=newSqlSessionTemplate(sqlSessionFactory());//使用上面配置的Factory
returntemplate;
}
//关于事务管理器,不管是JPA还是JDBC等都实现自接口PlatformTransactionManager
//如果你添加的是 spring-boot-starter-jdbc 依赖,框架会默认注入 DataSourceTransactionManager 实例。
//在Spring容器中,我们手工注解@Bean 将被优先加载,框架不会重新实例化其他的 PlatformTransactionManager 实现类。
/*@Bean(name="transactionManager")
@Primary
publicDataSourceTransactionManagermasterTransactionManager(){
//MyBatis自动参与到spring事务管理中,无需额外配置,只要org.mybatis.spring.SqlSessionFactoryBean引用的数据源
//与DataSourceTransactionManager引用的数据源一致即可,否则事务管理会不起作用。
returnnewDataSourceTransactionManager(ds);
}*/
}
MybatisDatasource2Config.java
packagecom.zjt.config;
importcom.zjt.util.MyMapper;
importorg.apache.ibatis.session.SqlSessionFactory;
importorg.mybatis.spring.SqlSessionFactoryBean;
importorg.mybatis.spring.SqlSessionTemplate;
importorg.mybatis.spring.annotation.MapperScan;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.core.io.support.PathMatchingResourcePatternResolver;
importorg.springframework.core.io.support.ResourcePatternResolver;
importjavax.sql.DataSource;
/**
*
*@description
*/
@Configuration
//精确到mapper目录,以便跟其他数据源隔离
@MapperScan(basePackages="com.zjt.mapper2",markerInterface=MyMapper.class,sqlSessionFactoryRef="sqlSessionFactory2")
publicclassMybatisDatasource2Config{
@Autowired
@Qualifier("businessDataSource")
privateDataSourceds;
@Bean
publicSqlSessionFactorysqlSessionFactory2()throwsException{
SqlSessionFactoryBeanfactoryBean=newSqlSessionFactoryBean();
factoryBean.setDataSource(ds);
//指定mapperxml目录
ResourcePatternResolverresolver=newPathMatchingResourcePatternResolver();
factoryBean.setMapperLocations(resolver.getResources("classpath:mapper2/*.xml"));
returnfactoryBean.getObject();
}
@Bean
publicSqlSessionTemplatesqlSessionTemplate2()throwsException{
SqlSessionTemplatetemplate=newSqlSessionTemplate(sqlSessionFactory2());//使用上面配置的Factory
returntemplate;
}
//关于事务管理器,不管是JPA还是JDBC等都实现自接口PlatformTransactionManager
//如果你添加的是 spring-boot-starter-jdbc 依赖,框架会默认注入 DataSourceTransactionManager 实例。
//在Spring容器中,我们手工注解@Bean 将被优先加载,框架不会重新实例化其他的 PlatformTransactionManager 实现类。
/*@Bean(name="transactionManager2")
@Primary
publicDataSourceTransactionManagermasterTransactionManager(){
//MyBatis自动参与到spring事务管理中,无需额外配置,只要org.mybatis.spring.SqlSessionFactoryBean引用的数据源
//与DataSourceTransactionManager引用的数据源一致即可,否则事务管理会不起作用。
returnnewDataSourceTransactionManager(ds);
}*/
}
由于我们本例中只使用一个事务管理器:xatx,故就不在使用TxAdviceInterceptor.java
和TxAdvice2Interceptor.java
中配置的事务管理器了;有需求的童鞋可以自己配置其他的事务管理器;(见DruidConfig.java中查看)
五、新建分布式业务测试接口JtaTestService.java和实现类JtaTestServiceImpl.java
其实就是一个很简单的test01()方法,在该方法中我们分别先后调用classService.saveOrUpdateTClass(tClass);
和teacherService.saveOrUpdateTeacher(teacher);
实现先后操作两个数据源:然后我们可以自己debug跟踪事务的提交时机,此外,也可以在在两个方法全执行结束之后,手动制造一个运行时异常,来检查分布式事务是否全部回滚;
注意:
在实现类的方法中我使用的是:
@Transactional(transactionManager="xatx",propagation=Propagation.REQUIRED,rollbackFor={java.lang.RuntimeException.class})
从而指定了使用哪个事务管理器,事务隔离级别(一般都用我这个默认的),回滚的条件(一般可以使用Exception),这三个可以自己根据业务实际修改;
packagecom.zjt.service3;
importjava.util.Map;
publicinterfaceJtaTestService{
publicMaptest01() ;
}
packagecom.zjt.service3.impl;
importcom.zjt.entity.TClass;
importcom.zjt.entity.Teacher;
importcom.zjt.service.TClassService;
importcom.zjt.service2.TeacherService;
importcom.zjt.service3.JtaTestService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.stereotype.Service;
importorg.springframework.transaction.annotation.Propagation;
importorg.springframework.transaction.annotation.Transactional;
importjava.util.LinkedHashMap;
importjava.util.Map;
@Service("jtaTestServiceImpl")
publicclassJtaTestServiceImplimplementsJtaTestService{
@Autowired
@Qualifier("teacherServiceImpl")
privateTeacherServiceteacherService;
@Autowired
@Qualifier("tclassServiceImpl")
privateTClassServicetclassService;
@Override
@Transactional(transactionManager="xatx",propagation=Propagation.REQUIRED,rollbackFor={java.lang.RuntimeException.class})
publicMap<String,Object>test01(){
LinkedHashMapresultMap=newLinkedHashMap();
TClasstClass=newTClass();
tClass.setName("8888");
tclassService.saveOrUpdateTClass(tClass);
Teacherteacher=newTeacher();
teacher.setName("8888");
teacherService.saveOrUpdateTeacher(teacher);
System.out.println(1/0);
resultMap.put("state","success");
resultMap.put("message","分布式事务同步成功");
returnresultMap;
}
}
六、建立JtaTestContoller.java,接受一个来自前端的http请求,触发JtaTestService 的test01方法:
packagecom.zjt.web;
importcom.zjt.service3.JtaTestService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.ResponseBody;
importjava.util.LinkedHashMap;
importjava.util.Map;
@Controller
@RequestMapping("/jtaTest")
publicclassJtaTestContoller{
@Autowired
@Qualifier("jtaTestServiceImpl")
privateJtaTestServicetaTestService;
@ResponseBody
@RequestMapping("/test01")
publicMaptest01() {
LinkedHashMapresultMap=newLinkedHashMap();
try{
returntaTestService.test01();
}catch(Exceptione){
resultMap.put("state","fail");
resultMap.put("message","分布式事务同步失败");
returnresultMap;
}
}
}
七、在test.ftl中增加一个按钮来测试;
//分布式事务测试
$("#JTATest").click(function(){
$.ajax({
type: "POST",
url: "${basePath!}/jtaTest/test01",
data: {} ,
async: false,
error: function (request) {
layer.alert("与服务器连接失败/(ㄒoㄒ)/~~");
return false;
},
success: function (data) {
if (data.state == 'fail') {
layer.alert(data.message);
return false;
}else if(data.state == 'success'){
layer.alert(data.message);
}
}
});
});
八、启动服务,验证结果:
点击这个按钮,跳转到controller:
当正常执行了sql语句之后,我们可以发现数据库并没有变化,因为整个方法的事务还没有走完,当我们走到1/0这步时:
抛出运行时异常,并被spring事务拦截器拦截,并捕获异常:
在this.completeTransactionAfterThrowing(txInfo, var16);
方法中会将事务全部回滚:
2204.243logback[http-nio-8080-exec-5]INFOc.a.i.imp.CompositeTransactionImp-rollback()doneoftransaction192.168.1.103.tm0000400006
此时,当我们再次打开数据库验证,依旧没有变化,证明分布式事务配置成功;
大家可以基于我的代码自己练习一下,自己尝试着使用多事务管理器的情况下的灵活配置;
九、后记:
本文源代码:
https://github.com/zhaojiatao/springboot-zjt-chapter10-springboot-atomikos-mysql-mybatis-druid.git
代码在tomcat和jetty环境下均可完成事务回滚;
在事务回滚时可能报一个Transactional not active
的警告,我google后,老外也说不出这个具体作用,大部分人认为这只是一个警告,可以忽略;
-End-
-
API
+关注
关注
2文章
1483浏览量
61797 -
分布式
+关注
关注
1文章
878浏览量
74459 -
数据源
+关注
关注
1文章
62浏览量
9661 -
SpringBoot
+关注
关注
0文章
173浏览量
167
原文标题:SpringBoot 分布式事务的解决方案(JTA+Atomic+多数据源)
文章出处:【微信号:AndroidPush,微信公众号:Android编程精选】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
评论