很久以前写过一片文章:SpringBoot+Mybatis-多数据源动态切换+动态加载,主要描述实现动态切换数据源,用到某个数据库的时候需要自己手动写代码切换, 但是最近公司业务需要,需要实现多个数据库的分布式事务,所以就有了本篇文章;注意流程:
(1) 在配置文件中配置数据库连接信息以及对应实体类.
(2) 配置多个数据库连接(数据源), 核心配置是注解@MapperScan(basePackages = {"com.mybatis.jta.demo.dao.car_impl*"}, sqlSessionTemplateRef = "sqlSessionTemplateCar")
, 扫描自己数据库的dao接口, 使用自己的sqlSessionTemplate
操作数据库,这样我们在写代码的时候就不需要手动切换数据库了.
(3) 配置事务管理器 .(协调多个数据源对事务进行提交或回滚)
(4) 编写代码获取spring容器中bean(连接数据源bean)
(5) 修改数据源bean中的数据库连接url等连接信息(动态切换数据源)
一.准备
java使用的是JTA方式实现的分布式事务, 如果想搞清楚原理的朋友请查看我的另外一篇文章: JTA分布式事务
-
版本:重点是
Mybatis
和druid
版本需要如下指定版本,因为Mybatis
新版实现了jdbc4.1+
的功能,而Druid
的并没有实现,只是抛出了SQLFeatureNotSupportedException
异常;jdk:1.8 mybatis-spring-boot-starter:1.3.4 druid-spring-boot-starter: 1.1.14 mysql-connector-java: 8.0.11 spring-boot-starter-parent: 2.1.8.RELEASE
-
三个数据库准备 db_test, db_user, ljyun_512_merchant, 存储sql语句的地址
1. db_car 数据库有表 tb_message_package_no 2. db_test 数据库有表 tb_user, tb_role 3. ljyun_512_merchant 商户私有库
-
pom.xml新增配置
com.alibaba druid 1.1.14 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.4 org.springframework.boot spring-boot-starter-jta-atomikos
二.application.properties中数据库配置和对应连接属性实体配置
- application.properties中数据库配置
# 数据库配置 car spring.datasource.car.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.car.driverClassName=com.mysql.cj.jdbc.Driver #spring.datasource.car.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.car.url=jdbc:mysql://localhost:3306/db_car?useUnicode=true&characterEncoding=utf8&useSSL\ =false spring.datasource.car.username=root spring.datasource.car.password=123456 spring.datasource.car.initialSize=5 spring.datasource.car.minIdle=5 spring.datasource.car.maxActive=20 spring.datasource.car.maxWait=60000 spring.datasource.car.timeBetweenEvictionRunsMillis=60000 spring.datasource.car.minEvictableIdleTimeMillis=300000 spring.datasource.car.validationQuery=SELECT 1 FROM DUAL spring.datasource.car.testWhileIdle=true spring.datasource.car.testOnBorrow=false spring.datasource.car.testOnReturn=false spring.datasource.car.poolPreparedStatements=true spring.datasource.car.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.car.filters=stat,wall,log4j spring.datasource.car.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 数据库配置 test spring.datasource.test.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.test.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.test.url=jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.test.username=root spring.datasource.test.password=123456 spring.datasource.test.initialSize=5 spring.datasource.test.minIdle=5 spring.datasource.test.maxActive=20 spring.datasource.test.maxWait=60000 spring.datasource.test.timeBetweenEvictionRunsMillis=60000 spring.datasource.test.minEvictableIdleTimeMillis=300000 spring.datasource.test.validationQuery=SELECT 1 FROM DUAL spring.datasource.test.testWhileIdle=true spring.datasource.test.testOnBorrow=false spring.datasource.test.testOnReturn=false spring.datasource.test.poolPreparedStatements=true spring.datasource.test.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.test.filters=stat,wall,log4j spring.datasource.test.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 动态创建的ljyun数据库 spring.datasource.yun.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.yun.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.yun.url=jdbc:mysql://localhost:3306/ljyun_512_merchant?useUnicode=true&characterEncoding=utf8&useSSL\ =false spring.datasource.yun.username=root spring.datasource.yun.password=123456 spring.datasource.yun.initialSize=5 spring.datasource.yun.minIdle=5 spring.datasource.yun.maxActive=20 spring.datasource.yun.maxWait=60000 spring.datasource.yun.timeBetweenEvictionRunsMillis=60000 spring.datasource.yun.minEvictableIdleTimeMillis=300000 spring.datasource.yun.validationQuery=SELECT 1 FROM DUAL spring.datasource.yun.testWhileIdle=true spring.datasource.yun.testOnBorrow=false spring.datasource.yun.testOnReturn=false spring.datasource.yun.poolPreparedStatements=true spring.datasource.yun.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.yun.filters=stat,wall,log4j spring.datasource.yun.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
- db_car对应数据库配置实体代码位置
- db_test对应数据库配置实体代码位置
- ljyun_512_merchant对应数据库配置实体代码位置
- 其实文件内容都是一样的,只是文件名不同而已;
三.数据源配置(很重要的一个环节)
- db_car数据源配置
DataSourceCarConfig.java
package com.mybatis.jta.demo.config; import com.alibaba.druid.pool.xa.DruidXADataSource; import com.atomikos.jdbc.AtomikosDataSourceBean; 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.BeanUtils; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; /** * db_car数据源配置 * Create by liangxifeng on 19-9-27 */ @Configuration // 使用注解的方式使用 //@MapperScan(basePackages = {"com.mybatis.jta.demo.mapper.car*"}, sqlSessionTemplateRef = "sqlSessionTemplateCar") @MapperScan(basePackages = {"com.mybatis.jta.demo.dao.car_impl*"}, sqlSessionTemplateRef = "sqlSessionTemplateCar") // // 扫描dao或mapper接口 public class DataSourceCarConfig { //只能在三个数据源配置信息中出现一次,表示告诉spring初始化时候优先加载哪个数据源 @Primary @Bean("dataSourceCarXA") public DruidXADataSource dataSourceXA(DataSourceCarProperties dataSourceCarProperties) { DruidXADataSource dataSource = new DruidXADataSource(); // 使用BeanUtils将数据库连接属性映射到数据源DruidXADataSource的属性中 //当然也可以通过dataSource.setUrl()的方式配置c BeanUtils.copyProperties(dataSourceCarProperties,dataSource); return dataSource; } @Bean(name = "dataSourceCar") public DataSource dataSourceCar(@Qualifier("dataSourceCarXA") DruidXADataSource dataSource){ AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean(); xaDataSource.setXaDataSource(dataSource); xaDataSource.setUniqueResourceName("dataSourceCar"); return xaDataSource; } @Bean(name = "sqlSessionFactoryCar") public SqlSessionFactory sqlSessionFactoryCar(@Qualifier("dataSourceCar") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage("com.mybatis.jta.demo.entity.car"); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/car/*Mapper.xml")); return bean.getObject(); } @Bean(name = "sqlSessionTemplateCar") public SqlSessionTemplate sqlSessionTemplateCar( @Qualifier("sqlSessionFactoryCar") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
- db_test数据源配置
DataSourceTestConfig.java
package com.mybatis.jta.demo.config; import com.alibaba.druid.pool.xa.DruidXADataSource; import com.atomikos.jdbc.AtomikosDataSourceBean; 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.BeanUtils; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; /** * Description: db_test数据源配置 * Create by liangxifeng on 19-9-27 */ @Configuration // 扫描dao或mapper接口 //@MapperScan(basePackages = {"com.mybatis.jta.demo.mapper.test*"}, sqlSessionTemplateRef = "sqlSessionTemplateTest") @MapperScan(basePackages = {"com.mybatis.jta.demo.dao.test_impl*"}, sqlSessionTemplateRef = "sqlSessionTemplateTest") // public class DataSourceTestConfig { @Bean("dataSourceTestXA") public DruidXADataSource dataSourceXA(DataSourceTestProperties dataSourceTestProperties) { DruidXADataSource dataSource = new DruidXADataSource(); BeanUtils.copyProperties(dataSourceTestProperties,dataSource); return dataSource; } @Bean(name = "dataSourceTest") public DataSource dataSourceTest(@Qualifier("dataSourceTestXA") DruidXADataSource dataSource){ AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean(); xaDataSource.setXaDataSource(dataSource); xaDataSource.setUniqueResourceName("dataSourceTest"); return xaDataSource; } @Bean(name = "sqlSessionFactoryTest") public SqlSessionFactory sqlSessionFactoryTest(@Qualifier("dataSourceTest") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage("com.mybatis.jta.demo.entity.test"); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/test/*Mapper.xml")); return bean.getObject(); } @Bean(name = "sqlSessionTemplateTest") public SqlSessionTemplate sqlSessionTemplateTest( @Qualifier("sqlSessionFactoryTest") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
- ljyun_512_merchnat对应数据源配置
DataSourceYunConfig.java
, 该数据源配置暂时连接的是ljyun_512_merchant
数据库,下面会介绍通过参数动态修改该数据源的数据库连接信息,也就意味着切换数据库了.package com.mybatis.jta.demo.config; import com.alibaba.druid.pool.xa.DruidXADataSource; import com.atomikos.jdbc.AtomikosDataSourceBean; 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.BeanUtils; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; /** * ljyun私有库数据源配置 * Create by liangxifeng on 19-9-27 */ @Configuration // 使用注解的方式使用 //@MapperScan(basePackages = {"com.mybatis.jta.demo.mapper.car*"}, sqlSessionTemplateRef = "sqlSessionTemplateCar") @MapperScan(basePackages = {"com.mybatis.jta.demo.dao.yun_impl*"}, sqlSessionTemplateRef = "sqlSessionTemplateYun") // // 扫描dao或mapper接口 public class DataSourceYunConfig { @Bean("dataSourceYunXA") public DruidXADataSource dataSourceXA(DataSourceYunProperties dataSourceYunProperties) { DruidXADataSource dataSource = new DruidXADataSource(); BeanUtils.copyProperties(dataSourceYunProperties,dataSource); return dataSource; } @Bean(name = "dataSourceYun") public DataSource dataSourceYun(@Qualifier("dataSourceYunXA") DruidXADataSource dataSource){ AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean(); xaDataSource.setXaDataSource(dataSource); xaDataSource.setUniqueResourceName("dataSourceYun"); return xaDataSource; } @Bean(name = "sqlSessionFactoryYun") public SqlSessionFactory sqlSessionFactoryYun(@Qualifier("dataSourceYun") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage("com.mybatis.jta.demo.entity.yun"); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources ("classpath:/mapper/yun/*Mapper.xml")); return bean.getObject(); } @Bean(name = "sqlSessionTemplateYun") public SqlSessionTemplate sqlSessionTemplateYun( @Qualifier("sqlSessionFactoryYun") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
- XA事务管理器配置类
XATransactionManagerConfig.java
package com.mybatis.jta.demo.config; import com.atomikos.icatch.jta.UserTransactionImp; import com.atomikos.icatch.jta.UserTransactionManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.jta.JtaTransactionManager; import javax.transaction.TransactionManager; import javax.transaction.UserTransaction; /** * XA事务管理器配置类 * Create by liangxifeng on 19-9-27 */ @Configuration @EnableTransactionManagement public class XATransactionManagerConfig { @Bean(name = "userTransaction") public UserTransaction userTransaction() throws Throwable { UserTransactionImp userTransactionImp = new UserTransactionImp(); //userTransactionImp.setTransactionTimeout(10000); return userTransactionImp; } @Bean(name = "atomikosTransactionManager", initMethod="init", destroyMethod = "close") //@Bean(name = "atomikosTransactionManager") public TransactionManager atomikosTransactionManager() throws Throwable { UserTransactionManager userTransactionManager = new UserTransactionManager(); userTransactionManager.setForceShutdown(false); return userTransactionManager; } @Bean(name = "transactionManager") @DependsOn({ "userTransaction", "atomikosTransactionManager" }) public PlatformTransactionManager transactionManager() throws Throwable { return new JtaTransactionManager(userTransaction(),atomikosTransactionManager()); } }
- 以上配置是基于mybatis配置文件的方式,其实xml和注解方式配置区别在于数据源配置文件中的
@MapperScan
, 注解的方式扫描的是有注解的mapper
接口目录,xml方式扫描的是dao
层接口目录.xml的方式在sqlSessionFactory中有要配置setMapperLocations(xml配置文件路径)
.- 基于注解方式配置如下:
@MapperScan(basePackages = {"com.mybatis.jta.demo.mapper.car*"}, sqlSessionTemplateRef = "sqlSessionTemplateCar") @Bean(name = "sqlSessionFactoryCar") public SqlSessionFactory sqlSessionFactoryCar(@Qualifier("dataSourceCar") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage("com.mybatis.jta.demo.entity.car"); return bean.getObject(); }
- 基于xml的配置:
@MapperScan(basePackages = {"com.mybatis.jta.demo.dao.car_impl*"}, sqlSessionTemplateRef = "sqlSessionTemplateCar") @Bean(name = "sqlSessionFactoryCar") public SqlSessionFactory sqlSessionFactoryCar(@Qualifier("dataSourceCar") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage("com.mybatis.jta.demo.entity.car"); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/car/*Mapper.xml")); return bean.getObject(); }
- 到此除了dao,service等业务代码编写外,核心配置已经完成.业务代码以及单元测试代码请查看我的代码库.单元测试请查看:
TestIndexService.testMessageUser()
方法,单元测试新增db_car.MessagePackageNo
和db_test.User
表数据,固定数据源
;测试代码位置 - 但是我们公司内部,需要通过外部参数来连接不同的数据库,也就是本文章中的
ljyun_512_merchant
有N个数据库,比如:ljyun_1_merchant,ljyun_2_merchant,ljyun_3_merchant,....
,这就需要我们动态连接数据库.我的实现方式是通过外部参数动态修改spring容器中的ljyun_512_merchant数据源
的url,username,password
等属性.接下来就是动态修改数据源bean属性方式实现动态切换数据源.
四.两个工具类配置
-
自定义获取从容器中获取bean的工具类
SpringContextUtil
package com.mybatis.jta.demo.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * 自定义获取从容器中获取bean的工具类 * liangxifeng 2019-10-13 */ @Component public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil.applicationContext = applicationContext; } //获取applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } //通过name获取 Bean. public static Object getBean(String name) { return getApplicationContext().getBean(name); } //通过class获取Bean. public static
T getBean(Class clazz) { return getApplicationContext().getBean(clazz); } //通过name,以及Clazz返回指定的Bean public static T getBean(String name, Class clazz) { return getApplicationContext().getBean(name, clazz); } } -
动态修改spring容器数据库连接池bean的url属性类
SwitchDB.java
package com.mybatis.jta.demo.util; import com.alibaba.druid.pool.xa.DruidXADataSource; import lombok.extern.slf4j.Slf4j; /** * 动态修改数据库连接池的url属性类 * Create by liangxifeng on 19-10-13 */ @Slf4j public class SwitchDB { /** * 修改spring容器中私有库数据源url信息 * @param ljyunId 私有库云编号 */ public static void to(int ljyunId) { //从spring容器中获取数据源 DruidXADataSource dataSource = (DruidXADataSource) SpringContextUtil.getBean("dataSourceYunXA"); try { // 这里需要先关闭数据源,才可以使新修改的数据源设置生效 dataSource.close(); } catch (Exception e) { log.info("关闭数据源失败,连接url={}",dataSource.getUrl()); e.printStackTrace(); } String preUrl = "jdbc:mysql://localhost:3306/"; String postUrl = "useUnicode=true&characterEncoding=utf8&useSSL=false"; log.info("切换数据库ljyunId="+ljyunId); //修改数据源的连接url属性 dataSource.setUrl(preUrl+"ljyun_"+ljyunId+"_merchant?" +postUrl); } }
-
使用
TestIndexService.testAddUserTags()
方法单元测试,单元测新增db_test.User和ljyun_1_merchant.app_tag表数据, 注意事务存在与测试的service中./** * 单元测试新增db_test.User和ljyun_512_merchant.app_tag表数据 * 测试动态修改数据源的连接url属性,也就意味着切换数据库了 * 在service中启用事务注解 */ @Test public void testAddUserTags(){ User user = new User(); user.setCreateTime(null); user.setName("张三"); user.setAge(10); AppTag appTag = new AppTag(); appTag.setTagName("app_name_1"); //appTag.setTagType(1); appTagService.insert(user,appTag); }
appTagService.insert()
中使用SwitchDB.to()
切换数据库/** * 分布式事务新增操作 * @param user db_test.tb_user实体 * @param appTag 私有库app_tag实体 */ @Transactional public void insert(User user, AppTag appTag) { user.setCreateTime(null); //新增db_test.tb_user表数据 userDao.insert(user); //切换为ljyun_1_merchant数据库 SwitchDB.to(1); //ljYun私有库数据库连接url信息 System.out.println("yun数据库连接url="+((DruidXADataSource) SpringContextUtil.getBean("dataSourceYunXA")).getUrl()); //db_car数据库连接url信息 DruidXADataSource dataSourceCar = (DruidXADataSource) SpringContextUtil.getBean("dataSourceCarXA"); System.out.println("db_car连接url="+dataSourceCar.getUrl()); //db_test数据库连接url信息 DruidXADataSource dataSourceTest = (DruidXADataSource) SpringContextUtil.getBean("dataSourceTestXA"); System.out.println("db_test连接url="+dataSourceTest.getUrl()); //int a = 10/0; //故意异常回滚 //新增私有库.app_tag表数据 appTagDao.insert(appTag); //手动回滚事务 //TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); }
-
可以在业务代码中通过如下方式查看自己现在连接数据库的信息:
以下代码中dataSourceYunXA
就是数据源配置文件DataSourceCarConfig.java
中对应的bean的名称.//ljYun私有库数据库连接url信息 System.out.println("yun数据库连接url="+((DruidXADataSource) SpringContextUtil.getBean("dataSourceYunXA")).getUrl());
五. 错误与总结
- 配置JTA过程出现错误, 参考文章
- 解决SQLFeatureNotSupportedException异常
- https://github.com/alibaba/druid/issues/2823
- 最终通过这个配置出来的(基于mybatis注解), git地址;
- 本篇文章的实现方式是mybatis基于xml的,完整代码地址: https://github.com/liangxifeng833/jta-demo