spring boot 多数据源 + 事务控制

1,首先在启动类加上@EnableTransactionManagement注解

   

复制代码
package cn.bforce.common;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@ComponentScan(basePackages={"cn.bforce.common"})  
@EnableCaching
@EnableTransactionManagement
public class YuntuSysBaseApplication {

    public static void main(String[] args) {
        SpringApplication.run(YuntuSysBaseApplication.class, args);
    }
}
复制代码

2,application.properties文件配置的双数据源文件配置

  

复制代码
#datasource b-force
spring.datasource.bf.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.bf.url=jdbc:mysql://192.168.18.221:3306/b-force?characterEncoding=utf8&useSSL=true
spring.datasource.bf.username=root
spring.datasource.bf.password=root

spring.datasource.bfscrm.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.bfscrm.url=jdbc:mysql://192.168.18.221:3306/b-force-scrm?characterEncoding=utf8&useSSL=true
spring.datasource.bfscrm.username=root
spring.datasource.bfscrm.password=root
复制代码

3,JavaConfig 首先建立Java配置类,为其添加上注解@Configuration。并实现如下方法。

复制代码
package cn.bforce.common.persistence.datasource;


import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
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.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;


@Configuration
public class GlobalDataConfiguration
{
    @Bean(name = "bfDataSource")
    @Qualifier("bfDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.bf")
    public DataSource primaryDataSource()
    {
        System.out.println("-------------------- bfDataSource init ---------------------");
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "bfscrmDataSource")
    @Qualifier("bfscrmDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.bfscrm")
    public DataSource secondaryDataSource()
    {
        System.out.println("-------------------- bfscrmDataSource init ---------------------");
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "bfJdbcTemplate")
    public JdbcTemplate bfJdbcTemplate(@Qualifier("bfDataSource") DataSource dataSource)
    {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "bfscrmJdbcTemplate")
    public JdbcTemplate bfscrmscrmJdbcTemplate(@Qualifier("bfscrmDataSource") DataSource dataSource)
    {
        return new JdbcTemplate(dataSource);
    }
    
    /******配置事务管理********/
    
    @Bean
    public PlatformTransactionManager bfTransactionManager(@Qualifier("bfDataSource")DataSource prodDataSource) {
     return new DataSourceTransactionManager(prodDataSource);
    }
     
    @Bean
    public PlatformTransactionManager bfscrmTransactionManager(@Qualifier("bfscrmDataSource")DataSource sitDataSource) {
     return new DataSourceTransactionManager(sitDataSource);
    }

}
复制代码

4,使用。

复制代码
  @Transactional(value = "bfscrmTransactionManager",readOnly=true)
    public DataObject doLoad(Serializable rowId)
    {
        return shopLbsRepository.doLoad(rowId);
    }
    
    @Transactional(value = "bfscrmTransactionManager")
    public int doUpdate(String v) {
         shopLbsRepository.doUpdate(v);
         int a  = 8/0;
         return 1;
    }
复制代码

 

总结:测试可用的。

 ****最后说明:如果要用指定的那个数据源,注解 JdbcTemplate 的时候。看如下代码。

   @Autowired
    @Qualifier("bfscrmJdbcTemplate")
    protected JdbcTemplate jdbcTemp;
    
    @Autowired
    @Qualifier("bfscrmDataSource")
    protected DataSource dataSource;
   @Autowired
    @Qualifier("bfJdbcTemplate")
    protected JdbcTemplate jdbcTemp;
    
    @Autowired
    @Qualifier("bfDataSource")
    protected DataSource dataSource;

 

************************

博主给自己的小程序打个广告,支付宝搜索: 变换购物助手  

淘宝,天猫购物最高返利谢谢大家使用支持

posted @   变换  阅读(24675)  评论(5编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示