您好,登錄后才能下訂單哦!
這篇文章主要介紹“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。