首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

利用Spring的AbstractRoutingDataSource解决多数据源的有关问题

2013-07-04 
利用Spring的AbstractRoutingDataSource解决多数据源的问题多数据源问题很常见,例如读写分离数据库配置。?

利用Spring的AbstractRoutingDataSource解决多数据源的问题

多数据源问题很常见,例如读写分离数据库配置。

?

原来的项目出现了新需求,局方要求新增某服务器用以提供某代码,涉及到多数据源的问题。

?

研究成果如下:

?

1、首先配置多个datasource

?

[html] view plaincopy
  1. <bean?id="dataSource"?class="org.apache.commons.dbcp.BasicDataSource">??????????<property?name="driverClassName"?value="net.sourceforge.jtds.jdbc.Driver">??
  2. ????????</property>??????????<property?name="url"?value="jdbc:jtds:sqlserver://10.82.81.51:1433;databaseName=standards">??
  3. ????????</property>??????????<property?name="username"?value="youguess"></property>??
  4. ????????<property?name="password"?value="youguess"></property>??????</bean>??
  5. ????<bean?id="dataSource2"?class="org.apache.commons.dbcp.BasicDataSource">??????????<property?name="driverClassName"?value="net.sourceforge.jtds.jdbc.Driver">??
  6. ????????</property>??????????<property?name="url"?value="jdbc:jtds:sqlserver://10.82.81.52:1433;databaseName=standards">??
  7. ????????</property>??????????<property?name="username"?value="youguess"></property>??
  8. ????????<property?name="password"?value="youguess"></property>??</bean>??


2、写一个DynamicDataSource类继承AbstractRoutingDataSource,并实现determineCurrentLookupKey方法

?

[java] view plaincopy
  1. package?com.standard.core.util;??import?org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;??
  2. public?class?DynamicDataSource?extends?AbstractRoutingDataSource?{??????@Override??
  3. ????protected?Object?determineCurrentLookupKey()?{??????????return?CustomerContextHolder.getCustomerType();??
  4. ????}??}??



?

3、利用ThreadLocal解决线程安全问题

?

?

?

[java] view plaincopy
  1. package?com.standard.core.util;??public?class?CustomerContextHolder?{??
  2. ????public?static?final?String?DATA_SOURCE_A?=?"dataSource";??????public?static?final?String?DATA_SOURCE_B?=?"dataSource2";??
  3. ????private?static?final?ThreadLocal<String>?contextHolder?=?new?ThreadLocal<String>();??????public?static?void?setCustomerType(String?customerType)?{??
  4. ????????contextHolder.set(customerType);??????}??
  5. ????public?static?String?getCustomerType()?{??????????return?contextHolder.get();??
  6. ????}??????public?static?void?clearCustomerType()?{??
  7. ????????contextHolder.remove();??????}??
  8. }??


4、数据源配置

?

?

[html] view plaincopy
  1. <bean?id="dynamicDataSource"?class="com.standard.core.util.DynamicDataSource"?>??????????<property?name="targetDataSources">??
  2. ????????????<map?key-type="java.lang.String">??????????????????<entry?value-ref="dataSource"?key="dataSource"></entry>??
  3. ????????????????<entry?value-ref="dataSource2"?key="dataSource2"></entry>??????????????</map>??
  4. ????????</property>??????????<property?name="defaultTargetDataSource"?ref="dataSource"?>??
  5. ????????</property>??????</bean>???


5、在DAOImpl中切换数据源

?

?

[java] view plaincopy
  1. CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_B);???


搞定!

热点排行