1. When accessing, first set the data source to be used through DbContextHolder.setDbType("ds1");. DbContextHolder is a class used to store data source information, which records data source information through ThreadLocal.
2. The DynamicDataSource class integrates Spring's AbstractRoutingDataSource class, and obtains the data source type through the determineCurrentLookupKey method. If there is no corresponding data source, the defaultTargetDataSource configuration is used.
Note: When the data source is set, the data source will always be used to connect, unless the data source is reset with DbContextHolder.setDbType or cleared with DbContextHolder.clearDbType(), and the defaultTargetDataSource is used to connect after clearing.
1. Configuration file
properties
ds1.driverClassName=oracle.jdbc.OracleDriver ds1.url=jdbc:oracle:thin:@localhost:1521:ORCL ds1.username=SSM ds1.password=SSM ds2.driverClassName=oracle.jdbc.OracleDriver ds2.url=jdbc:oracle:thin:@10.27.192.43:1522:ORCL ds2.username=FRAMEWORK_DEV ds2.password=123456
xml
<bean id="dataSource" class="com.cnpc.framework.db.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="ds1" value-ref="ds1" /> <entry key="ds2" value-ref="ds2" /> </map> </property> <property name="defaultTargetDataSource" ref="ds1" /> </bean> <bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${ds1.driverClassName}" /> <property name="url" value="${ds1.url}" /> <property name="username" value="${ds1.username}" /> <property name="password" value="${ds1.password}" /> </bean> <bean id="ds2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${ds2.driverClassName}" /> <property name="url" value="${ds2.url}" /> <property name="username" value="${ds2.username}" /> <property name="password" value="${ds2.password}" /> </bean>
2. The source code of the Java file
com.cnpc.framework.db.DynamicDataSource
public class DynamicDataSource extends AbstractRoutingDataSource { /** * Get the currently used data source. */ @Override protected Object determineCurrentLookupKey() { return DbContextHolder.getDbType(); } public Logger getParentLogger() { // TODO Auto-generated method stub return null; } }
DbContextHolder source code
public class DbContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); /** * Set the current database. * @param dbType */ public static void setDbType(String dbType) { contextHolder.set(dbType); } /** * Get the current data source. * @return */ public static String getDbType() { String str = (String) contextHolder.get(); return str; } /** * clear context data */ public static void clearDbType() { contextHolder.remove(); } }
3. Test Code
public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception { DbContextHolder.setDbType("ds1"); List<DemoSysUser> list1 = demoSysUserService.getAll(); System.out.println(DbContextHolder.getDbType() + ".list.size()=" + list1.size()); DbContextHolder.setDbType("ds2"); List<DemoSysUser> list2 = demoSysUserService.getAll(); System.out.println(DbContextHolder.getDbType() + ".list.size()=" + list2.size()); DbContextHolder.clearDbType(); List<DemoSysUser> list = demoSysUserService.getAll(); ModelAndView mv = this.getAutoView().addObject("sysUserList", list); return mv; }