springboot多数据源多事务配置

springboot多数据源多事务配置

必须有一个主数据源, @Primary注解。


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.gb.soa.omp.ccommon.util.MyJdbcTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: Stud
 * @create: 2021-08-25 16:46
 **/
@Component
@Configuration
public class ActivitiDataSourceConfiguration {
    @Primary
    @Bean(name = "activitiDataSource")
    @ConfigurationProperties("spring.datasource.druid.activiti")
    public DruidDataSource activitiDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "activitiJdbcTemplate")
    public MyJdbcTemplate activitiJdbcTemplate() {
        MyJdbcTemplate jdbcTemplate = new MyJdbcTemplate();
        jdbcTemplate.setDataSource(activitiDataSource());
        //jdbcTemplate.setDbAnnotatePrefix(dbAnnotatePrefix);
        return jdbcTemplate;
    }

    @Primary
    @Bean(name = "activitiTransactionManager")
    public DataSourceTransactionManager activitiTransactionManager(@Qualifier("activitiDataSource") DruidDataSource druidDataSource) {
        DataSourceTransactionManager tm = new DataSourceTransactionManager(druidDataSource);
//        tm.setDataSource(activitiDataSource());
        tm.setNestedTransactionAllowed(true);
        return tm;
    }
}

其他数据源配置


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.gb.soa.omp.ccommon.util.MyJdbcTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: Stud
 * @create: 2021-08-25 16:46
 **/
@Component
@Configuration
public class PlatformDataSourceConfiguration {
    @Bean(name = "platformDataSource")
    @ConfigurationProperties("spring.datasource.druid.platform")
    public DruidDataSource platformDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "platformJdbcTemplate")
    public MyJdbcTemplate platformJdbcTemplate() {
        MyJdbcTemplate jdbcTemplate = new MyJdbcTemplate();
        jdbcTemplate.setDataSource(platformDataSource());
        //jdbcTemplate.setDbAnnotatePrefix(dbAnnotatePrefix);
        return jdbcTemplate;
    }

    @Bean(name = "platformTransactionManager")
    public DataSourceTransactionManager platformTransactionManager(@Qualifier("platformDataSource") DruidDataSource druidDataSource) {
        DataSourceTransactionManager tm = new DataSourceTransactionManager(druidDataSource);
//        tm.setDataSource(platformDataSource());
        tm.setNestedTransactionAllowed(true);
        return tm;
    }
}

测试类


import com.gb.soa.omp.ccommon.util.MyJdbcTemplate;
import com.gb.soa.omp.ccommon.util.TransactionUtil;
import com.ykcloud.soa.erp.activiti.ActivitiMain;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;

/**
 * @author tz.x
 * @date 2021/7/13 13:23
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ActivitiMain.class)
public class ActivitiWorkServiceTest {


    @Resource(name = "platformTransactionManager")
    private PlatformTransactionManager platformTransactionManager;

    @Resource(name = "platformJdbcTemplate")
    private MyJdbcTemplate platformJdbcTemplate;

    @Resource(name = "activitiTransactionManager")
    private DataSourceTransactionManager activitiTransactionManager;

    @Resource(name = "activitiJdbcTemplate")
    private MyJdbcTemplate activitiJdbcTemplate;

    @Test
    public void test1() {
        TransactionStatus txStatus1 = platformTransactionManager.getTransaction(TransactionUtil.newTransactionDefinition(300));
        try {
            update1();
            platformTransactionManager.commit(txStatus1);
        } catch (Exception e) {
            platformTransactionManager.rollback(txStatus1);
        }

        TransactionStatus txStatus2 = activitiTransactionManager.getTransaction(TransactionUtil.newTransactionDefinition(300));
        try {
            update2();
            activitiTransactionManager.commit(txStatus2);
        } catch (Exception e) {
            activitiTransactionManager.rollback(txStatus2);
        }

    }

    @Test
    public void test2() {
        TransactionStatus txStatus1 = platformTransactionManager.getTransaction(TransactionUtil.newTransactionDefinition(300));
        update11();
        platformTransactionManager.rollback(txStatus1);

        TransactionStatus txStatus2 = activitiTransactionManager.getTransaction(TransactionUtil.newTransactionDefinition(300));
        update22();
        activitiTransactionManager.rollback(txStatus2);

    }


    public void update1() {
        String sql = " update t_ru_workflow set proc_inst_id = '9527' where series = '18776680548' ";
        platformJdbcTemplate.update(sql);
        throw new RuntimeException("update111111111111111111报错");
    }

    public void update2() {
        String sql = " update act_hi_actinst set ASSIGNEE_ = null where ACT_TYPE_ = 'startEvent' ";
        activitiJdbcTemplate.update(sql);
        throw new RuntimeException("update222222222222222222报错");
    }

    public void update11() {
        String sql = " update t_ru_workflow set proc_inst_id = '9527' where series = '18776680548' ";
        platformJdbcTemplate.update(sql);
    }

    public void update22() {
        String sql = " update act_hi_actinst set ASSIGNEE_ = null where ACT_TYPE_ = 'startEvent' ";
        activitiJdbcTemplate.update(sql);
    }

}


  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页
评论

打赏作者

码农20年

给我你就上当了

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值