Spring Hibernate Multiple Sleep Configuration

I need to set up several Sessionfactories in my application, now I am facing a problem. I cannot use the second level cache at the moment, because only the cache from the first factory is returned. Giving hibernate.cache.region_prefixwill solve the problem, I think. How can I provide each factory with its own region in the Spring XML configuration?

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 do not want to provide properties on sessionfactory, is this even possible? I'm using Spring 3.0.2,Hibernate 3.5

+3
source share
2 answers

Ok 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

net.sf.ehcache.hibernate.SingletonEhCacheProvider , - --.

0

Source: https://habr.com/ru/post/1760693/


All Articles