使用Atomikos須要能夠包含 spring-boot-starter-jta-atomikos 依賴java
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jta-atomikos</artifactId> </dependency>
接下來配置兩個數據源mysql
配置信息以下:git
spring.datasource.test.url = jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull spring.datasource.test.username = root spring.datasource.test.password = root spring.datasource.test.minPoolSize = 3 spring.datasource.test.maxPoolSize = 25 spring.datasource.test.maxLifetime = 20000 spring.datasource.test.borrowConnectionTimeout = 30 spring.datasource.test.loginTimeout = 30 spring.datasource.test.maintenanceInterval = 60 spring.datasource.test.maxIdleTime = 60 spring.datasource.test.testQuery = select 1
Java Propertiesgithub
@ConfigurationProperties(prefix = "spring.datasource.test") public class TestDBConfig { private String url; private String username; private String password; /** min-pool-size 最小鏈接數 **/ private int minPoolSize; /** max-pool-size 最大鏈接數 **/ private int maxPoolSize; /** max-lifetime 鏈接最大存活時間 **/ private int maxLifetime; /** borrow-connection-timeout 獲取鏈接失敗從新獲等待最大時間,在這個時間內若是有可用鏈接,將返回 **/ private int borrowConnectionTimeout; /** login-timeout java數據庫鏈接池,最大可等待獲取datasouce的時間 **/ private int loginTimeout; /** maintenance-interval 鏈接回收時間 **/ private int maintenanceInterval; /** max-idle-time 最大閒置時間,超過最小鏈接池鏈接的鏈接將將關閉 **/ private int maxIdleTime; /** test-query 測試SQL **/ private String testQuery; // get set 略 }
而後要配置數據源, 須要用XADataSourcespring
@Bean(name = "testDataSource") public DataSource testDataSource() 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; }
第二個數據源配置差很少 不貼代碼了sql
Mybatis的配置都差很少,就是把數據源換一下,另外把本地事務那段刪除就行數據庫
而後要配置JTAspringboot
@Configuration @ComponentScan @EnableTransactionManagement public class TransactionManagerConfig { @Bean(name = "userTransaction") public UserTransaction userTransaction() throws Throwable { UserTransactionImp userTransactionImp = new UserTransactionImp(); userTransactionImp.setTransactionTimeout(10000); return userTransactionImp; } @Bean(name = "atomikosTransactionManager", initMethod = "init", destroyMethod = "close") 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 { UserTransaction userTransaction = userTransaction(); JtaTransactionManager manager = new JtaTransactionManager(userTransaction,atomikosTransactionManager()); return manager; } }
配置好transactionManagermybatis
找個Service試下就OK了spring-boot
demo地址: https://github.com/CodingZx/springboot-jta
數據庫爲mysql 就測試表就一個字段 根據mybatis的xml新建一下表便可.