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基于方法级别注解事务的多数据源切换问题的更多相关文章

  1. redis结合自定义注解实现基于方法的注解缓存,及托底缓存的实现

    本次分享如何使用redis结合自定义注解实现基于方法的注解缓存,及托底缓存的实现思路    现在的互联网公司大多数都是以Redis作为缓存,使用缓存的优点就不赘述了,写这篇文章的目的就是想帮助同学们如 ...

  2. springboot项目自定义注解实现的多数据源切换

    一.主要依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spri ...

  3. dubbo服务+Spring事务+AOP动态数据源切换 出错

    1:问题描述,以及分析 项目用了spring数据源动态切换,服务用的是dubbo.在运行一段时间后程序异常,更新操作没有切换到主库上. 这个问题在先调用读操作后再调用写操作会出现. 经日志分析原因: ...

  4. SpringBoot 内部方法调用,事务不起作用的原因及解决办法

    在做业务开发时,遇到了一个事务不起作用的问题.大概流程是这样的,方法内部的定时任务调用了一个带事务的方法,失败后事务没有回滚.查阅资料后,问题得到解决,记录下来分享给大家. 场景 我在这里模拟一个场景 ...

  5. Transaction事务注解和DynamicDataSource动态数据源切换问题解决

    问题描述: 写主库开事务的情况下会导致时不时的将更新/插入操作写入到从库上, 导致mysqlException update command denied   问题原因: jetty的工作队列会重用处 ...

  6. SpringBoot 基于lettuce 连接池 配置redis多数据源操作 生产配置

    添加pom<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons- ...

  7. 后端基于方法的权限控制--Spirng-Security

    后端基于方法的权限控制--Spirng-Security 默认情况下, Spring Security 并不启用方法级的安全管控. 启用方法级的管控后, 可以针对不同的方法通过注解设置不同的访问条件: ...

  8. Spring_AOP基于AspectJ的注解开发&JDBC的模板使用&事务管理(学习笔记3)

    一:AOP基于AspectJ的注解开发 1,简单的实例: 1)引入相应的jar包 ​ 2)在配置文件里引入相关约束 <beans xmlns="http://www.springfra ...

  9. SpringBoot 注解事务声明式事务

    转载请注明: http://www.cnblogs.com/guozp/articles/7446477.html springboot 对新人来说可能上手比springmvc要快,但是对于各位从sp ...

随机推荐

  1. vue启动问题(You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file.)

    解决vue启动出现: 在build/webpack.base.conf.js文件中,把...(config.dev.useEslint ? [createLintingRule()] : [])注释或 ...

  2. 窗体操作:CBrush类

    CBrush画刷定义了一种位图形式的像素,利用它可对区域内部填充颜色. 该类封装了Windows的图形设备接口(GDI)刷子.通过该类构造的CBrush对象可以传递给任何一个需要画刷的CDC成员函数. ...

  3. Poj 3057 未AC http://poj.org/showsource?solution_id=15175171

    <span style="font-size:18px;">#include <iostream> #include <cstdio> #inc ...

  4. HashMap、Hashtable、HashSet三种hash集合的区别

    转载:http://www.cnblogs.com/lzrabbit/p/3721067.html#h1 HashMap和Hashtable的区别 两者最主要的区别在于Hashtable是线程安全,而 ...

  5. App可视化埋点技术原理大揭秘

    一.背景 运营者能够对用户行为进行分析的前提,是对大量数据的掌握.在以往,这个数据通常是由开发者在控件点击.页面等事件中,一行行地编写埋点代码来完成数据收集的.然而传统的操作模式每当升级改版时,开发和 ...

  6. TCP定时器 之 连接建立定时器

    当服务器收到新的syn请求,会回复syn+ack给请求端,若某时间内未收到请求端回复的ack,新建连接定时器超时执行回调,重传syn+ack,当超时超过固定次数时,该连接中止:本文主要分析其初始化流程 ...

  7. python3笔记七:break和continue语句

    一:学习内容 break语句 continue语句 二:break语句 1. 说明 作用:跳出for和while的循环注意:只能跳出距离它最近的那一层循环 2.举例1 for i in range(1 ...

  8. python3笔记十三:python数据类型-Set集合

    一:学习内容 集合概念 集合创建 集合添加 集合插入 集合删除 集合访问 集合操作:并集.交集 二:集合概念 1.set:类似dict,是一组key的集合,不存储value 2.本质:无序和无重复元素 ...

  9. 五、smarty模板继承特性

    1.如何去实现模板之间的继承 继承是发生在模板之间的事,和PHP程序没有关系的 方法一: 在模板中使用<{extends}>函数实现模板的继承 <{extends file=”模板文 ...

  10. webDriver各版本对应

    chromeDriver http://npm.taobao.org/mirrors/chromedriver/ http://chromedriver.storage.googleapis.com/ ...