iBatis多数据源的苦恼
在仅使用ibatis时,多数据源简直就是梦魇,每多一个数据源就需要多一份sql-map-config配置文件。
采用spring的AbstractRoutingDataSource就可以简单的解决这个问题。
AbstractRoutingDataSource实现了javax.sql.DataSource接口,因此可以理解为一个虚拟的动态DataSource,在需要的时候根据上下文Context动态决定使用哪个数据源。
Spring+iBatis多数据源的配置
下面是一个完整的配置:
<?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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<!-- ========================= RESOURCE DEFINITIONS ========================= -->
<!-- Main JNDI DataSource for J2EE environments -->
<!-- Refers to the main database, containing product and account data -->
<!-- (see dataAccessContext-local.xml for an alternative) -->
<!--<jee:jndi-lookup id="dataSource" jndi-name="jdbc/jpetstore"/>-->
<!-- Additional JNDI DataSource for J2EE environments -->
<!-- Refers to the order database, containing order data -->
<!-- (see dataAccessContext-local.xml for an alternative) -->
<!--<jee:jndi-lookup id="orderDataSource" jndi-name="jdbc/jpetstore-order"/>-->
<bean id="sysDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbcsys.driverClassName}"/>
<property name="url" value="${jdbcsys.url}"/>
<property name="username" value="${jdbcsys.username}"/>
<property name="password" value="${jdbcsys.password}"/>
<property name="defaultAutoCommit" value="false"/>
</bean>
<bean id="coreDataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbccore1.driverClassName}"/>
<property name="url" value="${jdbccore1.url}"/>
<property name="username" value="${jdbccore1.username}"/>
<property name="password" value="${jdbccore1.password}"/>
<property name="defaultAutoCommit" value="false"/>
</bean>
<bean id="coreDataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbccore2.driverClassName}"/>
<property name="url" value="${jdbccore2.url}"/>
<property name="username" value="${jdbccore2.username}"/>
<property name="password" value="${jdbccore2.password}"/>
<property name="defaultAutoCommit" value="false"/>
</bean>
<bean id="dataSource" class="com.bitfone.mvp.bulk.dao.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="SYSDB" value-ref="sysDataSource"/>
<entry key="COREDB1" value-ref="coreDataSource1"/>
<entry key="COREDB2" value-ref="coreDataSource2"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="sysDataSource"/>
</bean>
<!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource) -->
<!-- Necessary here due to the need for distributed transactions across two databases -->
<!-- (see dataAccessContext-local.xml for an alternative) -->
<!--<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>-->
<!--<bean id="transactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager"/>-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- SqlMap setup for iBATIS Database Layer -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<!--For release-->
<property name="configLocation" value="../config/dao/sql-map-config.xml"/>
<!--For Test-->
<!--<property name="configLocation" value="classpath:dao/sql-map-config.xml"/>-->
</bean>
</beans>
其核心是DynamicDataSource,代码如下
public class DynamicDataSource extends AbstractRoutingDataSource
{
protected Object determineCurrentLookupKey()
{
return DbContextHolder.getDbType();
}
}
上下文DbContextHolder为一线程安全的ThreadLocal,如下
public class DbContextHolder
{
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setDbType(String dbType) {
contextHolder.set(dbType);
}
public static String getDbType() {
return ((String)contextHolder.get());
}
public static void clearDbType() {
contextHolder.remove();
}
}
以上就介绍了Spring+iBatis多数据源的配置方案。本文参考eddysheng的博客:《基于spring和ibatis的多数据源切换方案》。