调查PROXOOL数据源动态切换的时候发现切换不成功,换了其他的数据源就没有这个问题。不甘心只好DEBUG proxool的源代码,经过两小时的努力终于找到了问题所在,proxool在生成connection pool的时候会根据数据源数量生成数据连接池MAP,而列表是根据数据源的别名alias属性做为KEY来定义的,而通过AbstractRoutineDataSource进行数据源切换时会查找数据连接池MAP获取连接池,而如果没有配置alias属性就会导致map的主键为null,当然就取不到匹配的连接池了。alias在但数据源时候是个可有可无的属性,没想到在配置多数据源时如此重要。。。
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSourceType();
}
}
public class DataSourceMap {
public static final String HICHANCE = "hichance";
public static final String CHINAOCS = "chinaocs";
}
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.set(null);
}
}
<bean id="hichanceDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"
p:driverUrl="#{dsProps['driverUrl']}" p:driver="#{dsProps['driver']}"
p:user="#{dsProps['user']}" p:password="#{dsProps['password']}"
p:simultaneousBuildThrottle="#{dsProps['simultaneousBuildThrottle']}"
p:prototypeCount="#{dsProps['prototypeCount']}"
p:maximumConnectionCount="#{dsProps['maximumConnectionCount']}"
p:minimumConnectionCount="#{dsProps['minimumConnectionCount']}"
p:maximumActiveTime="#{dsProps['maximumActiveTime']}"
p:houseKeepingTestSql="SELECT 1" p:testBeforeUse="true"
p:statisticsLogLevel="DEBUG" p:trace="true" p:alias="hichanceDataSource" />
<bean id="chinaocsDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"
p:driverUrl="#{dsProps['codriverUrl']}" p:driver="#{dsProps['codriver']}"
p:user="#{dsProps['couser']}" p:password="#{dsProps['copassword']}"
p:simultaneousBuildThrottle="#{dsProps['simultaneousBuildThrottle']}"
p:prototypeCount="#{dsProps['prototypeCount']}"
p:maximumConnectionCount="#{dsProps['maximumConnectionCount']}"
p:minimumConnectionCount="#{dsProps['minimumConnectionCount']}"
p:maximumActiveTime="#{dsProps['maximumActiveTime']}"
p:houseKeepingTestSql="SELECT 1" p:testBeforeUse="true"
p:statisticsLogLevel="DEBUG" p:trace="true" p:alias="chinaocsDataSource" />
<bean id="dataSource" class="com.risingdl.hichance.data.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="hichance" value-ref="hichanceDataSource" />
<entry key="chinaocs" value-ref="chinaocsDataSource" />
</map>
</property>
<property name="defaultTargetDataSource" ref="hichanceDataSource" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.risingdl.*.entity</value>
<value>com.risingdl.*.entity.*</value>
<value>com.risingdl.hichance.*.entity</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>