【问题标题】:Spring hibernate ehcache setup春季休眠 ehcache 设置
【发布时间】:2011-07-18 21:30:43
【问题描述】:

我在让休眠二级缓存用于缓存域对象时遇到了一些问题。根据ehcache documentation,将缓存添加到我现有的工作应用程序中应该不会太复杂。

我有以下设置(仅概述了相关的 sn-ps):

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE
public void Entity {
    // ... 
}

ehcache-entity.xml

<cache name="com.company.Entity" eternal="false"
    maxElementsInMemory="10000" overflowToDisk="true" diskPersistent="false"
    timeToIdleSeconds="0" timeToLiveSeconds="300"
    memoryStoreEvictionPolicy="LRU" />

ApplicationContext.xml

<bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="ds" />
    <property name="annotatedClasses">
        <list>
            <value>com.company.Entity</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="net.sf.ehcache.configurationResourceName">/ehcache-entity.xml</prop>
            <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>
            .... 
    </property>
</bean>

Maven 依赖项

   <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.4.0.GA</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-hibernate3</artifactId>
        <version>2.0.8</version>
        <exclusions>
            <exclusion>
                <artifactId>hibernate</artifactId>
                <groupId>org.hibernate</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.3.2</version>
    </dependency>

使用启用缓存统计的测试类:

    Cache cache = cacheManager.getCache("com.company.Entity");
    cache.setStatisticsAccuracy(Statistics.STATISTICS_ACCURACY_GUARANTEED);
    cache.setStatisticsEnabled(true);
    // store, read etc ... 
    cache.getStatistics().getMemoryStoreObjectCount(); // returns 0

似乎没有任何操作会触发任何缓存更改。我错过了什么?目前我在 DAO 中使用HibernateTemplate,也许这会产生一些影响。

[编辑]

设置为 DEBUG 时唯一的 ehcache 日志输出是:

SettingsFactory: Cache region factory : net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory

【问题讨论】:

  • 在加载 ecache xml 时日志是否会告诉您任何信息?
  • @heldt,我已更新我的帖子以包含 ehcache 警告

标签: java hibernate spring caching ehcache


【解决方案1】:

您是否需要手动告诉 Hibernate 使用 EHCache 提供程序?我从来没有真正确定这是否是必需的,但是 Hibernate 确实支持许多缓存提供程序,所以我怀疑可能有必要明确告诉 Hibernate 你想要哪个。尝试将此属性添加到 ApplicationContext.xml

<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>

【讨论】:

  • 感谢您的回复,根据the docs,因为休眠 3.3 不需要该属性。
【解决方案2】:

我已经确定了几个原因:

  1. 正确的maven依赖:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.3.Final</version>
    </dependency>
    
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.4.1</version>
    </dependency>
    
  2. @Cacheable 注释从javax.persistence 添加到我的实体。

  3. 从 hibernate 而不是 ehcache 读取日志记录。

    getSessionFactory().getStatistics().logSummary();

  4. 似乎并非所有休眠操作都会影响缓存。这我需要进一步阅读。

【讨论】:

    【解决方案3】:

    通过查看您的配置,一切似乎都很好。唯一值得注意的是,在使用 HibernateTemplate 时,如果您打算使用查询缓存,则必须显式设置缓存查询(true)...我不建议这样做,除非您真的需要它:http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/HibernateTemplate.html#setCacheQueries(boolean)

    您是否尝试过使用 Hibernate 统计信息而不是 Ehcache 统计信息?你在那里得到缓存未命中吗? (我问的原因是确保您使用与 Hibernate 相同的 CacheManager)...

    【讨论】:

    • 我不打算使用查询缓存,因此它已被禁用,但感谢您的注意。我也在重构hibernatetemplate的过程中,所以我会在完成后回来:)
    【解决方案4】:

    你可以参考下面的配置

    <prop key="hibernate.cache.use_query_cache">true</prop>
    

    【讨论】:

      【解决方案5】:

      hibernate.cfg.xml 中添加:

       <hibernate-configuration>
         <session-factory>
           ...
           <property name="cache.use_second_level_cache">true</property>
           <property name="cache.use_query_cache">true</property>
           <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
      

      【讨论】:

        猜你喜欢
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        • 2013-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-25
        • 2016-12-14
        相关资源
        最近更新 更多