java - ehcache xml 需要 defaultCache 元素,其他缓存正在获取 defaultCache 属性

标签 java hibernate jpa ehcache

这就是我的 ehcache.xml 的样子:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"
    updateCheck="false" name="defaultCache1">
    <diskStore path="java.io.tmpdir" />
    <defaultCache name="defaultCache" maxElementsInMemory="10000" eternal="false" statistics="true" timeToIdleSeconds="10"
        timeToLiveSeconds="10" overflowToDisk="false" diskPersistent="false" memoryStoreEvictionPolicy="LRU" /> 

    <cache name="PreferenceValueEntity" eternal="false" maxElementsInMemory="1000"
        timeToIdleSeconds="5" timeToLiveSeconds="5" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" />
</ehcache>

我的 persistence.xml 包含以下内容:

<!-- EHCache managed by hibernate -->           
        <property name="hibernate.cache.use_second_level_cache" value="true" />
        <property name="hibernate.cache.use_query_cache" value="true" />
        <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
        <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" />
        <property name="net.sf.ehcache.configurationResourceName" value="/META-INF/ehcache.xml"/>

我正在使用 - JPA 和 Hibernate 5.2.x - ehcache-2.10.3

问题在于 timeToIdleSeconds 继承自 defaultCache,因此缓存会在 10 秒而不是 5 秒后过期。

  1. 我不需要 defaultCache,但从 ehcache.xml 中删除它会在 tomcat 启动时引发异常。强制我将其添加到 ehcache.xml 中。我知道根据 ehcache 文档它不是必需的,但不确定是什么原因导致需要它。
  2. 为什么 timeToLiveSeconds 继承自 defaultCache。

解决其中任何一个问题都会解决我的问题,但如果两个问题都得到解决那就太好了。

谢谢

最佳答案

使用Hibernate时,需要创建相当多的缓存。除非您在配置中显式定义它们,否则将使用 defaultCache 机制。

这意味着当 Hibernate 需要缓存时,它会向 CacheManager 请求它,如果该缓存不存在,Ehcache 将使用 defaultCache 定义来创建它.

有两个选择:

  1. 根据您的需要配置defaultCache
  2. 确定您的应用程序需要的所有缓存名称并明确定义它们。

关于java - ehcache xml 需要 defaultCache 元素,其他缓存正在获取 defaultCache 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45843656/

相关文章:

java - JDOM 对 ID 和 IDREF 有支持吗?

java - java中删除数组中的空元素

java - 嵌套的 Hibernate 实体

hibernate - JPA2 中分离对象的问题

java - 数组的 volatile 数组

java - 安卓 : hide/show footer/header as scrolling scrollview down/up

Hibernate 实体作为静态类

mysql - 用于维护记录草稿副本的数据库设计模式

jpa - JPA utf-8字符未保留

java - 尝试在 Java 中将 List 从数据库转换为 json 时的 Stackoverflow