项目中有时候需要用的数据源不止一个,这个时候需要对数据源进行切换,一种方法只在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中加日志追踪。
网友评论