springboot jta atomikos实现分布式事物管理

 更新时间:2019年12月20日 11:14:56   作者:求知若渴的蜗牛  
这篇文章主要介绍了springboot jta atomikos实现分布式事物管理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了springboot jta atomikos实现分布式事物管理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

当项目在连接多个数据库时可能会发生事务问题,即一个库的事务不可能去操作另一个数据库的事务,这时就需要使用atomikos对数据库的事务进行统一的管理

第一步添加atomikos的依赖

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

第二步配置数据源,我这里有2个数据库(ruan和youxianqi),你有多少就加多少。

spring:
 datasource:
  system:
   jdbc-url: jdbc:oracle:thin:@localhost:1521/orcl
   driver-class-name: oracle.jdbc.OracleDriver
   username: yuan
   password: 1234
   initial-size: 5
   min-idle: 5
   max-active: 20
   min-evictable-idle-time-millis: 300000
   validation-query: SELECT 1 FROM DUAL
   test-while-idle: true
  kllogt:
   jdbc-url: jdbc:oracle:thin:@localhost:1521/orcl
   driver-class-name: oracle.jdbc.OracleDriver
   username: youxianqi
   password: youxianqi
   initial-size: 5
   min-idle: 5
   max-active: 20
   min-evictable-idle-time-millis: 300000
   validation-query: SELECT 1 FROM DUAL
   test-while-idle: true
logging:
 level:
  org.springframework.web: debug

然后创建DBConfig1和DBConfig2,这两个实体类就是存放两个数据源的数据的。

package com.cgb.config;
 
 
import org.springframework.boot.context.properties.ConfigurationProperties;
 
@ConfigurationProperties(prefix = "spring.datasource.system")
public class DBConfig1 {
 
  private String jdbc-url;
  private String username;
  private String password;
 
  private int minPoolSize;
 
  private int maxPoolSize;
 
  private int maxLifetime;
 
  private int borrowConnectionTimeout;
 
  private int loginTimeout;
 
  private int maintenanceInterval;
 
  private int maxIdleTime;
 
  private String testQuery;
 
  public String getJdbc-url() {
    return url;
  }
 
  public void setJdbc-url(String jdbc-url) {
    this.jdbc-url= jdbc-url;
  }
 
  public String getUsername() {
    return username;
  }
 
  public void setUsername(String username) {
    this.username = username;
  }
 
  public String getPassword() {
    return password;
  }
 
  public void setPassword(String password) {
    this.password = password;
  }
 
  public int getMinPoolSize() {
    return minPoolSize;
  }
 
  public void setMinPoolSize(int minPoolSize) {
    this.minPoolSize = minPoolSize;
  }
 
  public int getMaxPoolSize() {
    return maxPoolSize;
  }
 
  public void setMaxPoolSize(int maxPoolSize) {
    this.maxPoolSize = maxPoolSize;
  }
 
  public int getMaxLifetime() {
    return maxLifetime;
  }
 
  public void setMaxLifetime(int maxLifetime) {
    this.maxLifetime = maxLifetime;
  }
 
  public int getBorrowConnectionTimeout() {
    return borrowConnectionTimeout;
  }
 
  public void setBorrowConnectionTimeout(int borrowConnectionTimeout) {
    this.borrowConnectionTimeout = borrowConnectionTimeout;
  }
 
  public int getLoginTimeout() {
    return loginTimeout;
  }
 
  public void setLoginTimeout(int loginTimeout) {
    this.loginTimeout = loginTimeout;
  }
 
  public int getMaintenanceInterval() {
    return maintenanceInterval;
  }
 
  public void setMaintenanceInterval(int maintenanceInterval) {
    this.maintenanceInterval = maintenanceInterval;
  }
 
  public int getMaxIdleTime() {
    return maxIdleTime;
  }
 
  public void setMaxIdleTime(int maxIdleTime) {
    this.maxIdleTime = maxIdleTime;
  }
 
  public String getTestQuery() {
    return testQuery;
  }
 
  public void setTestQuery(String testQuery) {
    this.testQuery = testQuery;
  }
 
}

然后创建两个数据源RuanMyBatisConfig和YouMyBatisConfig,注意@Primary注解只能有一个。

package com.cgb.datasource;
 
import java.sql.SQLException;
 
import javax.sql.DataSource;
 
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.cgb.config.DBConfig1;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
 
@Configuration
@MapperScan(basePackages = "com.cgb.ruan", sqlSessionTemplateRef = "testSqlSessionTemplate")
public class RuanMyBatisConfig {
 
  // 配置数据源
  @Primary
  @Bean(name = "dataSource1")
  public DataSource testDataSource(DBConfig1 testConfig) throws SQLException {
    MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
    mysqlXaDataSource.setUrl(testConfig.getUrl());
    mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
    mysqlXaDataSource.setPassword(testConfig.getPassword());
    mysqlXaDataSource.setUser(testConfig.getUsername());
    mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
 
    AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
    xaDataSource.setXaDataSource(mysqlXaDataSource);
    xaDataSource.setUniqueResourceName("dataSource1");
 
    xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());
    xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());
    xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());
    xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());
    xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());
    xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());
    xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());
    xaDataSource.setTestQuery(testConfig.getTestQuery());
    return xaDataSource;
  }
 
  @Bean(name = "testSqlSessionFactory")
  public SqlSessionFactory testSqlSessionFactory(@Qualifier("dataSource1") DataSource dataSource)
      throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    return bean.getObject();
  }
 
  @Bean(name = "testSqlSessionTemplate")
  public SqlSessionTemplate testSqlSessionTemplate(
      @Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
  }
}

其实在多个数据源的时候,我们怎么去指定数据库呢?

其中一个做法是写注解,表明使用哪个数据库,但是这种是不是很麻烦。最好的做法是分包管理:

好啦,大功告成,我们来看看效果吧。

我们发现控制台打印添加学生成功,好我们看看数据库里有没有数据呢?

毫无疑问是没有的,说明事务起作用了。那我们把那行异常代码注释掉,再看看效果。成功了,去看看数据库有没有呢。

ojbk,想想同时操作多个数据库,是不是很爽啊,哈哈哈。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 基于Docker的K8s(Kubernetes)集群部署方案

    基于Docker的K8s(Kubernetes)集群部署方案

    这篇文章主要介绍了基于Docker的K8s(Kubernetes)集群部署方案,文中介绍了安装k8s的可视化界面的相关操作,需要的朋友可以参考下
    2024-01-01
  • 利用idea搭建SSM项目看这一篇就够了

    利用idea搭建SSM项目看这一篇就够了

    SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容),常作为数据源较简单的web项目的框架,下面这篇文章主要给大家介绍了关于利用idea搭建SSM项目的相关资料,需要的朋友可以参考下
    2023-05-05
  • springboot下添加全局异常处理和自定义异常处理的过程解析

    springboot下添加全局异常处理和自定义异常处理的过程解析

    在spring项目中,优雅处理异常,好处是可以将系统产生的全部异常统一捕获处理,自定义的异常也由全局异常来捕获,如果涉及到validator参数校验器使用全局异常捕获也是较为方便,这篇文章主要介绍了springboot下添加全局异常处理和自定义异常处理,需要的朋友可以参考下
    2023-12-12
  • 简单讲解Java设计模式编程中的单一职责原则

    简单讲解Java设计模式编程中的单一职责原则

    这篇文章主要介绍了Java设计模式编程中的单一职责原则,这在团队开发编写接口时经常使用这样的约定,需要的朋友可以参考下
    2016-02-02
  • 解决SpringMVC @RequestMapping不设置value出现的问题

    解决SpringMVC @RequestMapping不设置value出现的问题

    这篇文章主要介绍了解决SpringMVC @RequestMapping不设置value出现的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java程序的初始化顺序,static{}静态代码块和实例语句块的使用方式

    Java程序的初始化顺序,static{}静态代码块和实例语句块的使用方式

    这篇文章主要介绍了Java程序的初始化顺序,static{}静态代码块和实例语句块的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • spring cloud gateway中配置uri三种方式

    spring cloud gateway中配置uri三种方式

    gateway 组件是SpringCloud 组件中的网关组件,主要是解决路由转发的问题,跟nginx有点类似,区别是nginx多用在前端上,gateway用在后端上,本文给大家介绍的非常详细,需要的朋友参考下吧
    2023-08-08
  • java中Struts2文件上传问题详解

    java中Struts2文件上传问题详解

    文件上传和文件下载是我们在web应用程序中常用的两个功能,在java中,实现这两种功能的方式也有很多种,其中struts2就给我们提供了一种算是比较简单的方式吧,下面我们就一起来看一下,
    2015-07-07
  • Intellij IDEA集成JProfiler性能分析工具

    Intellij IDEA集成JProfiler性能分析工具

    作为Java程序员,性能分析是我们必须掌握的技能之一,在性能分析中,JProfiler是一款非常强大的工具,本文就来介绍一下Intellij IDEA集成JProfiler性能分析工具,就有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • spring 集成 mybatis的实例详解

    spring 集成 mybatis的实例详解

    这篇文章主要介绍了spring 集成 mybatis的实例详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01

最新评论