項目中有時候需要用的數據源不止一個,這個時候需要對數據源進行切換,一種方法只在xml文件中進行配置,將mybatis對應的配置注入成需要使用的數據源,這種方式的弊端是sqlsession,事務都要配置多次,另外一種方法是用spring的aop特性來完成。
spring-context-db的配置xml
<context:property-placeholder location="classpath:db.properties"
ignore-unresolvable="true"/>
<bean id="globalDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl"
value="${mysql.url}"></property>
<property name="user" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
<property name="testConnectionOnCheckout" value="${jdbc.testConnectionOnCheckout}"/>
<property name="preferredTestQuery" value="${jdbs.preferredTestQuery}"/>
</bean>
#定義第二個數據源
<bean id="regionDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl"
value="${seperate.mysql.url}"/>
<property name="user" value="${seperate.mysql.username}"/>
<property name="password" value="${seperate.mysql.password}"/>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
<property name="testConnectionOnCheckout" value="${jdbc.testConnectionOnCheckout}"/>
<property name="preferredTestQuery" value="${jdbs.preferredTestQuery}"/>
</bean>
#注入一個用于控制兩個數據源的multipleDataSource
<bean id="multipleDataSource" class="com.xxx.xxx.xxx.shared.MultipleDataSource">
<property name="defaultTargetDataSource" ref="globalDataSource"/>
<property name="targetDataSources">
###根據類型注入實際使用的數據源
<map key-type="com.xxx.xxx.xxx.shared.DataSources">
<entry key="Global" value-ref="globalDataSource"/>
<entry key="Region" value-ref="regionDataSource"/>
</map>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="multipleDataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="multipleDataSource"/>
<property name="mapperLocations">
<list>
<value>classpath*:mapper/*Mapper.xml</value>
</list>
</property>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.xxx.xxx.dao.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
db.properties
#cloudbill mysql database
mysql.url=jdbc:mysql://{ip}:4365/dbname1?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
mysql.username=db1Username
mysql.password=db1Password
#cloudbillLog mysql database
seperate.mysql.url=jdbc:mysql://{ip}:4365/dbname2?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
seperate.mysql.username=db1Username
seperate.mysql.password=db2Password
#jdbc common properties
jdbc.initialPoolSize=10
jdbc.maxPoolSize=100
jdbc.testConnectionOnCheckout=true
jdbs.preferredTestQuery=SELECT 1
定義多數據源,繼承AbstractRoutingDataSource,重寫determineCurrentLookupKey
package com.xxx.xxx.xxx.shared;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class MultipleDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<DataSources> dataSourceKey = new InheritableThreadLocal<DataSources>() {
@Override
protected DataSources initialValue() {
return DataSources.Global;
}
};
public static void setDataSourceKey(DataSources dataSource) {
dataSourceKey.set(dataSource);
}
@Override
protected Object determineCurrentLookupKey() {
return dataSourceKey.get();
}
}
附上AbstractRoutingDataSource的源碼
public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {
private Map<Object, Object> targetDataSources;
private Object defaultTargetDataSource;
/**
* Retrieve the current target DataSource. Determines the
* {@link #determineCurrentLookupKey() current lookup key}, performs
* a lookup in the {@link #setTargetDataSources targetDataSources} map,
* falls back to the specified
* {@link #setDefaultTargetDataSource default target DataSource} if necessary.
* @see #determineCurrentLookupKey()
*/
protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
Object lookupKey = determineCurrentLookupKey();
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
dataSource = this.resolvedDefaultDataSource;
}
if (dataSource == null) {
throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
}
return dataSource;
}
/**
* Determine the current lookup key. This will typically be
* implemented to check a thread-bound transaction context.
* <p>Allows for arbitrary keys. The returned key needs
* to match the stored lookup key type, as resolved by the
* {@link #resolveSpecifiedLookupKey} method.
*/
protected abstract Object determineCurrentLookupKey();
......
}
定一個數據源的枚舉類
package com.xxx.xxx.xxx.shared;
public enum DataSources {
Global,Region;
}
定一個切面,在xxxMapper中的任意方法設置連接點,利用@Before和@After來控制數據源的切換。
package com.xxx.xxx.xxx;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import com.xxx.xxx.xxx.shared.DataSources;
import com.xxx.xxx.xxx.shared.MultipleDataSource;
@Component
@Aspect
public class MultipleDataSourceInterptor {
@Pointcut("execution(* com.xxx.xxx.xxx.dao.mapper.xxxMapper.*(..))")
public void aspectPoint() {
}
@Before("aspectPoint()")
public void advice(JoinPoint jp) throws Throwable {
MultipleDataSource.setDataSourceKey(DataSources.Region);
}
@After("aspectPoint()")
public void adviceAfterReturn(JoinPoint jp) {
MultipleDataSource.setDataSourceKey(DataSources.Global);
}
}
最后spring-context的配置文件中還需要加入
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=“http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd”
<aop:aspectj-autoproxy/>
開啟aop的功能。
以上就可以實現數據源的自動切換了。關于本地測試的方法,利用單元測試沒法測,只能在contoller中加接口進行測試了,在接口中使用兩個數據源分別對應的DAO,開啟debug模式,通過查看數據庫的數據和mybatis框架的debug日志,查看數據源的切換情況,也可以在aop中加日志追蹤。