• 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;

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

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

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

  • 相关阅读:
    cocos2d-x 屏幕坐标系和OPenGL坐标系转换
    cocos2d-x 判断系统语言
    cocos2d-x 动画加速与减速
    高性能网络服务器编程:为什么linux下epoll是最好,Netty要比NIO.2好?
    Netty学习三:线程模型
    java NIO原理及实例
    java多线程系列(四)---ReentrantLock的使用
    Java并发之AQS详解
    微服务踩坑之边界
    设计模式:观察者模式(有利于代码解耦)
  • 原文地址:https://www.cnblogs.com/chen-msg/p/7485701.html
Copyright © 2020-2023  润新知