在网上找到的配置多数据源的方法。
1.扩展 org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource类
实现代码
代码
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
// TODO Auto-generated method stub
return DbContextHolder.getDbType();
}
}
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
// TODO Auto-generated method stub
return DbContextHolder.getDbType();
}
}
2.实现一个线程安全的DbContextHolder类。
代码
public class DbContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setDbType(String dbType)
{
contextHolder.set(dbType);
}
/**
* 取得dbtype类型
* @return
*/
public static String getDbType()
{
String str=(String) contextHolder.get();
if(null==str || "".equals(str))
str="1";
return str;
}
public static void clearDbType()
{
contextHolder.remove();
}
}
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setDbType(String dbType)
{
contextHolder.set(dbType);
}
/**
* 取得dbtype类型
* @return
*/
public static String getDbType()
{
String str=(String) contextHolder.get();
if(null==str || "".equals(str))
str="1";
return str;
}
public static void clearDbType()
{
contextHolder.remove();
}
}
3.修改spring的配置文件。
代码
<!--动态选择数据源-->
<bean id="dataSource" class="tecamo.core.utils.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="1" value-ref="ds1"/>
<entry key="2" value-ref="ds2"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="ds1"/>
</bean>
<!-- 数据源定义 -->
<bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="ds2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbcOrcale.driverClassName}"/>
<property name="url" value="${jdbcOrcale.url}"/>
<property name="username" value="${jdbcOrcale.username}"/>
<property name="password" value="${jdbcOrcale.password}"/>
</bean>
<bean id="dataSource" class="tecamo.core.utils.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="1" value-ref="ds1"/>
<entry key="2" value-ref="ds2"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="ds1"/>
</bean>
<!-- 数据源定义 -->
<bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="ds2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbcOrcale.driverClassName}"/>
<property name="url" value="${jdbcOrcale.url}"/>
<property name="username" value="${jdbcOrcale.username}"/>
<property name="password" value="${jdbcOrcale.password}"/>
</bean>
4.切换数据源方法。
在serverice方法之前调用
DbContextHolder.setDbType("2");表示使用第二个数据源。