最近由於項目須要在作兩個項目間數據同步的需求,具體是項目1的數據經過消息隊列同步到項目2中,由於這個更新操做還涉及到更新多個庫的數據,因此就須要多數據源切換的操做。下面就講講在Spring中如何進行數據源切換。這裏是使用AbstractRoutingDataSource類來完成具體的操做,AbstractRoutingDataSource是Spring2.0後增長的。java
實現數據源切換的功能就是自定義一個類擴展AbstractRoutingDataSource抽象類,其實該至關於數據源DataSourcer的路由中介,能夠實如今項目運行時根據相應key值切換到對應的數據源DataSource上。先看看AbstractRoutingDataSource的源碼:spring
public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean { /* 只列出部分代碼 */ private Map<Object, Object> targetDataSources; private Object defaultTargetDataSource; private boolean lenientFallback = true; private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); private Map<Object, DataSource> resolvedDataSources; private DataSource resolvedDefaultDataSource; @Override public Connection getConnection() throws SQLException { return determineTargetDataSource().getConnection(); } @Override public Connection getConnection(String username, String password) throws SQLException { return determineTargetDataSource().getConnection(username, password); } 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; } protected abstract Object determineCurrentLookupKey(); }
從源碼能夠看出AbstractRoutingDataSource繼承了AbstractDataSource並實現了InitializingBean,AbstractRoutingDataSource的getConnection()方法調用了determineTargetDataSource()的該方法,這裏重點看determineTargetDataSource()方法代碼,方法裏使用到了determineCurrentLookupKey()方法,它是AbstractRoutingDataSource類的抽象方法,也是實現數據源切換要擴展的方法,該方法的返回值就是項目中所要用的DataSource的key值,拿到該key後就能夠在resolvedDataSource中取出對應的DataSource,若是key找不到對應的DataSource就使用默認的數據源。sql
自定義類擴展AbstractRoutingDataSource類時就是要重寫determineCurrentLookupKey()方法來實現數據源切換功能。下面是自定義的擴展AbstractRoutingDataSource類的實現:數據庫
/** * 得到數據源 */ public class MultipleDataSource extends AbstractRoutingDataSource{ @Override protected Object determineCurrentLookupKey() { return DynamicDataSourceHolder.getRouteKey(); } }
DynamicDataSourceHolder類以下,實現對數據源的操做功能:express
/** * 數據源操做類 */ public class DynamicDataSourceHolder { private static ThreadLocal<String> routeKey = new ThreadLocal<String>(); /** * 獲取當前線程的數據源路由的key */ public static String getRouteKey() { String key = routeKey.get(); return key; } /** * 綁定當前線程數據源路由的key * 使用完成後必須調用removeRouteKey()方法刪除 */ public static void setRouteKey(String key) { routeKey.set(key); } /** * 刪除與當前線程綁定的數據源路由的key */ public static void removeRouteKey() { routeKey.remove(); } }
下面在xml文件中配置多個數據源:apache
<!-- 數據源 --> <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"> </property> <property name="url" value="jdbc:jtds:sqlserver://127.0.0.1;databaseName=test"> </property> <property name="username" value="***"></property> <property name="password" value="***"></property> </bean> <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"> </property> <property name="url" value="jdbc:jtds:sqlserver://127.0.0.2:1433;databaseName=test"> </property> <property name="username" value="***"></property> <property name="password" value="***"></property> </bean> <!-- 配置多數據源映射 --> <bean id="multipleDataSource" class="MultipleDataSource" > <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="dataSource1" key="dataSource1"></entry> <entry value-ref="dataSource2" key="dataSource2"></entry> </map> </property> <!-- 默認數據源 --> <property name="defaultTargetDataSource" ref="dataSource1" > </property> </bean>
到這裏基本的配置就完成了,下面只要在須要切換數據源的地方調用方法就好了,通常是在dao層操做數據庫前進行切換的,只需在數據庫操做前加上以下代碼便可:app
DynamicDataSourceHolder.setRouteKey("dataSource2");
上面介紹的是在dao層當須要切換數據源時手動加上切換數據源的代碼,也能夠使用AOP的方式,把配置的數據源類型都設置成註解標籤,在dao層中須要切換數據源操做的方法或類上寫上註解標籤,這樣實現起來可操做性也更強。ide
@DataSourceKey("dataSource1") public interface TestEntityMapper extends MSSQLMapper<TestEntity> { public void insertTest(TestEntity testEntity); }
DataSourceKey註解代碼以下:sqlserver
@Target({ElementType.TYPE,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface DataSourceKey { String value() default ""; }
註解配置完後就要寫一個實現數據源切換的類,以下:this
public class MultipleDataSourceExchange { /** * 攔截目標方法,獲取由@DataSource指定的數據源標識,設置到線程存儲中以便切換數據源 */ public void beforeDaoMethod(JoinPoint point) throws Exception { Class<?> target = point.getTarget().getClass(); MethodSignature signature = (MethodSignature) point.getSignature(); // 默認使用目標類型的註解,若是沒有則使用其實現接口的註解類 for (Class<?> cls : target.getInterfaces()) { resetDataSource(cls, signature.getMethod()); } resetDataSource(target, signature.getMethod()); } /** * 提取目標對象方法註解和類註解中的數據源標識 */ private void resetDataSource(Class<?> cls, Method method) { try { Class<?>[] types = method.getParameterTypes(); // 默認使用類註解 if (cls.isAnnotationPresent(DataSourceKey.class)) { DataSourceKey source = cls.getAnnotation(DataSourceKey.class); DynamicDataSourceHolder.setRouteKey(source.value()); } // 方法註解能夠覆蓋類註解 Method m = cls.getMethod(method.getName(), types); if (m != null && m.isAnnotationPresent(DataSourceKey.class)) { DataSourceKey source = m.getAnnotation(DataSourceKey.class); DynamicDataSourceHolder.setRouteKey(source.value()); } } catch (Exception e) { System.out.println(cls + ":" + e.getMessage()); } } }
代碼寫完後就要在xml配置文件上添加配置了(只列出部分配置):
<bean id="multipleDataSourceExchange" class="MultipleDataSourceExchange "/> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="multipleDataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="insert*" propagation="NESTED" rollback-for="Exception"/> <tx:method name="add*" propagation="NESTED" rollback-for="Exception"/> ... </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="service" expression="execution(* com.datasource..*.service.*.*(..))"/> <!-- 注意切換數據源操做要比持久層代碼先執行 --> <aop:advisor advice-ref="multipleDataSourceExchange" pointcut-ref="service" order="1"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="service" order="2"/> </aop:config>
到此就完成使用AOP的方式實現多數據源的動態切換了。