2

尝试使用 Spring 和 Hibernate 设置二级缓存。

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:database="${jdbc.databaseType}" p:generateDdl="true"
              p:showSql="true"/>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            <prop key="hibernate.ejb.naming_strategy">com.antaresgames.ad.lobby.repository.FixedDefaultComponentSafeNamingStrategy</prop>
            <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.cache.infinispan.tm.HibernateTransactionManagerLookup</prop>
            <prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>

            <!--https://docs.jboss.org/author/display/ISPN/Using+Infinispan+as+JPA-Hibernate+Second+Level+Cache+Provider-->
            <!--CACHE CONFIGURATION-->
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.use_query_cache">false</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.infinispan.JndiInfinispanRegionFactory</prop>
            <prop key="hibernate.cache.infinispan.cachemanager">java:Cachemanager</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.infinispan.InfinispanRegionFactory</prop>
            <prop key="hibernate.cache.infinispan.statistics">true</prop>
            <!--https://docs.jboss.org/author/display/ISPN/Eviction-->
            <!--<prop key="hibernate.cache.infinispan.entity.eviction.strategy">LIRS</prop>-->
            <!--https://docs.jboss.org/author/display/ISPN/Eviction+Examples-->
            <!--<prop key="hibernate.cache.infinispan.entity.eviction.wake_up_interval">2000</prop>-->
            <!--https://docs.jboss.org/author/display/ISPN/Eviction-->
            <!--<prop key="hibernate.cache.infinispan.entity.eviction.max_entries">5000</prop>-->
            <!--max time entity is living in memory in idle-->
            <!--<prop key="hibernate.cache.infinispan.entity.expiration.max_idle">60000</prop>-->
        </props>
    </property>
</bean>

但是由于缺乏 Hibernate 4 的文档,我认为我无法正确设置缓存。

启动应用程序时,我看到如下警告:

jta.JtaTransaction - HHH000426: You should set hibernate.transaction.manager_lookup_class if cache is enabled

并且在获取存储库的实体时,它每次都会进行查询。

有什么建议么?

4

1 回答 1

0

您应该只需要以下缓存/tm 相关属性:

<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.infinispan.InfinispanRegionFactory</prop>
<prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform</prop>
<prop key="hibernate.cache.infinispan.cfg">infinispan.xml</prop>

请注意,根据您的需要/配置,您可能需要将 InfinispanRegionFactory 替换为 JndiInfinispanRegionFactory。

于 2014-05-19T13:10:31.247 回答