springboot+JTA简单实现多数据源事务

news/2024/11/30 6:53:05/

Springboot + JTA 简单实现多数据源事务

    • JTA
    • 依赖
    • YML配置
    • DataSourceConfig+Mybatis 分包配置
        • db1
        • db2
    • 测试

JTA

JTA,即Java Transaction API,JTA允许应用程序执行分布式事务处理——在两个或多个网络计算机资源上访问并且更新数据。

依赖

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jta-atomikos</artifactId></dependency>

YML配置

spring:  jta:atomikos:datasource:primary:xa-properties:url: jdbc:mysql://host:3306/test-source?useUnicode=true&characterEncoding=utf-8user: ENC(leNmW2mZWHadQdE9aq5iIw==)password: ENC(k5SFNga7adQ/xFsxzbe3Rbe3pZEow0KdSj0pWlDjDJA=)xa-data-source-class-name: com.mysql.cj.jdbc.MysqlXADataSourceunique-resource-name: coolSourcemax-pool-size: 25min-pool-size: 3max-lifetime: 20000borrow-connection-timeout: 10000secondary:xa-properties:url: jdbc:mysql://host:3306/test-user?useUnicode=true&characterEncoding=utf-8user: ENC(leNmW2mZWHadQdE9aq5iIw==)password: ENC(k5SFNga7adQ/xFsxzbe3Rbe3pZEow0KdSj0pWlDjDJA=)xa-data-source-class-name: com.mysql.cj.jdbc.MysqlXADataSourceunique-resource-name: coolUsermax-pool-size: 25min-pool-size: 3max-lifetime: 20000borrow-connection-timeout: 10000

DataSourceConfig+Mybatis 分包配置

db1

@Configuration
@MapperScan(basePackages = "com.lcx.more.mapper.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DB1Config {@Bean("coolSourceDataSource")@ConfigurationProperties(prefix = "spring.jta.atomikos.datasource.primary")public DataSource primaryDataSource() {return new AtomikosDataSourceBean();}@Bean(name = "db1SqlSessionFactory")public SqlSessionFactory db1SqlSessionFactory(@Qualifier("coolSourceDataSource") DataSource coolSourceDataSource) throws Exception {MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean ();sessionFactory.setDataSource(coolSourceDataSource);MybatisConfiguration configuration = new MybatisConfiguration();configuration.setMapUnderscoreToCamelCase(true);//驼峰sessionFactory.setConfiguration(configuration);sessionFactory.setTypeAliasesPackage("com.lcx.more.entity.db1");sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));return sessionFactory.getObject();}@Bean(name = "db1SqlSessionTemplate")public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}}

db2

@Configuration
@MapperScan(basePackages = "com.lcx.more.mapper.db2", sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DB2Config {@Bean("coolUserDataSource")@ConfigurationProperties(prefix = "spring.jta.atomikos.datasource.secondary")public DataSource secondaryDataSource() {return new AtomikosDataSourceBean();}@Bean(name = "db2SqlSessionFactory")public SqlSessionFactory db2SqlSessionFactory(@Qualifier("coolUserDataSource") DataSource coolUserDataSource) throws Exception {MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean ();sessionFactory.setDataSource(coolUserDataSource);MybatisConfiguration configuration = new MybatisConfiguration();configuration.setMapUnderscoreToCamelCase(true);//驼峰sessionFactory.setConfiguration(configuration);sessionFactory.setTypeAliasesPackage("com.lcx.more.entity.db2");sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db2/*.xml"));return sessionFactory.getObject();}@Bean(name = "db2SqlSessionTemplate")public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}
}

测试

   @Transactional(rollbackFor = Exception.class)public void jtaTest() {// DB1TxJTATest txJTATest = new TxJTATest();txJTATest.setName("test2");txJTATestMapper.insert(txJTATest);// DB2 tb1TestTxA testTxA = new TestTxA();testTxA.setTestName("txa2");testTxAMapper.insert(testTxA);// DB2 tb2TestTxB testTxB = new TestTxB();testTxB.setTestCode("txb2");testTxBMapper.insert(testTxB);throw new RuntimeException("报错啦...");}

http://www.ppmy.cn/news/564649.html

相关文章

Kubeadm 部署k8s实现并且演示滚动不停服务更新

1. 环境准备 在开始之前&#xff0c;部署Kubernetes集群机器需要满足以下几个条件&#xff1a; 一台或多台机器&#xff0c;操作系统 CentOS7.x-86_x64&#xff1b;硬件配置&#xff1a;2GB或更多RAM&#xff0c;2个CPU或更多CPU&#xff0c;硬盘30GB或更多&#xff1b;集群中…

PictureView 1.2.1 中文版 (小巧的图片浏览工具)

Picture View 是一款轻量小巧的 macOS 图片浏览应用&#xff0c;可以帮助您方便快捷的浏览图片。此应用不会产生任何缓存&#xff0c;适合替代默认看图应用。多种的查看模式&#xff0c;方便快捷的操作设计&#xff0c;支持查看 EXIF 信息&#xff0c;支持防系统休眠。 两种看…

PCDViewer 3.2 Linux 版(Ubuntu)

PCDViewer Linux版终于和大家见面了&#xff01; PCDViewer 是一款轻量而又功能强大的点云可视化软件&#xff0c;可以方便高效地对各种格式的点云进行满足各类需求的渲染和查询。这样好用的软件&#xff0c;如果只有Windows版确实是一大遗憾&#xff0c;由于各方面的原因&…

HDFVIEW3.1.2下载

版本如下 各个版本请直接下载即可 centos https://hdf-wordpress-1.s3.amazonaws.com/wp-content/uploads/manual/HDFView/3.1.2/HDFView-3.1.2-centos7_64.tar.gz OSX https://hdf-wordpress-1.s3.amazonaws.com/wp-content/uploads/manual/HDFView/3.1.2/HDFView-3.1.2-osx1…

HyperWorks2020中文版

1、下载软件压缩包文件&#xff0c;得到HyperWorks2020源文件和&#xff1b; 2、解压后&#xff0c;首先点击Setup文件夹下的“hw2020_win64.exe”启动安装程序&#xff0c;可选简体中文&#xff1b; 3、选择阅读同意条款&#xff0c;依提示和自身需求来进行安装&#xff1b;…

#Labview <史上最全VISA驱动下载链接>

目录 1、前言2、下载链接 1、前言 NI-​VISA​是​一个​NI​仪器​驱动​程序&#xff0c;​是​虚拟​仪器​软件​架构​(VISA) I/​O​标准​的​具体​实现。​虚拟​仪器​软件​架构​(VISA)​这​一​标准​用于​配置​和​编​程​基于​GPIB、​VXI、​PXI、​串​口…

vnc viewer中文版,vnc viewer中文版如何管理服务器

vnc viewer是一款远程控制的软件&#xff0c;一般用于远程解决电脑故障或软件调试&#xff0c;小编在使用vnc viewer时发现了一款好用的vnc viewer中文版软件&#xff1a;IIS7服务器管理工具&#xff0c;那在使用vnc viewer中文版软件时如何管理服务器呢&#xff1f;接下来给大…

LabVIEW,NI-DAQmx,LabWindows各个版本下载

最近实验室要用LabVIEW采集数据&#xff0c;找了一下下载地址&#xff0c;官网需要注册&#xff0c;很不爽。后经查找&#xff0c;发现有个专门用来下载的不需要注册的http://download.ni.com&#xff0c;整理了一下各个工具的下载具体地址。 这个是总的最近资源的下载地址&am…