为了方便sessionFactory控制,或进行数据同步等事务。。。。。。。项目需要多个数据库连接池,并使用一个sessionfactory管理。
<bean id="dataSourceXGRWW"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.exchangww</value>
</property>
</bean>
<bean id="dataSourceJFWAP"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.exchangwap</value>
</property>
</bean>
<bean id="dataSource" class="cn.com.xgr.dynamicds.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="XGRWW" value-ref="dataSourceXGRWW"/>
<entry key="JFWAP" value-ref="dataSourceJFWAP"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSourceJFWAP"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.autoReconnect">true</prop>
<!-- Proxool Config -->
<prop key="hibernate.proxool.xml">proxool.xml</prop>
<prop key="hibernate.proxool.existing_pool">true</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">25</prop>
<prop key="hibernate.statement_cache.size">25</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:cn/com/xgr/po</value>
</list>
</property>
</bean>
dataSource由cn.com.xgr.dynamicds.DynamicDataSource动态管理,这是关键
public class DynamicDataSource extends AbstractRoutingDataSource {
protected Object determineCurrentLookupKey() {
// TODO Auto-generated method stub
return CustomerContextHolder.getCustomerType();
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
public class CustomerContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setCustomerType(String customerType) {
contextHolder.set(customerType);
}
public static String getCustomerType() {
return (String) contextHolder.get();
}
public static void clearCustomerType() {
contextHolder.remove();
}
}
public class DataSourceMap {
public static final String XGRWW = "XGRWW"; //有数据的端口
public static final String JFWAP = "JFWAP"; //无数据的端口
}
在需要使用的时候只需要提前切换即可
CustomerContextHolder.setCustomerType(DataSourceMap.JFWAP);//设置数据源
this.updateJFApplyMainByCheckstate(jfmVo, jfmPo);//更新wap.Checkstate字段
CustomerContextHolder.setCustomerType(DataSourceMap.XGRWW);//设置数据源
当然上面的配置使用了proxool这部分的数据库连接池,不熟悉的可以换成myeclipse自带的数据源配置也是可以的