spring配置文件:
后台引用,在增删改查的时候要指定数据源方式:
引用的类:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName" default-lazy-init="true">
<!-- Local DataSource that works in any environment -->
<bean id="syjdataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="minPoolSize" value="${c3p0.minPoolSize}"/>
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
<property name="testConnectionOnCheckin" value="${c3p0.testConnectionOnCheckin}"/>
<property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>
<property name="acquireRetryDelay" value="${c3p0.acquireRetryDelay}"/>
<property name="breakAfterAcquireFailure" value="${c3p0.breakAfterAcquireFailure}"/>
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>
<property name="automaticTestTable" value="${c3p0.automaticTestTable}"/>
</bean>
<!-- Local DataSource that works in any environment -->
<bean id="dutydataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${duty.jdbc.username}"/>
<property name="password" value="${duty.jdbc.password}"/>
<property name="minPoolSize" value="${duty.c3p0.minPoolSize}"/>
<property name="maxPoolSize" value="${duty.c3p0.maxPoolSize}"/>
<property name="initialPoolSize" value="${duty.c3p0.initialPoolSize}"/>
<property name="maxIdleTime" value="${duty.c3p0.maxIdleTime}"/>
<property name="acquireIncrement" value="${duty.c3p0.acquireIncrement}"/>
<property name="testConnectionOnCheckin" value="${duty.c3p0.testConnectionOnCheckin}"/>
<property name="acquireRetryAttempts" value="${duty.c3p0.acquireRetryAttempts}"/>
<property name="acquireRetryDelay" value="${duty.c3p0.acquireRetryDelay}"/>
<property name="breakAfterAcquireFailure" value="${duty.c3p0.breakAfterAcquireFailure}"/>
<property name="idleConnectionTestPeriod" value="${duty.c3p0.idleConnectionTestPeriod}"/>
<property name="automaticTestTable" value="${duty.c3p0.automaticTestTable}"/>
</bean>
<!-- 动态切换数据源 -->
<bean class="com.harmony.datasource.DynamicDataSource" id="dataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="syjdataSource" key="syjbs_event"></entry>
<entry value-ref="dutydataSource" key="syjbs_duty"></entry>
</map>
</property>
<!-- 设置数据源默认启动项-->
<property name="defaultTargetDataSource" ref="syjdataSource"></property>
</bean>
<!--Hibernate TransactionManager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--事务代理,如果service需要事务,从此处继承Base TransactionProxyed Service Bean-->
<bean id="baseTxService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="edit*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="JdbcDAOUtil"
class="com.harmony.framework.dao.impl.JdbcDAOImpl">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<!-- 定义lobHandler -->
<bean id="lobHandler" lazy-init="true" class="org.springframework.jdbc.support.lob.OracleLobHandler">
<property name="nativeJdbcExtractor">
<ref bean="nativeJdbcExtractor"/>
</property>
</bean>
<bean id="nativeJdbcExtractor" lazy-init="true" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor"/>
<!-- Session Factory set -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="lobHandler" ref="lobHandler"/>
<!-- 建议模块的hbm文件统一放置,此处添加模块的类路径,如:<value>classpath:/com/harmony/模块名称/domain/hbm/</value> -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/harmony/domain/hbm/</value>
<value>classpath:/com/harmony/duty/domain/hbm/</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="query.substitutions">true 1, false 0, yes '1', no '1'</prop>
<!--oracle 数据库必须添加上,否则内存越来越大,反应越来越慢!-->
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">25</prop>
<prop key="hibernate.jdbc.use_scrollable_resultset">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<!--<prop key="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</prop>-->
<!-- configure for WebLogic
<prop key="jta.UserTransaction">javax/transaction/UserTransaction</prop>
<prop key="hibernate.transaction.manager_lookup_class">net.sf.hibernate.transaction.WeblogicTransactionManagerLookup</prop>
-->
<!-- configure for WebSphere -->
<!--<prop key="jta.UserTransaction">${jta.userTransaction}</prop>
<prop key="hibernate.transaction.manager_lookup_class">${hibernate.transaction.manager_lookup_class}</prop>-->
</props>
</property>
</bean>
<bean id="hibernateTemplateInterceptor" class="com.harmony.framework.dbaccesslog.service.hibernate.HibernateTemplateInterceptor">
<property name="logEnable">
<value>false</value>
</property>
</bean>
<bean id="hibernateTemplateTarget" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<constructor-arg>
<ref local="sessionFactory"/>
</constructor-arg>
</bean>
<bean id="hibernateTemplate" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyTargetClass" value="true"/>
<property name="target">
<ref local="hibernateTemplateTarget"/>
</property>
<property name="interceptorNames">
<list>
<value>hibernateTemplateInterceptor</value>
</list>
</property>
</bean>
</beans>
后台引用,在增删改查的时候要指定数据源方式:
/**
* 根据指定的条件重置发送失败的数据,可以实现继续发送。
*
*/
public void updateReset() throws Exception {
CustomerContextHolder.setCustomerType(Constants.EVENT_SYJBS_DATASOURCE);
.......
}
引用的类:
package com.harmony.datasource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
protected Object determineCurrentLookupKey() {
return CustomerContextHolder.getCustomerType();
}
}
package com.harmony.datasource;
import org.springframework.util.Assert;
public class CustomerContextHolder {
@SuppressWarnings("unchecked")
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
@SuppressWarnings("unchecked")
public static void setCustomerType(String customerType) {
Assert.notNull(customerType, "customerType cannot be null");
contextHolder.set(customerType);
}
public static String getCustomerType() {
return (String) contextHolder.get();
}
public static void clearCustomerType() {
contextHolder.remove();
}
}