本文主要是介绍Transation-多数据源下解决同一个方法多数个事务问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
在最近的工作中,公司要把以前php写的代码先一部分转为java,里面在一个方法里面需要对多个数据源进行事务的管理。在这篇文章里面我会介绍一些自己在解决这个问题时的一些思路和我的解决办法。
问题引入:
由于是定时任务,需要在同一个方法里面对多个数据源进行操作,但是因为是不同数据源因此使用事务的会发现会报错。
public int addWithTransation() {int currentTimeMills = (int) Instant.now().getEpochSecond();CreditRecord creditRecordA = new CreditRecord();creditRecordA.setId(3L);creditRecordA.setBeforeAmount(100);creditRecordA.setChangeAmount(50);creditRecordA.setAfterAmount(150);creditRecordA.setCreateTime(currentTimeMills);creditRecordA.setUpdateTime(currentTimeMills);CreditRecord creditRecordB = new CreditRecord();creditRecordB.setId(3L);creditRecordB.setBeforeAmount(0);creditRecordB.setChangeAmount(-50);creditRecordB.setAfterAmount(-50);creditRecordB.setCreateTime(currentTimeMills);creditRecordB.setUpdateTime(currentTimeMills);this.baseMapper.insert(creditRecordA);creditRecordDB2Service.add(creditRecordB);if(true){throw new RuntimeException("抛出异常");}return 1;}
这是个简单的例子,在creditRecordDB1Service里面使用另一个数据源的creditRecordDB2Service去add的时候,一旦添加了事务之后,发现只有creditRecordDB1Service的能够回滚,creditRecordDB2Service的add方法是不能回滚的,且creditRecordDB2Service连接的是creditRecordDB1Service的数据源。
为了弄清原理,我后面看了一下源码。为什么加了事务之后,DataSource仍然是第一个Datasource.
从@Transation的注解里面并没有介绍从哪被拦截,但其实这与spring的aop有关。想要知道具体内容可参考这位大佬的文章,过程链条比较长,我仍在理清当中。
通过aop的注册,找到TransactionAttributeSourcePointcut的代理类的matcher方法。
Spring源码解析之事务篇
去找到方法是否有@Transation的注解。
获取到目标bean匹配的增强器之后,会为bean创建代理,在执行代理类的目标方法时,会调用Advisor的getAdvice获取MethodInterceptor并执行其invoke方法,而BeanFactoryTransactionAttributeSourceAdvisor的getAdvice方法会返回我们在文章开始看到的为其织入的另外一个bean,也就是TransactionInterceptor,它实现了MethodInterceptor。
在经过TransactionInterceptor后,经由AbstractPlatformTransactionManager来操作dataSource.在其继承类DataSourceTransactionManager中的doBegin方法里面,我们可以看到:
在与数据库获取连接的时候通过obtainDataSource().getConnection()拿到了DataSource来创建数据库连接。
而DataSource在代理的时候被初始化,设置了DataSource.
从获取数据库的连接起,在这事务中只用一个数据源。
好,为了解决使用事务的时候,在与数据库连接时就定好了datasource不能改变的问题。我们通过配置指定datasource给指定的mapper就能解决这个问题了。
@Configuration
@MapperScan(basePackages = "com.example.multisource.dao.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
//通过将指定的mapper指定给特定sqlsessionFactory,解决事务下datasource不能变更问题
public class Db1Config {@Bean(name = "db1")@ConfigurationProperties(prefix = "spring.datasource.druid.db1" )public DataSource db1() {return DruidDataSourceBuilder.create().build();}// 事务控制器@Bean(name = "db1TransactionManager")@Primarypublic DataSourceTransactionManager dp1TransactionManager() {return new DataSourceTransactionManager(db1());}@Bean(name = "db1SqlSessionFactory")@Primarypublic SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1") DataSource db1)throws Exception {final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();sessionFactory.setDataSource(db1);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/db1/*.xml"));/*设置sql控制台打印*/com.baomidou.mybatisplus.core.MybatisConfiguration configuration = new com.baomidou.mybatisplus.core.MybatisConfiguration();configuration.setLogImpl(StdOutImpl.class);sessionFactory.setConfiguration(configuration);return sessionFactory.getObject();}
}
@Configuration
@MapperScan(basePackages = "com.example.multisource.dao.db2", sqlSessionFactoryRef = "db2SqlSessionFactory")
public class Db2Config {@Bean(name = "db2")@ConfigurationProperties(prefix = "spring.datasource.druid.db2" )public DataSource db2() {return DruidDataSourceBuilder.create().build();}@Bean(name = "db2TransactionManager")@Primarypublic DataSourceTransactionManager db2TransactionManager() {return new DataSourceTransactionManager(db2());}@Bean(name = "db2SqlSessionFactory")@Primarypublic SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2") DataSource db2)throws Exception {final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();sessionFactory.setDataSource(db2);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/db2/*.xml"));/*设置sql控制台打印*/com.baomidou.mybatisplus.core.MybatisConfiguration configuration = new com.baomidou.mybatisplus.core.MybatisConfiguration();configuration.setLogImpl(StdOutImpl.class);sessionFactory.setConfiguration(configuration);return sessionFactory.getObject();}
}
解决报错时回滚
网上看到了许多demo,但是大多有缺失或者说话只说一半。
我大体的思路就是@Transation这个注解不能duplicate,那我就自己写一个自定义注解,来启动两个transationmanager来启动两个事务。
自定义注解(比较粗糙,如果有人想用的话得需要自己改一下,可以传入数组来for循环启动事务)
/*** @ClassName DataSource* @Author kris* @Date 2020/5/29**/
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MultiTm {
}
AOP
@Component
@Slf4j
@Aspect
@Order(-1)
public class TsetAspect {@Pointcut("@within(com.example.multisource.annonation.MultiTm) || @annotation(com.example.multisource.annonation.MultiTm)")public void TsetAspect(){}@Around(value = "TsetAspect()")public Object transactionalGroupAspectArround(ProceedingJoinPoint pjp) throws Throwable{
// db1Manager.setDataSource((DataSource) SpringContextUtil.getBean("db1"));
// db2Manager.setDataSource((DataSource) SpringContextUtil.getBean("db2"));DataSourceTransactionManager db1Manager = (DataSourceTransactionManager) SpringContextUtil.getBean("db1TransactionManager");TransactionStatus transactionDB1Status = db1Manager.getTransaction(new DefaultTransactionDefinition());DataSourceTransactionManager db2Manager = (DataSourceTransactionManager) SpringContextUtil.getBean("db2TransactionManager");TransactionStatus transactionDB2Status = db2Manager.getTransaction(new DefaultTransactionDefinition());try{Object obj = pjp.proceed();db2Manager.commit(transactionDB2Status);
//这点很重要,不这样写会transation is not active。因为是db1Manager先启动,db2Manager再启动,因此db2Manager其实在包在了db1Manager里面的,所以commit或者rollback的时候要先commit或rollback db2Manager。符合后进先出原则,可以Stack来优化。db1Manager.commit(transactionDB1Status);return obj;}catch(Exception e){log.info(e.getMessage());db2Manager.rollback(transactionDB2Status);db1Manager.rollback(transactionDB1Status);return null;}}
}
这点很重要,不这样写会transation is not active。因为是db1Manager先启动,db2Manager再启动,因此db2Manager其实在包在了db1Manager里面的,所以commit或者rollback的时候要先commit或rollback db2Manager。符合后进先出原则,可以Stack来优化。
根据上面做点优化:
@Component
@Slf4j
@Aspect
@Order(-1)
public class TsetAspect {@Pointcut("@within(com.example.multisource.annonation.MultiTm) || @annotation(com.example.multisource.annonation.MultiTm)")public void TsetAspect(){}@Around(value = "TsetAspect() && @annotation(multiTm)")public Object transactionalGroupAspectArround(ProceedingJoinPoint pjp, MultiTm multiTm) throws Throwable{Stack<DataSourceTransactionManager> dataSourceTransactionManagerStack = new Stack<>();Stack<TransactionStatus> transactionStatusStack = new Stack<>();if (multiTm.transactionManagers().length<1){log.info("[开启事务失败]:无指定多数据源管理器");return null;}for(String transationMangaeName: multiTm.transactionManagers()){DataSourceTransactionManager dbManager = (DataSourceTransactionManager) SpringContextUtil.getBean(transationMangaeName);TransactionStatus transactionDBStatus = dbManager.getTransaction(new DefaultTransactionDefinition());dataSourceTransactionManagerStack.push(dbManager);transactionStatusStack.push(transactionDBStatus);}try{Object obj = pjp.proceed();while(!dataSourceTransactionManagerStack.isEmpty()){dataSourceTransactionManagerStack.pop().commit(transactionStatusStack.pop());}return obj;}catch(Exception e){log.info(e.getMessage());while(!dataSourceTransactionManagerStack.isEmpty()){dataSourceTransactionManagerStack.pop().rollback(transactionStatusStack.pop());}return null;}}
}
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MultiTm {String[] transactionManagers() default {};
}
@Override@MultiTm(transactionManagers={"db1TransactionManager","db2TransactionManager"})public int addWithTransation() {int currentTimeMills = (int) Instant.now().getEpochSecond();CreditRecord creditRecordA = new CreditRecord();creditRecordA.setId(3L);creditRecordA.setBeforeAmount(100);creditRecordA.setChangeAmount(50);creditRecordA.setAfterAmount(150);creditRecordA.setCreateTime(currentTimeMills);creditRecordA.setUpdateTime(currentTimeMills);CreditRecord creditRecordB = new CreditRecord();creditRecordB.setId(3L);creditRecordB.setBeforeAmount(0);creditRecordB.setChangeAmount(-50);creditRecordB.setAfterAmount(-50);creditRecordB.setCreateTime(currentTimeMills);creditRecordB.setUpdateTime(currentTimeMills);this.baseMapper.insert(creditRecordA);creditRecordDB2Service.add(creditRecordB);if(true){throw new RuntimeException("抛出异常");}return 1;}
实际运行方法
@MultiTm
// @Transactional("db1TransactionManager")
// @Transactional("db2TransactionManager")public int addWithTransation() {int currentTimeMills = (int) Instant.now().getEpochSecond();CreditRecord creditRecordA = new CreditRecord();creditRecordA.setId(3L);creditRecordA.setBeforeAmount(100);creditRecordA.setChangeAmount(50);creditRecordA.setAfterAmount(150);creditRecordA.setCreateTime(currentTimeMills);creditRecordA.setUpdateTime(currentTimeMills);CreditRecord creditRecordB = new CreditRecord();creditRecordB.setId(3L);creditRecordB.setBeforeAmount(0);creditRecordB.setChangeAmount(-50);creditRecordB.setAfterAmount(-50);creditRecordB.setCreateTime(currentTimeMills);creditRecordB.setUpdateTime(currentTimeMills);this.baseMapper.insert(creditRecordA);creditRecordDB2Service.add(creditRecordB);if(true){throw new RuntimeException("抛出异常");}return 1;}
Github demo连接-各位同学也可以已此做个参考,有问题也可以和我联系,欢迎大家指导一下。看demo里面的controller下的mixAddWithTransation方法即可
Github demo
参考链接
1.Spring Boot多数据源事务管理
2.Spring Boot 中使用 @Transactional 注解配置事务管理
这篇关于Transation-多数据源下解决同一个方法多数个事务问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!