gpt4 book ai didi

java - Spring,多个Hibernate Sessionfactories配置

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:18:37 25 4
gpt4 key购买 nike

我需要在我的应用程序中设置多个 Sessionfactories,现在我遇到了一个问题。我现在不能使用二级缓存,因为只返回一级工厂的缓存。提供 hibernate.cache.region_prefix 可以解决我猜想的问题。我如何为每个工厂提供每个 Spring XML 配置自己的区域?

applicationContext.xml

<bean id="hibernateProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>/WEB-INF/hibernate/hibernate.properties</value>
</list>
</property>
</bean>

<bean id="cacheRegionFactory" class="org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge">
<constructor-arg>
<props>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</constructor-arg>
</bean>

<bean id="factory_1"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="db2Datasource" />
<property name="mappingDirectoryLocations" value="classpath:de/ac/hibernate" />
<property name="hibernateProperties" ref="hibernateProperties" />
<property name="cacheRegionFactory" ref="cacheRegionFactory" />
</bean>

<bean id="factory_2"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="db2Datasource2" />
<property name="mappingDirectoryLocations" value="classpath:de/ac/hibernate" />
<property name="hibernateProperties" ref="hibernateProperties" />
<property name="cacheRegionFactory" ref="cacheRegionFactory" />
</bean>

hibernate 属性

hibernate.dialect=org.hibernate.dialect.DB2400Dialect
hibernate.connection.autocommit=false
hibernate.connection.charset=UTF-8
hibernate.show_sql=false
hibernate.cache.use_second_level_cache=false
hibernate.generate_statistics=false

我不想为每个 sessionfactory 提供属性,这可能吗?我正在使用 Spring 3.0.2Hibernate 3.5

最佳答案

好的 hibernate.cache.region_prefix 解决了我的问题。我通过变通方法解决了这个问题。

LocalSessionFactoryBeanMod.class

public class LocalSessionFactoryBeanMod extends LocalSessionFactoryBean {

private String cacheRegion;

public String getCacheRegion() {
return this.cacheRegion;
}

public void setCacheRegion(String cacheRegion) {
this.cacheRegion = cacheRegion;
getHibernateProperties().put("hibernate.cache.region_prefix", cacheRegion);
}

@Override
public void setHibernateProperties(Properties hibernateProperties) {
if (getHibernateProperties().isEmpty()) {
super.setHibernateProperties(hibernateProperties);
} else {
getHibernateProperties().putAll(hibernateProperties);
}
}
}

applicationContext.xml

<bean id="factory_1"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="db2Datasource" />
<property name="mappingDirectoryLocations" value="classpath:de/ac/hibernate" />
<property name="hibernateProperties" ref="hibernateProperties" />
<property name="cacheRegionFactory" ref="cacheRegionFactory" />
<property name="cacheRegion" value="ip_10_5_14_5_" />
</bean>

当然,这不是一个真正干净或通用的解决方案,但符合我现在的需求。可能其他人可以提供更流畅的解决方案。

关于java - Spring,多个Hibernate Sessionfactories配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3528989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com