Spring problem of using multiple data sources to solve AbstractRoutingDataSource [manually switch the code, non-use of Spring AOP] AbstractRoutingDataSource multiple data sources to solve a problem

turn:

Spring AbstractRoutingDataSource use of multiple data sources to solve the problem

Multiple Data Sources is common, for example, separate read and write database configuration.

The original project the emergence of new demands, the Council request to add a server to provide a code that involves the issue of multiple data sources.

Solutions are as follows:

1, a plurality of first datasource

Copy the code
 1 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  2 <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver">  3 </property>  4 <property name="url" value="jdbc:jtds:sqlserver://10.82.81.51:1433;databaseName=standards">  5 </property>  6 <property name="username" value="youguess"></property>  7 <property name="password" value="youguess"></property>  8 </bean>  9 <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"> 10 <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"> 11 </property> 12 <property name="url" value="jdbc:jtds:sqlserver://10.82.81.52:1433;databaseName=standards"> 13 </property> 14 <property name="username" value="youguess"></property> 15 <property name="password" value="youguess"></property> 16 </bean>
Copy the code

2, write a class that inherits DynamicDataSource AbstractRoutingDataSource, and methods to achieve determineCurrentLookupKey

 

Copy the code
1 package com.standard.core.util;
2 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; 3 public class DynamicDataSource extends AbstractRoutingDataSource { 4  @Override 5 protected Object determineCurrentLookupKey() { 6 return CustomerContextHolder.getCustomerType(); 7  } 8 }
Copy the code

 

3, the use of ThreadLocal solve thread safety issues

 

Copy the code
 1 package com.standard.core.util;
 2 public class CustomerContextHolder {  3 public static final String DATA_SOURCE_A = "dataSource";  4 public static final String DATA_SOURCE_B = "dataSource2";  5 private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();  6 public static void setCustomerType(String customerType) {  7  contextHolder.set(customerType);  8  }  9 public static String getCustomerType() { 10 return contextHolder.get(); 11  } 12 public static void clearCustomerType() { 13  contextHolder.remove(); 14  } 15 }
Copy the code

 

 

 

4, the data source configured

 

 

Copy the code
 1 <bean id="dynamicDataSource" class="com.standard.core.util.DynamicDataSource" >  2 <property name="targetDataSources">  3 <map key-type="java.lang.String">  4 <entry value-ref="dataSource" key="dataSource"></entry>  5 <entry value-ref="dataSource2" key="dataSource2"></entry>  6 </map>  7 </property>  8 <property name="defaultTargetDataSource" ref="dataSource" >  9 </property> 10 </bean> 
Copy the code

5, the switching data source DAOImpl

1 CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_B); 

 

Guess you like

Origin www.cnblogs.com/libin6505/p/11229947.html