1.动态数据源工具类
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 动态数据源
* @author FengMy
*
* @since 2012-11-13
*/
public class RoutingDataSource extends AbstractRoutingDataSource {
protected Object determineCurrentLookupKey() {
return DataSourceHolder.getDataSource();
}
}
/**
*
* 数据源存放类
* @author FengMy
*
* @since 2012-11-13
*/
public class DataSourceHolder {
private static final ThreadLocal<Map<String, Object>> currentDataSource = new ThreadLocal<Map<String, Object>>();
private static final String DATA_SOURCE = "DATA_SOURCE";
public static void setDataSource(String dataSource) {
if(!StringUtils.isEmpty(dataSource)){
Map<String, Object> holder = currentDataSource.get();
if(holder == null) {
holder = new HashMap<String, Object>();
currentDataSource.set(holder);
}
holder.put(DATA_SOURCE, dataSource);
}
}
public static String getDataSource() {
Map<String, Object> holder = currentDataSource.get();
if(holder == null) return null;
return (String) holder.get(DATA_SOURCE);
}
public static void clear() {
currentDataSource.remove();
}
}
2.数据连接配置applicationContext.xml
<bean id="dataSource" class="com.cstp.common.database.RoutingDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="dataSource01" key="dataSource01"></entry>
<entry value-ref="dataSource02" key="dataSource02"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource01"/>
</bean>
<bean id="dataSource01" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" dependency-check="none">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://...:3306/test01?useUnicode=true&characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="cmpc" />
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="25000" />
<property name="acquireIncrement" value="1" />
<property name="acquireRetryDelay" value="1000" />
<property name="acquireRetryAttempts" value="30" />
<property name="initialPoolSize" value="3" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="testConnectionOnCheckin" value="true" />
</bean>
<bean id="dataSource02" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" dependency-check="none">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://.....:3306/test02?useUnicode=true&characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="cmpc" />
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="25000" />
<property name="acquireIncrement" value="1" />
<property name="acquireRetryDelay" value="1000" />
<property name="acquireRetryAttempts" value="30" />
<property name="initialPoolSize" value="3" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="testConnectionOnCheckin" value="true" />
</bean>
<bean id="client" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="configLocation">
<value>classpath:SqlMapConfig.xml</value>
</property>
</bean>
3.操作时,调用
DataSourceHolder.setDataSource("dataSource01");