参考: https://github.com/classloader/springboot-jta-atomikos-demo
參考:二 :建议参考 https://blog.csdn.net/a510835147/article/details/75675311
依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sea.sample</groupId> <artifactId>spring-boot-jta-atomikos-sample</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> </parent> <name>mul-at</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <hibernate-entitymanager.version>4.3.5.Final</hibernate-entitymanager.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jta-atomikos</artifactId> <!-- <version>${spring.boot.version}</version> --> </dependency> <!-- https://blog.csdn.net/a510835147/article/details/75675311 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.12.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>1.5.8</version> </dependency> </dependencies> </project>
application.yml
spring.main.show_banner=false server.port = 8083 endpoints.shutdown.enabled=true #order.datasource.url=jdbc:mariadb://10.10.54.233:3306/test2 #order.datasource.username=root #order.datasource.password=oppein123! #order.datasource.driverClassName = org.mariadb.jdbc.Driver # #customer.datasource.url=jdbc:mariadb://10.10.54.233:3306/test1 #customer.datasource.username=root #customer.datasource.password=oppein123! #customer.datasource.driverClassName = org.mariadb.jdbc.Driver order.datasource.url=jdbc:mysql://localhost:3306/sea?useUnicode=true&characterEncoding=UTF-8&pinGlobalTxToPhysicalConnection=true order.datasource.username=root order.datasource.password=root order.datasource.driverClassName = com.mysql.jdbc.Driver customer.datasource.url=jdbc:mysql://localhost:3306/shan?useUnicode=true&characterEncoding=UTF-8&pinGlobalTxToPhysicalConnection=true customer.datasource.username=root customer.datasource.password=root customer.datasource.driverClassName = com.mysql.jdbc.Driver spring.jta.atomikos.datasource.max-pool-size=20 spring.jta.atomikos.connectionfactory.max-pool-size=20 spring.jta.atomikos.connectionfactory.borrow-connection-timeout=60 spring.jta.atomikos.datasource.borrow-connection-timeout=60 # Show all queries spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.properties.hibernate.show-sql=true spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.generate_statistics=true
step1:
写一个 AtomikosJtaPlatform
package com.oppein.www; import javax.transaction.TransactionManager; import javax.transaction.UserTransaction; import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform; public class AtomikosJtaPlatform extends AbstractJtaPlatform { private static final long serialVersionUID = 1L; static TransactionManager transactionManager; static UserTransaction transaction; @Override protected TransactionManager locateTransactionManager() { return transactionManager; } @Override protected UserTransaction locateUserTransaction() { return transaction; } }
主配置事务:
package com.oppein.www; import javax.transaction.TransactionManager; import javax.transaction.UserTransaction; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.vendor.Database; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.jta.JtaTransactionManager; import com.atomikos.icatch.jta.UserTransactionImp; import com.atomikos.icatch.jta.UserTransactionManager; @Configuration @ComponentScan @EnableTransactionManagement public class MainConfig { @Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); hibernateJpaVendorAdapter.setShowSql(true); hibernateJpaVendorAdapter.setGenerateDdl(true); hibernateJpaVendorAdapter.setDatabase(Database.MYSQL); return hibernateJpaVendorAdapter; } @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); AtomikosJtaPlatform.transactionManager = userTransactionManager; return userTransactionManager; } @Bean(name = "transactionManager") @DependsOn({ "userTransaction", "atomikosTransactionManager" }) public PlatformTransactionManager transactionManager() throws Throwable { UserTransaction userTransaction = userTransaction(); AtomikosJtaPlatform.transaction = userTransaction; TransactionManager atomikosTransactionManager = atomikosTransactionManager(); return new JtaTransactionManager(userTransaction, atomikosTransactionManager); } }
数据源1:CustomerConfig
package com.oppein.www; import java.sql.SQLException; import java.util.HashMap; import javax.sql.DataSource; import org.mariadb.jdbc.MariaDbDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.Primary; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import com.atomikos.jdbc.AtomikosDataSourceBean; import com.oppein.www.repository.customer.CustomerDatasourceProperties; //import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource; @Configuration @DependsOn("transactionManager") @EnableJpaRepositories(basePackages = "com.oppein.www.repository.customer", entityManagerFactoryRef = "customerEntityManager", transactionManagerRef = "transactionManager") @EnableConfigurationProperties(CustomerDatasourceProperties.class) public class CustomerConfig { @Autowired private JpaVendorAdapter jpaVendorAdapter; @Autowired private CustomerDatasourceProperties customerDatasourceProperties; @Primary @Bean(name = "customerDataSource", initMethod = "init", destroyMethod = "close") // @Qualifier("customerDataSource") public DataSource customerDataSource() throws SQLException { // MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource(); MariaDbDataSource mysqlXaDataSource = new MariaDbDataSource(); mysqlXaDataSource.setUrl(customerDatasourceProperties.getUrl()); // mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true); mysqlXaDataSource.setPassword(customerDatasourceProperties.getPassword()); mysqlXaDataSource.setUser(customerDatasourceProperties.getUsername()); AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean(); xaDataSource.setXaDataSource(mysqlXaDataSource); xaDataSource.setUniqueResourceName("xads1"); xaDataSource.setBorrowConnectionTimeout(60); xaDataSource.setMaxPoolSize(20); return xaDataSource; } @Primary @Bean(name = "customerEntityManager") @DependsOn("transactionManager") public LocalContainerEntityManagerFactoryBean customerEntityManager() throws Throwable { HashMap<String, Object> properties = new HashMap<String, Object>(); properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName()); properties.put("javax.persistence.transactionType", "JTA"); LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); entityManager.setJtaDataSource(customerDataSource()); entityManager.setJpaVendorAdapter(jpaVendorAdapter); entityManager.setPackagesToScan("com.oppein.www.domain.customer"); entityManager.setPersistenceUnitName("customerPersistenceUnit"); entityManager.setJpaPropertyMap(properties); return entityManager; } }
数据源 2 OrderConfig
package com.oppein.www; import java.sql.SQLException; import java.util.HashMap; import javax.sql.DataSource; import com.oppein.www.repository.order.OrderDatasourceProperties; import org.mariadb.jdbc.MariaDbDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import com.atomikos.jdbc.AtomikosDataSourceBean; //import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource; @Configuration @DependsOn("transactionManager") @EnableJpaRepositories(basePackages = "com.oppein.www.repository.order", entityManagerFactoryRef = "orderEntityManager", transactionManagerRef = "transactionManager") @EnableConfigurationProperties(OrderDatasourceProperties.class) public class OrderConfig { @Autowired private JpaVendorAdapter jpaVendorAdapter; @Autowired private OrderDatasourceProperties orderDatasourceProperties; @Bean(name = "orderDataSource", initMethod = "init", destroyMethod = "close") public DataSource orderDataSource() throws SQLException { // MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource(); MariaDbDataSource mysqlXaDataSource = new MariaDbDataSource(); mysqlXaDataSource.setUrl(orderDatasourceProperties.getUrl()); // mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true); mysqlXaDataSource.setPassword(orderDatasourceProperties.getPassword()); mysqlXaDataSource.setUser(orderDatasourceProperties.getUsername()); AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean(); xaDataSource.setXaDataSource(mysqlXaDataSource); xaDataSource.setUniqueResourceName("xads2"); xaDataSource.setBorrowConnectionTimeout(60); xaDataSource.setMaxPoolSize(20); return xaDataSource; } @Bean(name = "orderEntityManager") public LocalContainerEntityManagerFactoryBean orderEntityManager() throws Throwable { HashMap<String, Object> properties = new HashMap<String, Object>(); properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName()); properties.put("javax.persistence.transactionType", "JTA"); LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); entityManager.setJtaDataSource(orderDataSource()); entityManager.setJpaVendorAdapter(jpaVendorAdapter); entityManager.setPackagesToScan("com.oppein.www.domain.order"); entityManager.setPersistenceUnitName("orderPersistenceUnit"); entityManager.setJpaPropertyMap(properties); return entityManager; } }