springbootjtaatomikos如何实现分布式事物管理-创新互联

这篇文章将为大家详细讲解有关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);
  }
}

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

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

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

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

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

关于“springboot jta atomikos如何实现分布式事物管理”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网站栏目:springbootjtaatomikos如何实现分布式事物管理-创新互联
文章出自:https://www.cdcxhl.com/article42/cegghc.html

成都网站建设公司_创新互联,为您提供网站内链网站改版虚拟主机服务器托管品牌网站制作小程序开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设