最近因為項目需要在做兩個項目間數據同步的需求,具體是項目1的數據通過消息隊列同步到項目2中,因為這個更新操作還涉及到更新多個庫的數據,所以就需要多數據源切換的操作。下面就講講在Spring中如何進行數據源切換。這里是使用AbstractRoutingDataSource類來完成具體的操作,AbstractRoutingDataSource是Spring2.0后增加的。
實現數據源切換的功能就是自定義一個類擴展AbstractRoutingDataSource抽象類,其實該相當于數據源DataSourcer的路由中介,可以實現在項目運行時根據相應key值切換到對應的數據源DataSource上。先看看AbstractRoutingDataSource的源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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就使用默認的數據源。
自定義類擴展AbstractRoutingDataSource類時就是要重寫determineCurrentLookupKey()方法來實現數據源切換功能。下面是自定義的擴展AbstractRoutingDataSource類的實現:
1
2
3
4
5
6
7
8
9
10
|
/** * 獲得數據源 */ public class MultipleDataSource extends AbstractRoutingDataSource{ @Override protected Object determineCurrentLookupKey() { return DynamicDataSourceHolder.getRouteKey(); } } |
DynamicDataSourceHolder類如下,實現對數據源的操作功能:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/** * 數據源操作類 */ 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文件中配置多個數據源:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<!-- 數據源 --> < 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層操作數據庫前進行切換的,只需在數據庫操作前加上如下代碼即可:
1
|
DynamicDataSourceHolder.setRouteKey( "dataSource2" ); |
上面介紹的是在dao層當需要切換數據源時手動加上切換數據源的代碼,也可以使用AOP的方式,把配置的數據源類型都設置成注解標簽,在dao層中需要切換數據源操作的方法或類上寫上注解標簽,這樣實現起來可操作性也更強。
1
2
3
4
|
@DataSourceKey ( "dataSource1" ) public interface TestEntityMapper extends MSSQLMapper<TestEntity> { public void insertTest(TestEntity testEntity); } |
DataSourceKey注解代碼如下:
1
2
3
4
5
6
|
@Target ({ElementType.TYPE,ElementType.METHOD}) @Retention (RetentionPolicy.RUNTIME) @Documented public @interface DataSourceKey { String value() default "" ; } |
注解配置完后就要寫一個實現數據源切換的類,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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配置文件上添加配置了(只列出部分配置):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
< 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的方式實現多數據源的動態切換了。
原文鏈接:http://www.cnblogs.com/weknow619/p/6415900.html