c语言sscanf函数的用法是什么
277
2023-07-13
Spring配置多数据源切换
多数据源切换
db.properties
#mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?autoReconnect=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=admin
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=1000
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
#MySQL
# driverClassName 根据url自动识别 这一项可配可不配,如果不配置druid会根据url自动识别dbType,然后选择相应的driverClassName
jdbc.driver1=com.mysql.jdbc.Driver
jdbc.url1=jdbc:mysql://localhost:3306/test1?allowMultiQueries=true&autoReconnect=true&characterEncoding=utf-8
jdbc.username1=root
jdbc.password1=admin
# 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
initialSize1=0
# 最大连接池数量
maxActive1=1000
#定义最小空闲
minIdle1=1
# 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,
# 缺省启用公平锁,并发效率会有所下降,
# 如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
maxWait1=60000
# druid 监控
# 属性类型是字符串,通过别名的方式配置扩展插件,
# 常用的插件有:
# 监控统计用的filter:stat
# 日志用的filter:log4j
# 防御sql注入的filter:wall
filters1=stat,log4j
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis1=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis1=300000
# 建议配置为true,不影响性能,并且保证安全性。
# 申请连接的时候检测,如果空闲时间大于
# timeBetweenEvictionRunsMillis,
# 执行validationQuery检测连接是否有效。
testWhileIdle1=true
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
testOnBorrow1=false
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnReturn1=false
# 是否缓存preparedStatement,也就是PSCache。
# PSCache对支持游标的数据库性能提升巨大,比如说oracle。
# 在mysql5.5以下的版本中没有PSCache功能,建议关闭掉。
# 该应该是支持PSCache。
poolPreparedStatements1=false
# 要启用PSCache,必须配置大于0,当大于0时,
# poolPreparedStatements自动触发修改为true。
# 在Druid中,不会存在Oracle下PSCache占用内存过多的问题,
# 可以把这个数值配置大一些,比如说100
maxOpenPreparedStatements1=-1
applicationContext.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:tx="http://springframework.org/schema/tx" xmlns:websocket="http://springframework.org/schema/websocket" xsi:schemaLocation=" http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd http://springframework.org/schema/websocket http://springframework.org/schema/websocket/spring-websocket.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:tx="http://springframework.org/schema/tx"
xmlns:websocket="http://springframework.org/schema/websocket"
xsi:schemaLocation="
http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx.xsd
http://springframework.org/schema/websocket
http://springframework.org/schema/websocket/spring-websocket.xsd">
创建MultipleDataSource.java
package com.ys.dbConfig;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class MultipleDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return MultipleDataSourceHandler.getRouteKey();
}
}
创建MultipleDataSourceHandler.java
package com.ys.dbConfig;
/**
*@ Title MultipleDataSourceHandler.java
*@ description: 多数据源Handler
*@ time 创建时间:2018年8月25日 上午10:52:12
**/
public class MultipleDataSourceHandler {
private static ThreadLocal
/**
* @Title: getRouteKey
* @Description: 获取当前线程的数据源路由的key
* @param @return
* @return String
* @date createTime:2018年8月27日上午10:34:52
*/
public static String getRouteKey(){
return routeKey.get();
}
/**
* @Title: setRouteKey
* @Description: 绑定当前线程数据源路由的key 使用完成后必须调用removeRouteKey()方法删除
* @param @param key
* @return void
* @date createTime:2018年8月27日上午10:35:03
*/
public static void setRouteKey(String key){
routeKey.set(key);
}
/**
* @Title: removeRouteKey
* @Description: 删除与当前线程绑定的数据源路由的key
* @return void
* @date createTime:2018年8月27日上午10:35:31
*/
public static void removeRouteKey(){
routeKey.remove();
}
}
切换数据源
MultipleDataSourceHandler.setRouteKey(“dataSource1”);
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~