Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement springboot-jta-atomikos Multi-data Source transaction Management

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to realize springboot-jta-atomikos multi-data source transaction management". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to realize springboot-jta-atomikos multi-data source transaction management" can help you solve the problem.

Project directory structure

Achieve 1. Add dependency pom.xml org.springframework.boot spring-boot-starter-jta-atomikos2. Configure database connection information application.properties#atomikos test 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=arsenal3. Create a multiple data source DBAtomikosConfig.javapackage 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 * / @ Configurationpublic class DBAtomikosConfig {/ /-data source 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);} /-data source 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);} /-configure the JtaTransactionManager of spring, and the underlying layer delegates to atomikos for processing-@ Bean public JtaTransactionManager jtaTransactionManager () {UserTransactionManager userTransactionManager = new UserTransactionManager () UserTransaction userTransaction = new UserTransactionImp (); return new JtaTransactionManager (userTransaction, userTransactionManager);} 4. Test transaction class TestAtomikos.javapackage 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 * / @ Servicepublic class TestAtomikos implements ITest {@ Qualifier ("testOneJdbcTemplate") @ Autowired private JdbcTemplate testOneJdbcTemplate @ Qualifier ("testTwoJdbcTemplate") @ Autowired private JdbcTemplate testTwoJdbcTemplate; / * Test normal * / @ Transactional (rollbackFor = Exception.class, value = "jtaTransactionManager") public void test () {testOneJdbcTemplate.execute ("insert into user (name, age) values ('Zhang San', 18);"); testTwoJdbcTemplate.execute ("insert into user (name, age) values ('Li Si', 20);") } / * Test anomalies * / @ Transactional (rollbackFor = Exception.class, value = "jtaTransactionManager") public void testByException () {testOneJdbcTemplate.execute ("insert into user (name, age) values ('Zhang San', 18);"); testTwoJdbcTemplate.execute ("insert into user (name, age) values ('Li Si', 20);"); int I = 1canned 0;}} 5. Test SpringbootAtomikosApplicationTests.java / / use atomikos private static Class CLS = TestAtomikos.class; @ Autowired ApplicationContext applicationContext; @ Test public void testByException () {ITest test = (ITest) applicationContext.getBean (CLS); test.testByException ();} Test results

Execution error

Database test1 user table has no record

Database test2 user table has no record

This is the end of the introduction to "how to implement springboot-jta-atomikos multi-data source transaction management". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report