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;
************************
博主给自己的小程序打个广告,支付宝搜索: 变换购物助手
淘宝,天猫购物最高返利谢谢大家使用支持
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了