SpringMVC動態切換多個數據源解決方案
1.數據源配置
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="coreDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>${db.mysql.core.url}</value>
</property>
<property name="username">
<value>${db.mysql.core.username}</value>
</property>
<property name="password">
<value>${db.mysql.core.password}</value>
</property>
<property name="initialSize" value="1"/>
<property name="maxTotal" value="20"/>
<property name="maxIdle" value="20"/>
<property name="minIdle" value="1"/>
<property name="maxWaitMillis" value="60000"/>
<property name="validationQuery" value="select 1 from dual"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="28800000"/>
</bean>
<bean id="reportDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>${db.mysql.report.url}</value>
</property>
<property name="username">
<value>${db.mysql.report.username}</value>
</property>
<property name="password">
<value>${db.mysql.report.password}</value>
</property>
<property name="initialSize" value="1"/>
<property name="maxTotal" value="20"/>
<property name="maxIdle" value="20"/>
<property name="minIdle" value="1"/>
<property name="maxWaitMillis" value="60000"/>
<property name="validationQuery" value="select 1 from dual"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="28800000"/>
</bean>
<bean id="dataSource" class="com.xiaomi.dev.service.datasource.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="coreDataSource" key="core"></entry>
<entry value-ref="reportDataSource" key="report"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="coreDataSource"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:sqlmap/*.xml"></property>
</bean>
<bean class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
2. DataSourceConst.java
public class DataSourceConst {
public static final String CORE = "core";
public static final String REPORT = "report";
}
3. DynamicDataSource.java
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSourceType();
}
}
4. DataSourceContextHolder.java
public class DataSourceContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setDataSourceType(String dataSourceType) {
contextHolder.set(dataSourceType);
}
public static String getDataSourceType() {
return (String) contextHolder.get();
}
public static void clearDataSourceType() {
contextHolder.remove();
}
}
5. 數據庫查詢前切換到指定庫
DataSourceContextHolder.setDataSourceType(DataSourceConst.REPORT);
DataSourceContextHolder.setDataSourceType(DataSourceConst.CORE);