//数据源公用设置
<bean id="dataSource" name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 指定连接数据库的驱动-->
<property name="driverClass" value="${jdbc.driverClassName}"/>
<!-- 指定连接池中保留的最大连接数. Default:15-->
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
<!-- 指定连接池中保留的最小连接数-->
<property name="minPoolSize" value="${jdbc.minPoolSize}"/>
<!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3-->
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0-->
<property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3-->
<property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>
<!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。
但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0-->
<property name="maxStatements" value="${jdbc.maxStatements}"/>
<!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/>
</bean>
<!-- dataSource配置 -->
<bean id="fromDataSource" parent="dataSource">
<property name="jdbcUrl">
<value>${jdbc.from.url}</value>
</property>
<property name="user">
<value>${jdbc.from.username}</value>
</property>
<property name="password">
<value>${jdbc.from.password}</value>
</property>
</bean>
<bean id="toDataSource" parent="dataSource">
<property name="jdbcUrl">
<value>${jdbc.to.url}</value>
</property>
<property name="user">
<value>${jdbc.to.username}</value>
</property>
<property name="password">
<value>${jdbc.to.password}</value>
</property>
</bean>
<!-- 动态DataSource配置 -->
<bean id="dynamicDataSource" class="cn.com.sanfo.common.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="fromDataSource" value-ref="fromDataSource"/>
<entry key="toDataSource" value-ref="toDataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="fromDataSource"/>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
//ref="动态数据源id"
<property name="dataSource" ref="dynamicDataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
<bean id="jdbcWrap" class="cn.com.sanfo.database.JdbcWrapImp">
//ref="动态数据源id"
<property name="dataSource" ref="dynamicDataSource"/>
</bean>
//动态数据源路由类DynamicDataSource
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
public static final String DATA_SOURCE_FROM = "fromDataSource";// 对应动态数据源配置中的key
public static final String DATA_SOURCE_TO = "toDataSource";
// 本地线程,获取当前正在执行的currentThread
public static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setCustomerType(String customerType) {
contextHolder.set(customerType);
}
public static String getCustomerType() {
return contextHolder.get();
}
public static void clearCustomerType() {
contextHolder.remove();
}
@Override
protected Object determineCurrentLookupKey() {
return getCustomerType();
}
}
//测试
DynamicDataSource.clearCustomerType();
DynamicDataSource.setCustomerType(DynamicDataSource.DATA_SOURCE_TO);
System.out.print(DynamicDataSource.getCustomerType());