重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

springboot中怎么利用jtaatomikos实现分布式事物管理

这期内容当中小编将会给大家带来有关springboot中怎么利用jtaatomikos实现分布式事物管理,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联公司是专业的茂名网站建设公司,茂名接单;提供网站建设、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行茂名网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

第一步添加atomikos的依赖

org.springframework.bootspring-boot-starter-jta-atomikos

第二步配置数据源,我这里有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: truelogging: 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中怎么利用jtaatomikos实现分布式事物管理了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


文章题目:springboot中怎么利用jtaatomikos实现分布式事物管理
文章起源:http://cqcxhl.cn/article/pjgsoi.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP