背景:项目中使用了两个mysql数据源,有一个功能是同时修改两个库里的表数据,需要进行事务控制。项目框架为springcloud+mybatis。
项目结构如下
增加maven依赖
<!--分布式事务支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
<!--lombok jar 自动get set-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
application.properties
#carinfo
spring.datasource.carinfo.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.carinfo.driverClassName=com.mysql.jdbc.Driver
spring.datasource.carinfo.url=xxx
spring.datasource.carinfo.username=xxx
spring.datasource.carinfo.password=xxx
spring.datasource.carinfo.maxActive=200
spring.datasource.carinfo.minIdle=2
spring.datasource.carinfo.initialSize=5
spring.datasource.carinfo.maxWait=60000
spring.datasource.carinfo.timeBetweenEvictionRunsMillis=60000
spring.datasource.carinfo.minEvictableIdleTimeMillis=300000
spring.datasource.carinfo.validationQuery=SELECT 1 FROM DUAL
spring.datasource.carinfo.testWhileIdle=true
spring.datasource.carinfo.testOnBorrow=false
spring.datasource.carinfo.testOnReturn=false
spring.datasource.carinfo.poolPreparedStatements=true
spring.datasource.carinfo.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.carinfo.filters=stat
spring.datasource.carinfo.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#emscartype
spring.datasource.emscartype.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.emscartype.driverClassName=com.mysql.jdbc.Driver
spring.datasource.emscartype.url=xx
spring.datasource.emscartype.username=xx
spring.datasource.emscartype.password=xx
spring.datasource.emscartype.maxActive=200
spring.datasource.emscartype.minIdle=2
spring.datasource.emscartype.initialSize=5
spring.datasource.emscartype.maxWait=60000
spring.datasource.emscartype.timeBetweenEvictionRunsMillis=60000
spring.datasource.emscartype.minEvictableIdleTimeMillis=300000
spring.datasource.emscartype.validationQuery=SELECT 1 FROM DUAL
spring.datasource.emscartype.testWhileIdle=true
spring.datasource.emscartype.testOnBorrow=false
spring.datasour