SpringBoot使用Atomikos技术整合多数据源的实现
Atomikos是一个为Java平台提供增值服务的并且开源类事务管理器,如果将事务统一注册到Atomikos中,则可以统一管理。常用于后台管理系统的多数据库操作。
多数据源的划分方式
方式:a、按包名b、按注解区分
Springboot默认集成事务,只主要在方法上加上@Transactional即可,不需要开启@EnableTransactionManagement注解
如果多个数据库,则每个方法都需要单独设置事务管理器@Transactional(transactionManager="test2TransactionManager"),如果事务中含多数据源则无效。
Atomikos使用
1.依赖引入
org.springframework.boot spring-boot-starter-jta-atomikos
2.多数据源配置文件
#Mysql1 mysql.datasource.test1.url=jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8 mysql.datasource.test1.username=root mysql.datasource.test1.password=root mysql.datasource.test1.minPoolSize=3 mysql.datasource.test1.maxPoolSize=25 mysql.datasource.test1.maxLifetime=20000 mysql.datasource.test1.borrowConnectionTimeout=30 mysql.datasource.test1.loginTimeout=30 mysql.datasource.test1.maintenanceInterval=60 mysql.datasource.test1.maxIdleTime=60 #Mysql2 mysql.datasource.test2.url=jdbc:mysql://localhost:3306/test02?useUnicode=true&characterEncoding=utf-8 mysql.datasource.test2.username=root mysql.datasource.test2.password=root mysql.datasource.test2.minPoolSize=3 mysql.datasource.test2.maxPoolSize=25 mysql.datasource.test2.maxLifetime=20000 mysql.datasource.test2.borrowConnectionTimeout=30 mysql.datasource.test2.loginTimeout=30 mysql.datasource.test2.maintenanceInterval=60 mysql.datasource.test2.maxIdleTime=60
3.配置代码
@Data @ConfigurationProperties(prefix="mysql.datasource.test1") publicclassDBConfig1{ privateStringurl; privateStringusername; privateStringpassword; privateintminPoolSize; privateintmaxPoolSize; privateintmaxLifetime; privateintborrowConnectionTimeout; privateintloginTimeout; privateintmaintenanceInterval; privateintmaxIdleTime; privateStringtestQuery; } @Data @ConfigurationProperties(prefix="mysql.datasource.test2") publicclassDBConfig2{ privateStringurl; privateStringusername; privateStringpassword; privateintminPoolSize; privateintmaxPoolSize; privateintmaxLifetime; privateintborrowConnectionTimeout; privateintloginTimeout; privateintmaintenanceInterval; privateintmaxIdleTime; privateStringtestQuery; } @Configuration //basePackages最好分开配置如果放在同一个文件夹可能会报错 @MapperScan(basePackages="com.itmayiedu.test01",sqlSessionTemplateRef="testSqlSessionTemplate") publicclassMyBatisConfig1{ //配置数据源 @Primary @Bean(name="testDataSource") publicDataSourcetestDataSource(DBConfig1testConfig)throwsSQLException{ MysqlXADataSourcemysqlXaDataSource=newMysqlXADataSource(); mysqlXaDataSource.setUrl(testConfig.getUrl()); mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true); mysqlXaDataSource.setPassword(testConfig.getPassword()); mysqlXaDataSource.setUser(testConfig.getUsername()); mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true); AtomikosDataSourceBeanxaDataSource=newAtomikosDataSourceBean(); xaDataSource.setXaDataSource(mysqlXaDataSource); xaDataSource.setUniqueResourceName("testDataSource"); 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()); returnxaDataSource; } @Primary @Bean(name="testSqlSessionFactory") publicSqlSessionFactorytestSqlSessionFactory(@Qualifier("testDataSource")DataSourcedataSource) throwsException{ SqlSessionFactoryBeanbean=newSqlSessionFactoryBean(); bean.setDataSource(dataSource); returnbean.getObject(); } @Primary @Bean(name="testSqlSessionTemplate") publicSqlSessionTemplatetestSqlSessionTemplate( @Qualifier("testSqlSessionFactory")SqlSessionFactorysqlSessionFactory)throwsException{ returnnewSqlSessionTemplate(sqlSessionFactory); } } @Configuration @MapperScan(basePackages="com.itmayiedu.test02",sqlSessionTemplateRef="test2SqlSessionTemplate") publicclassMyBatisConfig2{ //配置数据源 @Bean(name="test2DataSource") publicDataSourcetestDataSource(DBConfig2testConfig)throwsSQLException{ MysqlXADataSourcemysqlXaDataSource=newMysqlXADataSource(); mysqlXaDataSource.setUrl(testConfig.getUrl()); mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true); mysqlXaDataSource.setPassword(testConfig.getPassword()); mysqlXaDataSource.setUser(testConfig.getUsername()); mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true); AtomikosDataSourceBeanxaDataSource=newAtomikosDataSourceBean(); xaDataSource.setXaDataSource(mysqlXaDataSource); xaDataSource.setUniqueResourceName("test2DataSource"); 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()); returnxaDataSource; } @Bean(name="test2SqlSessionFactory") publicSqlSessionFactorytestSqlSessionFactory(@Qualifier("test2DataSource")DataSourcedataSource) throwsException{ SqlSessionFactoryBeanbean=newSqlSessionFactoryBean(); bean.setDataSource(dataSource); returnbean.getObject(); } @Bean(name="test2SqlSessionTemplate") publicSqlSessionTemplatetestSqlSessionTemplate( @Qualifier("test2SqlSessionFactory")SqlSessionFactorysqlSessionFactory)throwsException{ returnnewSqlSessionTemplate(sqlSessionFactory); } }
4.启动加载配置
@EnableConfigurationProperties(value={DBConfig1.class,DBConfig2.class})
使用还是正常使用@Transactional方式
到此这篇关于SpringBoot使用Atomikos技术整合多数据源的实现的文章就介绍到这了,更多相关SpringBootAtomikos多数据源内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。