欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot動(dòng)態(tài)數(shù)據(jù)源+分布式事務(wù)的實(shí)現(xiàn)

 更新時(shí)間:2025年09月12日 10:18:03   作者:wenzheng_du  
本文主要介紹了springboot動(dòng)態(tài)數(shù)據(jù)源+分布式事務(wù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.引入jta-atomikos

這個(gè)springboot 是自帶的。我的springboot版本為2.5.9,數(shù)據(jù)庫(kù)為mysql。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jta-atomikos</artifactId>
        </dependency>

2.配置數(shù)據(jù)源(application.yml)

spring:
    # 數(shù)據(jù)源配置
    jta:
        enabled: true
    datasource:
        master:
            # 主庫(kù)數(shù)據(jù)源
            url: 
            username: 
            password: 
            driver-class-name: com.mysql.cj.jdbc.Driver
        slave:
            # 從庫(kù)數(shù)據(jù)源
            url: 
            username: 
            password: 
            driver-class-name: com.mysql.cj.jdbc.Driver

新建文件 DataSourceConfig ,配置數(shù)據(jù)源及線程池。你有幾個(gè)數(shù)據(jù)源就建立幾個(gè)XXXDataSource方法,最后放到dynamicDataSource方法中的Map中去。事務(wù)管理一定要用JtaTransactionManager。SqlSessionFactory 是可以不用寫的,如果要寫的話,如果你用了mybatis-plus,一定要用MybatisSqlSessionFactoryBean,不然在啟動(dòng)或者調(diào)用Mybatis-plus的方法時(shí),會(huì)報(bào)找不到bean的錯(cuò)。

import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;

import javax.sql.DataSource;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Aspect
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {

    @Autowired
    private Environment env;

    @Bean(name = "masterDataSource")
    public DataSource masterDataSource() {
        AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        ds.setUniqueResourceName("master");
        ds.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource");
        ds.setXaProperties(getXaProperties("master"));
        ds.setMaxPoolSize(10);
        ds.setMinPoolSize(5);
        ds.setBorrowConnectionTimeout(60);
        return ds;
    }

    @Bean(name = "slaveDataSource")
    public DataSource slaveDataSource() {
        AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        ds.setUniqueResourceName("slave");
        ds.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource");
        ds.setXaProperties(getXaProperties("slave"));
        ds.setMaxPoolSize(10);
        ds.setMinPoolSize(5);
        ds.setBorrowConnectionTimeout(60);
        return ds;
    }

    @Bean
    @Primary
    public DataSource dynamicDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
                                        @Qualifier("slaveDataSource") DataSource slaveDataSource) {
        DynamicDataSource dataSource = new DynamicDataSource();
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("master", masterDataSource);
        targetDataSources.put("slave", slaveDataSource);
        dataSource.setTargetDataSources(targetDataSources);
        dataSource.setDefaultTargetDataSource(masterDataSource);
        return dataSource;
    }

    @Bean
    public UserTransaction userTransaction() throws SystemException {
        UserTransactionImp userTransactionImp = new UserTransactionImp();
        userTransactionImp.setTransactionTimeout(10000);
        return userTransactionImp;
    }

    @Bean(initMethod = "init", destroyMethod = "close")
    public UserTransactionManager userTransactionManager() {
        UserTransactionManager userTransactionManager = new UserTransactionManager();
        userTransactionManager.setForceShutdown(false);
        return userTransactionManager;
    }

    @Bean
    public PlatformTransactionManager transactionManager(UserTransaction userTransaction,
                                                         UserTransactionManager userTransactionManager) {
        JtaTransactionManager jtaTransactionManager = new JtaTransactionManager();
        jtaTransactionManager.setUserTransaction(userTransaction);
        jtaTransactionManager.setTransactionManager(userTransactionManager);
        return jtaTransactionManager;
    }

    private Properties getXaProperties(String dataSourceType) {
        Properties xaProps = new Properties();
        String username = getPropertyValue("spring.datasource." + dataSourceType + ".username");
        String password = getPropertyValue("spring.datasource." + dataSourceType + ".password");
        String url = getPropertyValue("spring.datasource." + dataSourceType + ".url");
        xaProps.put("user", username);
        xaProps.put("password", password);
        xaProps.put("url", url);
        return xaProps;
    }

    private String getPropertyValue(String str) {
        return env.getProperty(str);
    }

}

新建文件DynamicDataSource,這個(gè)類繼承AbstractRoutingDataSource。

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        String dataSourceKey = DataSourceContextHolder.getDataSource();
        if (dataSourceKey == null) {
            return "master"; // 默認(rèn)數(shù)據(jù)源
        }
        return dataSourceKey;
    }
}

建立一個(gè)上下文DataSourceContextHolder文件,用于讀取當(dāng)前數(shù)據(jù)源。

public class DataSourceContextHolder {

    public static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void setDataSource(String dataSourceName) {
        threadLocal.set(dataSourceName);
    }

    public static String getDataSource() {
        return threadLocal.get();
    }

    public static void clearDataSource() {
        threadLocal.remove();
    }
}

3.配置注解

這個(gè)沒啥好說的,文件名自己定義都可以。注意后面的默認(rèn)數(shù)據(jù)源參數(shù),配置了的話一定要是你的。

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MDS {

    String value() default "master";

}

4.切面

在類上使用上面的自定義注解。然后用切面去切換數(shù)據(jù)源。這里我用了3個(gè)判斷,首先會(huì)先判斷方法上有沒有自定義注解,其次類上,都沒有就用默認(rèn)的數(shù)據(jù)源。

import cn.hutool.core.annotation.AnnotationUtil;
import com.rs.common.annotation.MDS;
import com.rs.framework.config.dynamic.DataSourceContextHolder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Order(0)
@Component
public class DataSourceAspect {

    @Before("@within(mds) || @annotation(mds)")
    public void before(JoinPoint joinPoint, MDS mds) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Class<?> targetClass = method.getDeclaringClass();
        Object value = AnnotationUtil.getAnnotationValue(targetClass, MDS.class);
        if(mds != null && mds.value() != null){
            DataSourceContextHolder.setDataSource(mds.value());
        } else if (value != null) {
            DataSourceContextHolder.setDataSource((String) value);
        } else {
            DataSourceContextHolder.setDataSource("master");
        }
    }

    @AfterReturning(pointcut = "@within(mds) ||@annotation(mds)", returning = "returnVal")
    public void afterReturning(JoinPoint joinPoint, MDS mds, Object returnVal) {
        DataSourceContextHolder.clearDataSource();
    }

    @AfterThrowing(pointcut = "@within(mds) ||@annotation(mds)", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, MDS mds, Throwable ex) {
        DataSourceContextHolder.clearDataSource();
    }

}

5.如何使用

一個(gè)方法中執(zhí)行不同數(shù)據(jù)源操作。getUserPageList方法使用的是默認(rèn)數(shù)據(jù)源master,即最后的返回是返回master的查詢結(jié)果。updateBusinessStatus方法使用的是從庫(kù)數(shù)據(jù)源,在執(zhí)行完sql后我寫了一個(gè)異常。

這里需要注意的是,一定要寫@Transactional(rollbackFor = Exception.class),且是寫在方法上!??!別為了省事寫在類上,寫在類上發(fā)生異常不會(huì)回滾(親測(cè))。

    @Override
    @Transactional(rollbackFor = Exception.class)
    public IPage<SysUserDto> getUserPageList(SysUser user, Page<SysUser> page) {
        user = new SysUser();
        user.setUserId(29L);
        user.setDelFlag("0");
        sysUserMapper.updateById(user);
        flwProcessService.updateBusinessStatus("1851531692768452608",3);
        return sysUserMapper.getUserPageList(user,page.page());
    }

    @Override
    @MDS("slave")
    @Transactional(rollbackFor = Exception.class)
    public void updateBusinessStatus(String procInstId, Integer businessStatus) {
        flwProcessMapper.update(null,new UpdateWrapper<FlwProcess>().lambda()
                .eq(FlwProcess::getProcInstId,procInstId)
                .set(FlwProcess::getBusinessStatus,businessStatus)
        );
        int i = 1/0;
    }

注意事項(xiàng)

一個(gè)方法不同數(shù)據(jù)源的操作@Transactional(rollbackFor = Exception.class)寫在類上?。。?/p>

一個(gè)方法不同數(shù)據(jù)源的操作@Transactional(rollbackFor = Exception.class)寫在類上?。?!

一個(gè)方法不同數(shù)據(jù)源的操作@Transactional(rollbackFor = Exception.class)寫在類上!?。?/p>

到此這篇關(guān)于springboot動(dòng)態(tài)數(shù)據(jù)源+分布式事務(wù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot動(dòng)態(tài)數(shù)據(jù)源+分布式事務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java實(shí)現(xiàn)RTF文件查看器(附帶源碼)

    java實(shí)現(xiàn)RTF文件查看器(附帶源碼)

    這篇文章主要介紹了一個(gè)基于?Java?Swing?的?RTF?文件查看器,既可作為輕量級(jí)桌面應(yīng)用,也可嵌入到更大規(guī)模的管理系統(tǒng)中,滿足用戶對(duì)?RTF?文檔的即時(shí)預(yù)覽、樣式保真和簡(jiǎn)單導(dǎo)航需求
    2025-06-06
  • 使用Maven打包、發(fā)布、配置版本號(hào)命令

    使用Maven打包、發(fā)布、配置版本號(hào)命令

    在軟件開發(fā)過程中,打包和發(fā)布是關(guān)鍵步驟,本文介紹了如何在打包和發(fā)布時(shí)跳過測(cè)試,如何指定項(xiàng)目版本號(hào),以及如何指定配置文件,提供了實(shí)用的技巧和方法,希望對(duì)開發(fā)者有所幫助
    2024-09-09
  • 詳細(xì)介紹Java關(guān)鍵字throw?throws?Throwable的用法與區(qū)別

    詳細(xì)介紹Java關(guān)鍵字throw?throws?Throwable的用法與區(qū)別

    這篇文章主要介紹了java中throws與throw及Throwable的用法和區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Java中的觀察者模式實(shí)例講解

    Java中的觀察者模式實(shí)例講解

    這篇文章主要介紹了Java中的觀察者模式實(shí)例講解,本文先是講解了觀察者模式的概念,然后以實(shí)例講解觀察者模式的實(shí)現(xiàn),以及給出了UML圖,需要的朋友可以參考下
    2014-12-12
  • springboot中@RequestMapping的用法

    springboot中@RequestMapping的用法

    這篇文章主要介紹了springboot中@RequestMapping的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 如何在logback日志配置里獲取服務(wù)器ip和端口

    如何在logback日志配置里獲取服務(wù)器ip和端口

    這篇文章主要介紹了如何在logback日志配置里獲取服務(wù)器ip和端口的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java使用OSS實(shí)現(xiàn)上傳文件功能

    Java使用OSS實(shí)現(xiàn)上傳文件功能

    這篇文章主要為大家詳細(xì)介紹了Java如何使用OSS實(shí)現(xiàn)上傳文件功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下
    2024-01-01
  • 詳解Spring循環(huán)依賴的解決方案

    詳解Spring循環(huán)依賴的解決方案

    這篇文章主要介紹了詳解Spring循環(huán)依賴的解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • 詳解Nacos中注冊(cè)中心和配置中心的實(shí)現(xiàn)

    詳解Nacos中注冊(cè)中心和配置中心的實(shí)現(xiàn)

    Spring?Cloud?Alibaba?是阿里巴巴提供的一站式微服務(wù)開發(fā)解決方案。而?Nacos?作為?Spring?Cloud?Alibaba?的核心組件之一,提供了兩個(gè)非常重要的功能:注冊(cè)中心和配置中心,我們今天來了解和實(shí)現(xiàn)一下二者
    2022-08-08
  • 詳解Java的JDBC API的存儲(chǔ)過程與SQL轉(zhuǎn)義語(yǔ)法的使用

    詳解Java的JDBC API的存儲(chǔ)過程與SQL轉(zhuǎn)義語(yǔ)法的使用

    這篇文章主要介紹了詳解Java的JDBC API的存儲(chǔ)過程與SQL轉(zhuǎn)義語(yǔ)法的使用,JDBC是Java用于連接使用各種數(shù)據(jù)庫(kù)的API,需要的朋友可以參考下
    2015-12-12

最新評(píng)論