Spring,多个Hibernate Sessionfactories配置

时间:2010-08-20 07:36:03

标签: java hibernate spring ehcache

我需要在我的应用中设置多个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.properties

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

2 个答案:

答案 0 :(得分:1)

确定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>

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

答案 1 :(得分:0)

您是否尝试将net.sf.ehcache.hibernate.SingletonEhCacheProvider用作缓存提供程序,因为它的唯一目的是支持多个会话工厂并使用单例缓存。