代码之家  ›  专栏  ›  技术社区  ›  onigunn

Spring,多个Hibernate Sessionfactories配置

  •  3
  • onigunn  · 技术社区  · 15 年前

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

    应用程序上下文.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.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
    

    Spring 3.0.2 , Hibernate 3.5

    2 回复  |  直到 15 年前
        1
  •  1
  •   onigunn    15 年前

    好 啊 hibernate.cache.region_prefix

    当地人SessionFactoryBeanMod.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);
            }
        }
    }
    

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

    当然,这不是一个真正干净或通用的解决方案,但适合我现在的需要。也许其他人可以提供一个更平滑的解决方案。

        2
  •  0
  •   Jay    10 年前

    你试过用吗 net.sf.ehcache.hibernate.SingletonEhCacheProvider