springboot基于方法级别注解事务的多数据源切换问题
springBoot多数据源配置
配置读数据源
@Component
@ConfigurationProperties(prefix = "jdbc.read")
@PropertySource("classpath:application.properties")
public class ReadDataSource{
private String userName;
private String password;
private String driver;
private String url; //TODO 此处应有get set方法
}
配置写数据源
@Component
@ConfigurationProperties(prefix = "jdbc.read")
@PropertySource("classpath:application.properties")
public class WriteDataSource{
private String userName;
private String password;
private String driver;
private String url; //TODO 此处应有get set方法
}
//配置数据源适配器 通过此类的set方法可以动态切换数据源,我们只需出入数据源对应key即可
public class DataSourceHolder {
private static final ThreadLocal<String> dataSourceTypes = new ThreadLocal<String>() {
@Override
protected String initialValue() {
return "writeDataSource";
}
};
public static String get() {
if(StringUtils.isEmpty(dataSourceTypes.get())){
return "writeDataSource";
}
return dataSourceTypes.get();
}
public static void set(String dataSourceType) {
dataSourceTypes.set(dataSourceType);
}
public static void reset() {
dataSourceTypes.set("writeDataSource");
}
public static void remove() {
dataSourceTypes.remove();
}
}
配置多数据源 此处多数据源的动态切换主要就是通过determineCurrentLookupKey获取对应数据源的key去决定使用哪个数据源
此处需要注意如果处于同一事务中,则数据源不可切换,在事务中,会直接去获取上一次缓存的数据源,没有则调用该方法获取,但只获取一次,所以有可能会导致数据源切换失败.后续我们会通过切面去清除缓存数据源.但仅仅是拿到开启事务第一次获取的数据源.
public class MultipleDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceHolder.get();
}
}
@ConfigurationProperties(prefix = "jdbc.read")此处映射以jdbc.read开头的配置属性名和实体类属性名一致
@PropertySource("classpath:application.properties") 指定从那个属性配置文件读取数据源,我的是Maven项目,所以放在resources下
注意:必须要能够被spring管理起来,所以需要配置到spring扫描路径.
接下来我们需要一个配置类:配置多数据源
//basePackages 指定读和写mapper包位置
@Configuration
@MapperScan(basePackages = {"com.xxx.template.dal.mapper.read","com.xxx.template.dal.mapper.write"},sqlSessionTemplateRef = "sqlSessionTemplate")
public Class DataSourceConfig{ @AutoWried
private ReadDataSource readDataSourceProperties;
@AutoWried
private ReadDataSource writeDataSourceProperties;
//配置读数据源属性 @Bean(destroyMethod = "close") public BasicDataSource readDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(readDataSourceProperties.getDriver()); dataSource.setUrl(readDataSourceProperties.getUrl()); dataSource.setUsername(readDataSourceProperties.getUserName()); dataSource.setPassword(readDataSourceProperties.getPassword()); dataSource.setInitialSize(readDataSourceProperties.getInitialSize()); dataSource.setMaxTotal(readDataSourceProperties.getMaxTotal()); dataSource.setMaxIdle(readDataSourceProperties.getMaxIdle()); dataSource.setRemoveAbandonedOnBorrow(true); dataSource.setRemoveAbandonedTimeout(10); dataSource.setMaxWaitMillis(30000); dataSource.setTestWhileIdle(true); dataSource.setTestOnBorrow(false); dataSource.setTestOnReturn(false); dataSource.setValidationQuery("SELECT 1"); dataSource.setTimeBetweenEvictionRunsMillis(30000); dataSource.setNumTestsPerEvictionRun(30); dataSource.setMinEvictableIdleTimeMillis(600000); return dataSource; } //配置写数据源属性 @Bean(destroyMethod = "close") public BasicDataSource writeDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(writeDataSourceProperties.getDriver()); dataSource.setUrl(writeDataSourceProperties.getUrl()); dataSource.setUsername(writeDataSourceProperties.getUserName()); dataSource.setPassword(writeDataSourceProperties.getPassword()); dataSource.setInitialSize(writeDataSourceProperties.getInitialSize()); dataSource.setMaxTotal(writeDataSourceProperties.getMaxTotal()); dataSource.setMaxIdle(writeDataSourceProperties.getMaxIdle()); dataSource.setRemoveAbandonedOnBorrow(true); dataSource.setRemoveAbandonedTimeout(10); dataSource.setMaxWaitMillis(30000); dataSource.setTestWhileIdle(true); dataSource.setTestOnBorrow(false); dataSource.setTestOnReturn(false); dataSource.setValidationQuery("SELECT 1"); dataSource.setTimeBetweenEvictionRunsMillis(30000); dataSource.setNumTestsPerEvictionRun(30); dataSource.setMinEvictableIdleTimeMillis(600000); return dataSource; }
//配置动态数据源属性 动态数据源包含读写数据源
@Bean
public MultipleDataSource dataSource() {
MultipleDataSource multipleDataSource = new MultipleDataSource();
Map<Object, Object> map = new HashMap<>();
map.put("readDataSource",readDataSource());
map.put("writeDataSource" ,writeDataSource());
//此处存放多数据源进入map,根据key动态切换
multipleDataSource.setTargetDataSources(map);
return multipleDataSource;
}
//配置sqlSessionFactory
@Bean
public SqlSessionFactoryBean sqlSessionFactory() throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
//指定mapper.xml的位置
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(DataSourceConfig.MAPPER_LOCATION));
//配置mybatis配置的位置
sqlSessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(DataSourceConfig.CONFIG_LOCATION));
return sqlSessionFactoryBean;
}
//配置sqlSessionTemplate
@Bean
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory().getObject());
return sqlSessionTemplate;
}
//配置事务管理
@Bean
public DataSourceTransactionManager transactionManager() {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource());
return dataSourceTransactionManager;
}
}
此刻我们数据源已经配好,接下来可以手动切换数据源,通过DataSourceHolder 的各种方法获取,清除,重置.使用完数据源做好调用清除方法,避免缓存导致无法切换数据源
我们也可以指定一个切面类去动态切换数据源
@Aspect
@Order(-1)
@Component
public class DataSourceSwitch { @Before("此处填写切入点表达式")
public void before(){
切换为读数据源
DataSourceHolder.set("writeDataSource");
}
@Before("此处填写切入点表达式")
public void before1(){
切换为读数据源
DataSourceHolder.set("readDataSource");
} @After("此处填写切入点表达式")
public void after(){
//移除数据源
DataSourceHolder.remove();
}
@After("此处填写切入点表达式")
public void after1(){ //移除数据源
DataSourceHolder.remove();
} }
在多数据源和事务结合起来的情况下,无法一个事务下切换数据源,因此只能一个事务下指定一个数据源,比如我们想读和写,那么最好使用写数据源,只读就只指定读数据源.
最后在我们方法级别加上@Transactional
在启动类上加@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})排除spring的默认数据源配置
springboot基于方法级别注解事务的多数据源切换问题的更多相关文章
- redis结合自定义注解实现基于方法的注解缓存,及托底缓存的实现
本次分享如何使用redis结合自定义注解实现基于方法的注解缓存,及托底缓存的实现思路 现在的互联网公司大多数都是以Redis作为缓存,使用缓存的优点就不赘述了,写这篇文章的目的就是想帮助同学们如 ...
- springboot项目自定义注解实现的多数据源切换
一.主要依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spri ...
- dubbo服务+Spring事务+AOP动态数据源切换 出错
1:问题描述,以及分析 项目用了spring数据源动态切换,服务用的是dubbo.在运行一段时间后程序异常,更新操作没有切换到主库上. 这个问题在先调用读操作后再调用写操作会出现. 经日志分析原因: ...
- SpringBoot 内部方法调用,事务不起作用的原因及解决办法
在做业务开发时,遇到了一个事务不起作用的问题.大概流程是这样的,方法内部的定时任务调用了一个带事务的方法,失败后事务没有回滚.查阅资料后,问题得到解决,记录下来分享给大家. 场景 我在这里模拟一个场景 ...
- Transaction事务注解和DynamicDataSource动态数据源切换问题解决
问题描述: 写主库开事务的情况下会导致时不时的将更新/插入操作写入到从库上, 导致mysqlException update command denied 问题原因: jetty的工作队列会重用处 ...
- SpringBoot 基于lettuce 连接池 配置redis多数据源操作 生产配置
添加pom<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons- ...
- 后端基于方法的权限控制--Spirng-Security
后端基于方法的权限控制--Spirng-Security 默认情况下, Spring Security 并不启用方法级的安全管控. 启用方法级的管控后, 可以针对不同的方法通过注解设置不同的访问条件: ...
- Spring_AOP基于AspectJ的注解开发&JDBC的模板使用&事务管理(学习笔记3)
一:AOP基于AspectJ的注解开发 1,简单的实例: 1)引入相应的jar包 2)在配置文件里引入相关约束 <beans xmlns="http://www.springfra ...
- SpringBoot 注解事务声明式事务
转载请注明: http://www.cnblogs.com/guozp/articles/7446477.html springboot 对新人来说可能上手比springmvc要快,但是对于各位从sp ...
随机推荐
- qt5--树形控件QTreeWidget
需要 #include <QTreeWidget> #include <QTreeWidgetItem> #include " ...
- Python基础-day05
高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...
- chomre 控制台断点调试
在上图蓝色圆圈中数字,它们分别代表: 1.停止断点调试 2.不跳入函数中去,继续执行下一行代码(F10) 3.跳入函数中去(F11) 4.从执行的函数中跳出 5.禁用所有的断点,不做任何调试 6.程序 ...
- sql 导入文件
zai SQLQuery4.sql 文件中 --BULK INSERT Table_1 from 'D:\aaaa#azzz.txt' with(fieldterminator=',',rowterm ...
- Linux Expect自动交互脚本
https://likegeeks.com/expect-command/ In the previous post, we talked about writing practical shell ...
- TTTTTTTTTTTTTTTTTTTTT POJ 3690 0与* 二维哈希 模板 +multiset
Constellations Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5923 Accepted: 1164 De ...
- Unity3D_(插件)小地图自刷新制作Minimap小地图
制作小地图:小地图自刷新制作小地图 原理:用不同的图标表示场景中不同的游戏物体,将(场景中)游戏物体位置实时放置小地图上,并控制图标的位置更新 好处:可更好控制小地图上所需要显示的游戏物体 游戏项目已 ...
- Implement GAN from scratch
GANs from Scratch 1: A deep introduction. With code in PyTorch and TensorFlow 修改文章代码中的错误后的代码如下: impo ...
- 剑指offer31----栈的压入、弹出序列
题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对 ...
- Scala学习(三)——集合
基本数据结构 Scala提供了一些不错的集合. 数组 Array 数组是有序的,可以包含重复项,并且可变. val numbers = Array(1, 2, 3, 4, 5, 1, 2, 3, 4, ...