Spring配置多数据源
1.Spring关于数据源部分的配置文件
<?xml version="1.0" encoding="UTF-8"?> <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" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-lazy-init="true"> <!-- =================================================================== - 数据库连接池配置 - =================================================================== --> <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="url" value="${ds1.jdbc.url}" /> <property name="username" value="${ds1.jdbc.username}" /> <property name="password" value="${ds1.jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="20" /> <property name="minIdle" value="20" /> <property name="maxActive" value="100" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 配置监控统计拦截的filters --> <property name="filters" value="stat,wall" /> </bean> <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="url" value="${ds2.jdbc.url}" /> <property name="username" value="${ds2.jdbc.username}" /> <property name="password" value="${ds2.jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="20" /> <property name="minIdle" value="20" /> <property name="maxActive" value="100" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 配置监控统计拦截的filters --> <property name="filters" value="stat,wall" /> </bean> <bean id="dataSource" class="org.matrixframework.common.router.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="dataSource1" key="dataSource1"></entry> <entry value-ref="dataSource2" key="dataSource2"></entry> </map> </property> <property name="defaultTargetDataSource" ref="dataSource1"></property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
2.封装数据源工具类
public class ThreadLocalUtil { private static ThreadLocal<String> dsKeyThreadLocal = new ThreadLocal<String>(); public static String getDataSourceKey() { return dsKeyThreadLocal.get(); } public static void setDataSourceKey(String value) { ThreadLocalUtil.dsKeyThreadLocal.set(value); } /** * 切换到默认数据源: DataSource 1 */ public static void switchDataSource1() { setDataSourceKey("dataSource1"); } /** * 切换到自定义数据源: DataSource 2 */ public static void switchDataSource2() { setDataSourceKey("dataSource2"); } }
3.通过AbstractRoutingDataSource实现数据源切换
public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return ThreadLocalUtil.getDataSourceKey(); } }
At last, 在操作数据之前, 手工调用 ThreadLocalUtil.switchDataSource~方法即可实现对目标数据源的切换