Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ehcache xml需要defaultCache元素,其他缓存正在获取defaultCache属性_Java_Hibernate_Jpa_Ehcache - Fatal编程技术网

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

Java ehcache xml需要defaultCache元素,其他缓存正在获取defaultCache属性,java,hibernate,jpa,ehcache,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 nam

这就是我的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>

My 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秒后过期

  • 我不需要defaultCache,但从ehcache.xml中删除它会在tomcat启动时引发异常。强迫我将其添加到ehcache.xml。我知道每个ehcache文档都不需要它,但不确定是什么原因导致需要它
  • 为什么timeToLiveSeconds是从defaultCache继承的
  • 解决其中任何一个问题都可以解决我的问题,但如果两者都得到解决,那就太好了


    谢谢,

    使用Hibernate时,需要创建大量缓存。除非在配置中明确定义它们,否则将使用
    defaultCache
    机制

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

    因此,有两种选择:

  • 根据需要配置
    defaultCache
  • 识别应用程序需要的所有缓存名称,并明确定义它们

  • 名为
    PreferenceValueEntity
    的实体的缓存名称必须是该实体的完全限定类名。例如,
    com.my.package.PreferenceValueEntity
    (我不知道
    PreferenceValueEntity
    的包名是什么,所以我只是在这里编造^^^)

    因此,您的配置应该如下所示:

    <cache name="com.my.package.PreferenceValueEntity" eternal="false" maxElementsInMemory="1000"
            timeToIdleSeconds="5" timeToLiveSeconds="5" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" />
    
    
    
    这是一个很好的例子


    提供了使用Hibernate二级缓存的良好教程。

    有什么例外?