最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中的多個(gè)事務(wù)管理詳解

 更新時(shí)間:2023年10月25日 10:38:03   作者:刻苦的樊同學(xué)  
這篇文章主要介紹了SpringBoot中的多個(gè)事務(wù)管理詳解,事務(wù)管理是一種組織和協(xié)調(diào)各種活動(dòng)和資源的方法,以實(shí)現(xiàn)特定目標(biāo),它涉及規(guī)劃、執(zhí)行和監(jiān)控各種任務(wù),以確保項(xiàng)目或組織的順利運(yùn)行,需要的朋友可以參考下

springboot多個(gè)事務(wù)管理

導(dǎo)入依賴

<!-- jta 管理多個(gè)數(shù)據(jù)源的事務(wù)-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>

在application.properties配置文件中對(duì)兩個(gè)數(shù)據(jù)庫(kù)的配置內(nèi)容稍作修改。

mysql.datasource.test1.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
mysql.datasource.test1.username = root
mysql.datasource.test1.password = 123456

mysql.datasource.test1.minPoolSize = 3
mysql.datasource.test1.maxPoolSize = 25
mysql.datasource.test1.maxLifetime = 20000
mysql.datasource.test1.borrowConnectionTimeout = 30
mysql.datasource.test1.loginTimeout = 30
mysql.datasource.test1.maintenanceInterval = 60
mysql.datasource.test1.maxIdleTime = 60

mysql.datasource.test1.testQuery = select 1
# Mysql 2
mysql.datasource.test2.url =jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8
mysql.datasource.test2.username =root
mysql.datasource.test2.password =123456
mysql.datasource.test2.minPoolSize = 3
mysql.datasource.test2.maxPoolSize = 25
mysql.datasource.test2.maxLifetime = 20000
mysql.datasource.test2.borrowConnectionTimeout = 30
mysql.datasource.test2.loginTimeout = 30
mysql.datasource.test2.maintenanceInterval = 60
mysql.datasource.test2.maxIdleTime = 60
mysql.datasource.test2.testQuery = select 1

在java文件夾下新建dbconfig包

里面分別寫兩個(gè)類和application.properties配置文件的信息相對(duì)應(yīng),配置文件中共配置了兩個(gè)數(shù)據(jù)源,那么dbconfig包中就需要寫兩個(gè)類,分別是DBconfig1和DBconfig2,下面省略了set和get方法。

@ConfigurationProperties("mysql.datasource.test1")
public class DBConfig1 {
    private String 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;
}
@ConfigurationProperties("mysql.datasource.test2")
public class DBConfig2 {
    private String 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;
}

在上篇文章中已經(jīng)配置了數(shù)據(jù)源的兩個(gè)配置類,這里需要修改一下。

@Configuration//注解到springboot容器中
@MapperScan(basePackages="com.gyf.test1.mapper",sqlSessionFactoryRef="test1SqlSessionFactory")
public class DataSource01 {

    // 配置數(shù)據(jù)源
    @Primary
    @Bean(name = "test1DataSource")
    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("test1DataSource");

        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 = "test1SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "test1SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
@Configuration//注解到springboot容器中
@MapperScan(basePackages="com.gyf.test2.mapper",sqlSessionFactoryRef="test2SqlSessionFactory")
public class DataSource02 {

    // 配置數(shù)據(jù)源
    @Bean(name = "test2DataSource")
    public DataSource testDataSource(DBConfig2 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("test2DataSource");
        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 = "test2SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }
    @Bean(name = "test2SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

啟動(dòng)類加上注解配置

@EnableAutoConfiguration
//@ComponentScan(basePackages = {"com.gyf.dbconfig","com.gyf.datasource","com.gyf.web","com.gyf.test1.service","com.gyf.test2.service"})
@ComponentScan(basePackages = {"com.gyf"})
@EnableConfigurationProperties(value = {DBConfig1.class, DBConfig2.class})
public class App
{
    public static void main( String[] args )
    {
        //啟動(dòng)springboot項(xiàng)目
        SpringApplication.run(App.class,args);
    }
}

在這里插入圖片描述

到此這篇關(guān)于SpringBoot中的多個(gè)事務(wù)管理詳解的文章就介紹到這了,更多相關(guān)SpringBoot多事務(wù)管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java集合快速構(gòu)建成樹形json實(shí)例

    java集合快速構(gòu)建成樹形json實(shí)例

    文章介紹了如何使用Java構(gòu)建樹形結(jié)構(gòu)的JSON,并實(shí)現(xiàn)了一個(gè)代碼工具來(lái)簡(jiǎn)化這一過(guò)程,該工具使用類適配器模式,允許靈活切換樹形結(jié)構(gòu),且無(wú)需修改實(shí)體類代碼,提高了代碼的可維護(hù)性和擴(kuò)展性
    2025-11-11
  • 詳解application.properties和application.yml文件的區(qū)別

    詳解application.properties和application.yml文件的區(qū)別

    這篇文章主要介紹了詳解application.properties和application.yml文件的區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • 使用MyBatis TypeHandler實(shí)現(xiàn)數(shù)據(jù)加密與解密的具體方案

    使用MyBatis TypeHandler實(shí)現(xiàn)數(shù)據(jù)加密與解密的具體方案

    在我們?nèi)粘5拈_發(fā)工作中,經(jīng)常會(huì)遇到一些敏感數(shù)據(jù)需要存儲(chǔ),比如用戶的手機(jī)號(hào)、身份證號(hào)、銀行卡號(hào)等,為了保障數(shù)據(jù)安全,我們通常會(huì)對(duì)這些敏感信息進(jìn)行加密后再存入數(shù)據(jù)庫(kù),所以本文給大家介紹了如何使用MyBatis TypeHandler實(shí)現(xiàn)數(shù)據(jù)加密與解密,需要的朋友可以參考下
    2025-11-11
  • java中ThreadLocalRandom的使用詳解

    java中ThreadLocalRandom的使用詳解

    這篇文章主要介紹了java中ThreadLocalRandom的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • java多線程編程之管道通信詳解

    java多線程編程之管道通信詳解

    這篇文章主要為大家詳細(xì)介紹了java多線程編程之線程間的通信,探討使用管道進(jìn)行通信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Java優(yōu)化if-else代碼的實(shí)戰(zhàn)記錄

    Java優(yōu)化if-else代碼的實(shí)戰(zhàn)記錄

    開發(fā)中經(jīng)常會(huì)根據(jù)不同的參數(shù)判斷走不同的邏輯業(yè)務(wù),我們常用的方法就是if/else嵌套使用,導(dǎo)致每增加一個(gè)需求就加一個(gè)if,慢慢的就會(huì)發(fā)現(xiàn)自己寫的代碼中出現(xiàn)了大量的if/else,這篇文章主要給大家介紹了關(guān)于Java優(yōu)化if-else代碼的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • Java中自定義注解類及使用實(shí)例解析

    Java中自定義注解類及使用實(shí)例解析

    這篇文章主要介紹了Java中自定義注解類并使用過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • springSecurity之如何添加自定義過(guò)濾器

    springSecurity之如何添加自定義過(guò)濾器

    這篇文章主要介紹了springSecurity之如何添加自定義過(guò)濾器的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • ThreadPoolTaskExecutor原理解析及使用實(shí)踐

    ThreadPoolTaskExecutor原理解析及使用實(shí)踐

    Spring框架中ThreadPoolTaskExecutor的使用,包括其核心原理、關(guān)鍵配置參數(shù)、使用場(chǎng)景、代碼示例以及注意事項(xiàng),ThreadPoolTaskExecutor通過(guò)封裝Java原生線程池,簡(jiǎn)化了異步任務(wù)執(zhí)行與線程池管理,支持與Spring生態(tài)的無(wú)縫集成,適用于各種異步任務(wù)處理場(chǎng)景
    2026-01-01
  • 解決Beanutils.copyproperties實(shí)體類對(duì)象不一致的問(wèn)題

    解決Beanutils.copyproperties實(shí)體類對(duì)象不一致的問(wèn)題

    這篇文章主要介紹了解決Beanutils.copyproperties實(shí)體類對(duì)象不一致的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評(píng)論

那坡县| 紫阳县| 腾冲县| 日照市| 鞍山市| 上栗县| 乌拉特中旗| 五原县| 鸡东县| 台南县| 武川县| 蒲城县| 古蔺县| 南投县| 遂川县| 庆安县| 伽师县| 安阳县| 灵寿县| 梓潼县| 安达市| 汉中市| 宣城市| 都兰县| 壤塘县| 九江市| 武山县| 上思县| 兴山县| 吴旗县| 安图县| 慈利县| 镇原县| 北宁市| 上杭县| 易门县| 东宁县| 横峰县| 东方市| 江达县| 九江市|