環境:Spring 3.0 ,Hibernate 3.5 ,同類型資料庫(DB2)
編前語:此片僅粗略的描述使用Spring和Hibernate采用注入方式管理多資料源在不同僚務的情況下使用的方法。
涉及到的檔案三個:testDaoImpl (資料庫操作檔案),applicationContext.xml(Spring主配置檔案) ,jdbc.properties (資料庫連接配接和各種資料庫相關配置檔案)
總體分三步:
1.增加資料庫連接配接
2.在Spring配置中增加新的資料源配置
3.在需要使用不同資料源的操作實作類中指定新的sessionFactory和transactional
具體使用修改如下(注意:所有配置僅用于多資料源連接配接測試,并非優化後配置,套用需謹慎!):
1.增加資料庫連接配接配置(該配置檔案中包含了cpool的配置和hibernate的配置資訊在xml配置中直接調用) jdbc.properties
jdbcq.driverClassName=com.db2.jcc.DB2Driver
jdbcq.url=jdbc:db2://192.168.1.11:50001/DB01:currentSchema=USER1;
jdbcq.username=user1
jdbcq.password=user1
2.在Spring配置中增加新的資料源配置 applicationContext.xml
(前提配置)
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
新增資料源:
<bean id="dataSourceQ" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbcq.driverClassName}" />
<property name="jdbcUrl" value="${jdbcq.url}" />
<property name="user" value="${jdbcq.username}" />
<property name="password" value="${jdbcq.password}" />
<property name="autoCommitOnClose" value="true" />
<property name="checkoutTimeout" value="${cpool.checkoutTimeout}" />
<property name="initialPoolSize" value="${cpool.minPoolSize}" />
<property name="minPoolSize" value="${cpool.minPoolSize}" />
<property name="maxPoolSize" value="${cpool.maxPoolSize}" />
<property name="maxIdleTime" value="${cpool.maxIdleTime}" />
<property name="acquireIncrement" value="${cpool.acquireIncrement}" />
<property name="acquireRetryAttempts" value="30" />
<property name="acquireRetryDelay" value="1000" />
<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}" />
</bean>
sessionFactory配置:
此處實體包是注解方式必須有的,隻有在sessionFactory 中進行掃描過的實體包才能在注入該sessionFactory的資料庫操作impl中操作。否則會出現找不到對應實體的異常。此處僅掃描用于測試的包。
另外一點需要注意的是如果配置二級緩存那麼需要使用不同的配置檔案和配置,否則會出現緩存空間已經被使用的異常。當然用于測試可以直接去掉這部配置設定置
此處建立了另外一個配置檔案,同時配置了不同的緩存空間
<!-- 新的sessioinFactory-->
<bean id="sessionFactoryQ"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 新的資料源-->
<property name="dataSource" ref="dataSourceQ" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<!-- 使用ehcache -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<!-- 最優化二級緩存 -->
<prop key="hibernate.cache.use_structured_entries">true</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache-hibernate-q.xml</prop>
</props>
</property>
<!-- 掃描實體所在包 隻掃描業務包-->
<property name="packagesToScan" >
<list>
<value>com.back.entities.test</value>
</list>
</property>
</bean>
事務管理配置:
<!-- 指定事務管理 -->
<bean id="transactionManagerQ" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryQ" />
</bean>
<!-- 使用annotation定義事務 -->
<tx:annotation-driven transaction-manager="transactionManagerQ" />
3.在需要使用不同資料源的操作實作類中指定新的sessionFactory和transactional
@Repository
@Transactional("transactionManagerQ")
public class TestDaoImpl implements TestDao {
private SessionFactory sessionFactory;
@Resource(name="sessionFactoryQ")
public void setSessionFactory(SessionFactory sessionFactoryQ) {
this.sessionFactory = sessionFactoryQ;
}
}
大概就是這樣,需要配置和修改的地方都如上已經處理了。因為是注解是以和純xml配置會有不同的地方。資料庫操作的實作類在項目中其實會繼承架構的一個實作類,但為了便于看到全貌,是以修改成以上的樣子,進而可以看到sessionFactory的設定。
轉載于:https://www.cnblogs.com/justbeginning/p/3487252.html