标签:
1.Spring关于数据源部分的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 3 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 4 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 8 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 9 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 11 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" 12 default-lazy-init="true"> 13 14 <!-- =================================================================== 15 - 数据库连接池配置 16 - =================================================================== --> 17 <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 18 <!-- 基本属性 url、user、password --> 19 <property name="url" value="${ds1.jdbc.url}" /> 20 <property name="username" value="${ds1.jdbc.username}" /> 21 <property name="password" value="${ds1.jdbc.password}" /> 22 23 <!-- 配置初始化大小、最小、最大 --> 24 <property name="initialSize" value="20" /> 25 <property name="minIdle" value="20" /> 26 <property name="maxActive" value="100" /> 27 28 <!-- 配置获取连接等待超时的时间 --> 29 <property name="maxWait" value="60000" /> 30 31 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 32 <property name="timeBetweenEvictionRunsMillis" value="60000" /> 33 34 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 35 <property name="minEvictableIdleTimeMillis" value="300000" /> 36 37 <property name="validationQuery" value="SELECT ‘x‘" /> 38 <property name="testWhileIdle" value="true" /> 39 <property name="testOnBorrow" value="false" /> 40 <property name="testOnReturn" value="false" /> 41 42 <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> 43 <property name="poolPreparedStatements" value="true" /> 44 <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> 45 46 <!-- 配置监控统计拦截的filters --> 47 <property name="filters" value="stat,wall" /> 48 </bean> 49 50 <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 51 <!-- 基本属性 url、user、password --> 52 <property name="url" value="${ds2.jdbc.url}" /> 53 <property name="username" value="${ds2.jdbc.username}" /> 54 <property name="password" value="${ds2.jdbc.password}" /> 55 56 <!-- 配置初始化大小、最小、最大 --> 57 <property name="initialSize" value="20" /> 58 <property name="minIdle" value="20" /> 59 <property name="maxActive" value="100" /> 60 61 <!-- 配置获取连接等待超时的时间 --> 62 <property name="maxWait" value="60000" /> 63 64 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 65 <property name="timeBetweenEvictionRunsMillis" value="60000" /> 66 67 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 68 <property name="minEvictableIdleTimeMillis" value="300000" /> 69 70 <property name="validationQuery" value="SELECT ‘x‘" /> 71 <property name="testWhileIdle" value="true" /> 72 <property name="testOnBorrow" value="false" /> 73 <property name="testOnReturn" value="false" /> 74 75 <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> 76 <property name="poolPreparedStatements" value="true" /> 77 <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> 78 79 <!-- 配置监控统计拦截的filters --> 80 <property name="filters" value="stat,wall" /> 81 </bean> 82 83 <bean id="dataSource" class="org.matrixframework.common.router.DynamicDataSource"> 84 <property name="targetDataSources"> 85 <map key-type="java.lang.String"> 86 <entry value-ref="dataSource1" key="dataSource1"></entry> 87 <entry value-ref="dataSource2" key="dataSource2"></entry> 88 </map> 89 </property> 90 <property name="defaultTargetDataSource" ref="dataSource1"></property> 91 </bean> 92 93 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 94 <property name="dataSource" ref="dataSource"></property> 95 </bean> 96 <tx:annotation-driven transaction-manager="transactionManager" /> 97 98 </beans>
2.封装数据源工具类
1 public class ThreadLocalUtil { 2 private static ThreadLocal<String> dsKeyThreadLocal = new ThreadLocal<String>(); 3 4 public static String getDataSourceKey() { 5 return dsKeyThreadLocal.get(); 6 } 7 8 public static void setDataSourceKey(String value) { 9 ThreadLocalUtil.dsKeyThreadLocal.set(value); 10 } 11 12 /** 13 * 切换到默认数据源: DataSource 1 14 */ 15 public static void switchDataSource1() { 16 setDataSourceKey("dataSource1"); 17 } 18 19 /** 20 * 切换到自定义数据源: DataSource 2 21 */ 22 public static void switchDataSource2() { 23 setDataSourceKey("dataSource2"); 24 } 25 }
3.通过AbstractRoutingDataSource实现数据源切换
1 public class DynamicDataSource extends AbstractRoutingDataSource { 2 3 @Override 4 protected Object determineCurrentLookupKey() { 5 return ThreadLocalUtil.getDataSourceKey(); 6 } 7 8 }
At last, 在操作数据之前, 手工调用 ThreadLocalUtil.switchDataSource~方法即可实现对目标数据源的切换
标签:
原文地址:http://www.cnblogs.com/darkdog/p/4608920.html