【问题标题】:Ehcache configuration with no effectehcache配置无效
【发布时间】:2015-04-26 21:39:56
【问题描述】:

我已经阅读了很多答案并查看了 ehcache 文档,但我无法找到解决此问题的方法。 Ehache 只是不想为我工作。

我正在使用 spring 4.1.6 RELEASE 和 hibernate 4.3.5 Final,我想启用缓存,这样我就不必每次都访问 DB。

这是我的 pom.xml 的一部分:

     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework-version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate-version}</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-ehcache</artifactId>
      <version>${hibernate-version}</version>
    </dependency>

hibernate.cfg.xml 的相关部分:

<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>

这是ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"  updateCheck="true" monitoring="autodetect" dynamicConfig="true">

<diskStore path="java.io.tmpdir"/>

<defaultCache
    eternal="false"
    maxElementsInMemory="100"
    timeToIdleSeconds="120"
    timeToLiveSeconds="120"
    diskExpiryThreadIntervalSeconds="120"
    memoryStoreEvictionPolicy="LRU">
</defaultCache>

<cache name="model.entities.User" maxElementsInMemory="100" eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600" />

</ehcache>

一个问题是,如果我将 maxEntriesLocalHeap 属性添加到 defaultCache,我会得到 ​​p>

error:    Element <defaultCache> does not allow attribute "maxEntriesLocalHeap".

虽然 XSD 将“maxEntriesLocalHeap”指定为 defaultCache 的属性。 我不太关心那个参数,所以我删除了它,但这很奇怪。 好吧,至少我知道一些 :) 阅读 ehcache.xml。 我知道这也是因为我得到这样的输出:

WARN : org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory - HHH020003: Could not find a specific ehcache configuration for cache named [model.entities.Role]; using defaults.

而且这个输出中没有 User 类,所以在 ehcache.xml 中添加最后一个缓存标签确实有一些效果。

我必须说,我也在每个 hbm.xml 文件中加入了这一行:

<cache usage="read-write" />

似乎一切正常,我没有收到任何错误,但是查看 mysql 日志文件显示 mysql 服务器非常频繁地使用相同的参数获取相同的选择查询,尽管该行没有被修改。这就是我想要避免的。

我没有在 spring 配置文件中做任何特定的事情。 我应该吗?

提前感谢您的帮助。我要疯了。

【问题讨论】:

  • 如果 XSD 允许某些东西并且运行时失败 - 您必须查看/使用不同版本的包
  • 回复:您的缓存使用率 - 您的 maxElementsInMemory 和 timeToLiveSeconds 值非常低,这可能会导致您看到的行为。运行查询时尝试转储 ehcache 统计信息
  • 不知道你说不同版本的包是什么意思?我不太确定如何转储这些统计信息,现在我所在的时区已经很晚了,所以我暂时搁置它。我从没想过用hibernate启用缓存会这么难。无论如何,感谢您的尝试。

标签: java spring hibernate maven ehcache


【解决方案1】:

我很着急,我认为这可以开箱即用,无需对代码进行任何更改。但事实并非如此。

几天后,当我终于有时间认真对待这个问题时,我发现我从未将 Hibernate Query 对象的可缓存属性设置为 true。

像这样:

query.setCacheable(true);

就是这样。现在可以了。 事务支持仍然存在一些问题,但缓存工作很重要:)

【讨论】:

    猜你喜欢
    • 2015-03-04
    • 2015-04-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 2012-03-27
    • 1970-01-01
    相关资源
    最近更新 更多