springboot-jta-atomikos多数据源事务管理如何实现
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍“springboot-jta-atomikos多数据源事务管理如何实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“springboot-jta-atomikos多数据源事务管理如何实现”文章能帮助大家解决问题。
项目目录结构
实现
1.添加依赖 pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jta-atomikos</artifactId></dependency>
2.配置数据库连接信息 application.properties
#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/Shanghaispring.datasource.test1.user=rootspring.datasource.test1.password=arsenalspring.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/Shanghaispring.datasource.test2.user=rootspring.datasource.test2.password=arsenal
3.创建多数据源 DBAtomikosConfig.java
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;@Configurationpublic 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); }}
4.测试事务类 TestAtomikos.java
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;@Servicepublic 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; }}
5.测试 SpringbootAtomikosApplicationTests.java
//使用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多数据源事务管理如何实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341