前言:
最近开发时遇到这样一个场景: 在一段定时任务中需要将A数据库中查询的结果处理后,然后插入B数据库,这是就考虑到用动态数据源,在这里记录一下配置过程,仅供参考:
一: 在spring.xml添加配置
<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc1.url}"/>
<property name="username" value="${jdbc1.user}"/>
<property name="password" value="${jdbc1.password}"/>
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat"/>
</bean>
<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc2.url}"/>
<property name="username" value="${jdbc2.user}"/>
<property name="password" value="${jdbc2.password}"/>
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat"/>
</bean>
<bean id="dataSource" class="com.util.dataSource.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="dataSource1" value-ref="dataSource1" />
<entry key="dataSource2" value-ref="dataSource2" />
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource1"/>
</bean>
<!-- 配置数据源切换实现类 -->
<bean id="dataSourceEntry" class="com.util.dataSource.DataSourceEntryImpl" />
二: 添加相应的工具类
public interface DataSourceEntry {
// 默认数据源
public final static String DEFAULT_SOURCE = "dataSource1";
/**
* 还原数据源
*
* @param join
*/
public void restore(JoinPoint join);
/**
* 设置数据源
*
* @param source
*/
public void set(String source);
/**
* 获取数据源
*
* @return String
*/
public String get();
/**
* 清空数据源
*/
public void clear();
}
/**
* 数据源切换实现类类
*
* @create 2013.04.03
*/
public class DataSourceEntryImpl implements DataSourceEntry {
private final static ThreadLocal<String> local = new ThreadLocal<String>();
public void clear() {
local.remove();
}
public String get() {
return local.get();
}
public void restore(JoinPoint join) {
local.set(DEFAULT_SOURCE);
}
public void set(String source) {
local.set(source);
}
}
public class DbContextHolder {
@SuppressWarnings("rawtypes")
private static final ThreadLocal contextHolder = new ThreadLocal();
@SuppressWarnings("unchecked")
public static void setDbType(String dbType) {
contextHolder.set(dbType);
}
public static String getDbType() {
return (String) contextHolder.get();
}
public static void clearDbType() {
contextHolder.remove();
}
}
public class DynamicDataSource extends AbstractRoutingDataSource {
private DataSourceEntry dataSourceEntry;
@Override
protected Object determineCurrentLookupKey() {
return DbContextHolder.getDbType();
}
@Resource
public void setDataSourceEntry(DataSourceEntry dataSourceEntry) {
this.dataSourceEntry = dataSourceEntry;
}
}
三: 具体的是业务方法
public void writeLocalData(String startTime,String endTime) {
try {
System.out.println(sdf1.format(new Date()));
//日报进行记录
List<StatementOneDay> carList1 = dataServise.oneData(startTime,endTime);
//切换到新的数据库
DbContextHolder.setDbType("dataSource2");
//向新的数据库插入数据
dataServise.addLocalData(carList1);
System.out.println(sdf1.format(new Date()));
} catch (Exception e) {
e.printStackTrace();
}
}