SpringBoot使用Atomikos技术整合多数据源的实现

网友投稿 369 2023-01-27

SpringBoot使用Atomikos技术整合多数据源的实现

Atomikos是一个为java平台提供增值服务的并且开源类事务管理器,如果将事务统一注册到Atomikos中,则可以统一管理。常用于后台管理系统的多数据库操作。

多数据源的划分方式

方式: a、按包名 b、按注解区分

Springboot默认集成事务,只主要在方法上加上@Transactional即可,不需要开启@EnableTransactionManagement注解

如果多个数据库,则每个方法都需要单独设置事务管理器@Transactional(transactionManager = "test2TransactionManager"),如果事务中含多数据源则无效。

Atomikos使用

1.依赖引入

org.springframework.boot

spring-boot-starter-jta-atomikos

2.多数据源配置文件

# mysql 1

mysql.datasource.test1.url = jdbc:mysql://localhost:3306/test01?useUnicodeTQPYzYiP=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

# Mysql 2

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")

public class DBConfig1 {

private String url;

private String username;

private String password;

private int minPoolSize;

private int maxPoolSize;

private int maxLifetime;

private int borrowConnectionTimeout;

private int loginTimeout;

private int maintenanceInterval;

private int maxIdleTime;

private String testQuery;

}

@Data

@ConfigurationProperties(prefix = "mysql.datasource.test2")

public class DBConfig2 {

private String url;

private String username;

private String password;

private int minPoolSize;

private int maxPoolSize;

private int maxLifetime;

private int borrowConnectionTimeout;

private int loginTimeout;

private int maintenanceInterval;

private int maxIdleTime;

private String testQuery;

}

@Configuration

// basePackages 最好分开配置 如果放在同一个文件夹可能会报错

@MapperScan(basePackages = "com.itmayiedu.test01", sqlSessionTemplateRef = "testSqlSessionTemplathttp://e")

public class MyBatisConfig1 {

// 配置数据源

@TQPYzYiPPrimary

@Bean(name = "testDataSource")

public DataSource testDataSource(DBConfig1 testConfig) throws SQLException {

MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();

mysqlXaDataSource.setUrl(testConfig.getUrl());

mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

mysqlXaDataSource.setPassword(testConfig.getPassword());

mysqlXaDataSource.setUser(testConfig.getUsername());

mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();

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());

return xaDataSource;

}

@Primary

@Bean(name = "testSqlSessionFactory")

public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource)

throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

return bean.getObject();

}

@Primary

@Bean(name = "testSqlSessionTemplate")

public SqlSessionTemplate testSqlSessionTemplate(

@Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

@Configuration

@MapperScan(basePackages = "com.itmayiedu.test02", sqlSessionTemplateRef = "test2SqlSessionTemplate")

public class MyBatisConfig2 {

// 配置数据源

@Bean(name = "test2DataSource")

public DataSource testDataSource(DBConfig2 testConfig) throws SQLException {

MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();

mysqlXaDataSource.setUrl(testConfig.getUrl());

mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

mysqlXaDataSource.setPassword(testConfig.getPassword());

mysqlXaDataSource.setUser(testConfig.getUsername());

mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();

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());

return xaDataSource;

}

@Bean(name = "test2SqlSessionFactory")

public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)

throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

return bean.getObject();

}

@Bean(name = "test2SqlSessionTemplate")

public SqlSessionTemplate testSqlSessionTemplate(

@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

4.启动加载配置

@EnableConfigurationProperties(value = { DBConfig1.class, DBConfig2.class })

使用还是正常使用@Transactional方式

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java8新特性之方法引用的实践指南
下一篇:开放api接口聊天软件(软件API接口)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~