这篇文章主要介绍“springboot-jta-atomikos多数据源事务管理如何实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“springboot-jta-atomikos多数据源事务管理如何实现”文章能帮助大家解决问题。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jta-atomikos</artifactId> </dependency>
#atomikos测试 spring.datasource.test1.url=jdbc:mysql://127.0.0.1:3306/test1?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai spring.datasource.test1.user=root spring.datasource.test1.password=arsenal spring.datasource.test2.url=jdbc:mysql://127.0.0.1:3306/test2?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai spring.datasource.test2.user=root spring.datasource.test2.password=arsenal
package com.llq.atomikos.config; import com.atomikos.icatch.jta.UserTransactionImp; import com.atomikos.icatch.jta.UserTransactionManager; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.transaction.jta.JtaTransactionManager; import javax.sql.DataSource; import javax.transaction.UserTransaction; import java.util.Properties; /** * @author lvlianqi * @description * @date 2022/3/7 */ @Configuration public class DBAtomikosConfig { //--------------------数据源1-------------------- @ConfigurationProperties(prefix = "spring.datasource.test1") @Bean public Properties testOneProperties() { return new Properties(); } @Bean(name = "testOneDataSource") @Primary public DataSource testOneDataSource() { AtomikosDataSourceBean ds = new AtomikosDataSourceBean(); Properties prop = testOneProperties(); ds.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource"); ds.setUniqueResourceName("testOne"); ds.setXaProperties(prop); return ds; } @Bean @Primary public JdbcTemplate testOneJdbcTemplate(@Qualifier("testOneDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } //--------------------数据源2-------------------- @ConfigurationProperties(prefix = "spring.datasource.test2") @Bean public Properties testTwoProperties() { return new Properties(); } @Bean(name = "testTwoDataSource") public DataSource testTwoDataSource() { AtomikosDataSourceBean ds = new AtomikosDataSourceBean(); Properties prop = testTwoProperties(); ds.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource"); ds.setUniqueResourceName("testTwo"); ds.setXaProperties(prop); return ds; } @Bean public JdbcTemplate testTwoJdbcTemplate(@Qualifier("testTwoDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } //--------------------配置spring的JtaTransactionManager,底层委派给atomikos进行处理-------------------- @Bean public JtaTransactionManager jtaTransactionManager () { UserTransactionManager userTransactionManager = new UserTransactionManager(); UserTransaction userTransaction = new UserTransactionImp(); return new JtaTransactionManager(userTransaction, userTransactionManager); } }
package com.llq.atomikos.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * @author lvlianqi * @description * @date 2022/3/7 */ @Service public class TestAtomikos implements ITest{ @Qualifier("testOneJdbcTemplate") @Autowired private JdbcTemplate testOneJdbcTemplate; @Qualifier("testTwoJdbcTemplate") @Autowired private JdbcTemplate testTwoJdbcTemplate; /** * 测试正常情况 */ @Transactional(rollbackFor = Exception.class, value = "jtaTransactionManager") public void test() { testOneJdbcTemplate.execute("insert into user (name, age) values ('张三', 18);"); testTwoJdbcTemplate.execute("insert into user (name, age) values ('李四', 20);"); } /** * 测试异常情况 */ @Transactional(rollbackFor = Exception.class, value = "jtaTransactionManager") public void testByException() { testOneJdbcTemplate.execute("insert into user (name, age) values ('张三', 18);"); testTwoJdbcTemplate.execute("insert into user (name, age) values ('李四', 20);"); int i = 1/0; } }
//使用atomikos private static Class CLS = TestAtomikos.class; @Autowired ApplicationContext applicationContext; @Test public void testByException() { ITest test = (ITest) applicationContext.getBean(CLS); test.testByException(); }
执行错误
数据库test1 user表没有记录
数据库test2 user表没有记记录
关于“springboot-jta-atomikos多数据源事务管理如何实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。