這篇文章將為大家詳細(xì)講解有關(guān)springboot jta atomikos如何實(shí)現(xiàn)分布式事物管理,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
當(dāng)項(xià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í)體類就是存放兩個數(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í)在多個數(shù)據(jù)源的時候,我們怎么去指定數(shù)據(jù)庫呢?
其中一個做法是寫注解,表明使用哪個數(shù)據(jù)庫,但是這種是不是很麻煩。最好的做法是分包管理:
好啦,大功告成,我們來看看效果吧。
我們發(fā)現(xiàn)控制臺打印添加學(xué)生成功,好我們看看數(shù)據(jù)庫里有沒有數(shù)據(jù)呢?
毫無疑問是沒有的,說明事務(wù)起作用了。那我們把那行異常代碼注釋掉,再看看效果。成功了,去看看數(shù)據(jù)庫有沒有呢。
關(guān)于“springboot jta atomikos如何實(shí)現(xiàn)分布式事物管理”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站m.newbst.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站欄目:springbootjtaatomikos如何實(shí)現(xiàn)分布式事物管理-創(chuàng)新互聯(lián)
文章出自:http://m.newbst.com/article42/cegghc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站改版、虛擬主機(jī)、服務(wù)器托管、品牌網(wǎng)站制作、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)