views:

48

answers:

1

Hi there,

I need to set up multiple Sessionfactories in my app, now I'm facing a problem. I can't use the 2nd level cache at the moment because only the cache from the first factory is returned. Providing a hibernate.cache.region_prefix would solve the problem I guess. How can I supply for each factory a own region per Spring XML config?

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

I don't want to provide the properties per sessionfactory is this even possible? I'm using Spring 3.0.2, Hibernate 3.5

+1  A: 

Ok hibernate.cache.region_prefix fixes my problem. I solved the problem by a workaround.

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>

Sure it isn't a really clean or generic solution but fits my needs for now. Probably someone else can provide a much smoother solution.

asrijaal