springboot jta atomikos實現(xiàn)分布式事物管理
這篇文章主要介紹了springboot jta atomikos實現(xiàn)分布式事物管理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
當(dāng)項目在連接多個數(shù)據(jù)庫時可能會發(fā)生事務(wù)問題,即一個庫的事務(wù)不可能去操作另一個數(shù)據(jù)庫的事務(wù),這時就需要使用atomikos對數(shù)據(jù)庫的事務(wù)進(jìn)行統(tǒng)一的管理
第一步添加atomikos的依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jta-atomikos</artifactId> </dependency>
第二步配置數(shù)據(jù)源,我這里有2個數(shù)據(jù)庫(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
然后創(chuàng)建DBConfig1和DBConfig2,這兩個實體類就是存放兩個數(shù)據(jù)源的數(shù)據(jù)的。
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;
}
}
然后創(chuàng)建兩個數(shù)據(jù)源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 {
// 配置數(shù)據(jù)源
@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);
}
}
其實在多個數(shù)據(jù)源的時候,我們怎么去指定數(shù)據(jù)庫呢?
其中一個做法是寫注解,表明使用哪個數(shù)據(jù)庫,但是這種是不是很麻煩。最好的做法是分包管理:
好啦,大功告成,我們來看看效果吧。
我們發(fā)現(xiàn)控制臺打印添加學(xué)生成功,好我們看看數(shù)據(jù)庫里有沒有數(shù)據(jù)呢?
毫無疑問是沒有的,說明事務(wù)起作用了。那我們把那行異常代碼注釋掉,再看看效果。成功了,去看看數(shù)據(jù)庫有沒有呢。
ojbk,想想同時操作多個數(shù)據(jù)庫,是不是很爽啊,哈哈哈。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java執(zhí)行SQL語句實現(xiàn)查詢的通用方法詳解
這篇文章主要介紹了java執(zhí)行SQL語句實現(xiàn)查詢的通用方法詳解,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
springboot如何讀取配置文件(application.yml)中的屬性值
本篇文章主要介紹了springboot如何讀取配置文件(application.yml)中的屬性值,具有一定的參考價值,有興趣的小伙伴可以了解一下2017-04-04
SSH框架網(wǎng)上商城項目第6戰(zhàn)之基于DataGrid的數(shù)據(jù)顯示
SSH框架網(wǎng)上商城項目第6戰(zhàn)之基于DataGrid的數(shù)據(jù)顯示,提供了豐富的選擇、排序、分組和編輯數(shù)據(jù)的功能支持,感興趣的小伙伴們可以參考一下2016-05-05
Mybatis中l(wèi)ike搭配concat的寫法詳解
這篇文章主要介紹了Mybatis中l(wèi)ike搭配concat的寫法詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
IDEA mybatis-generator逆向工程生成代碼
這篇文章主要介紹了IDEA mybatis-generator逆向工程生成代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
SpringBoot框架集成ElasticSearch實現(xiàn)過程示例詳解
這篇文章主要為大家介紹了SpringBoot如何集成ElasticSearch的實現(xiàn)過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
RocketMq同組消費者如何自動設(shè)置InstanceName
這篇文章主要介紹了RocketMq同組消費者如何自動設(shè)置InstanceName問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
通過AOP攔截Spring?Boot日志并將其存入數(shù)據(jù)庫功能實現(xiàn)
本文介紹了如何使用Spring Boot和AOP技術(shù)實現(xiàn)攔截系統(tǒng)日志并保存到數(shù)據(jù)庫中的功能,包括配置數(shù)據(jù)庫連接、定義日志實體類、定義日志攔截器、使用AOP攔截日志并保存到數(shù)據(jù)庫中等步驟,感興趣的朋友一起看看吧2023-08-08

