【问题标题】:EhCache appears twice in JavaMelodyEhCache 在 JavaMelody 中出现两次
【发布时间】:2015-02-02 11:00:44
【问题描述】:

在我的应用程序中,我为 EhCache(ehcache-core 版本 2.4.3)包含了以下 xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd" 
         updateCheck="false"
         monitoring="autodetect" 
         dynamicConfig="true">

    <defaultCache  
        eternal="false"
        timeToIdleSeconds="0" 
        timeToLiveSeconds="0">
    </defaultCache>

    <cache name="MyParamsCache" 
        eternal="true" 
        overflowToDisk="true"
        diskSpoolBufferSizeMB="20" 
        memoryStoreEvictionPolicy="LRU"
        statistics="true">
    </cache>

</ehcache>

缓存管理器被声明为

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehcache" />
</bean>

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml" />
</bean>

我使用 bean 向这个缓存添加参数(通过在 Spring 中自动装配 cacheManager)。应用程序中没有错误,但是当我检查 JavaMelody(Data cashes 部分)中的缓存时,我看到 MyParamsCache 出现了两次。一个实例充满了内容,而另一个是空的。

有谁知道为什么MyParamsCache 出现两次以及如何删除虚拟实例?

已编辑

我在 persistence.xml 中也有休眠二级缓存。缓存 bean 在那里再次创建,而不是使用已经存在的。代码是

<persistence-unit name="myPU">
        .....
        <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect" />
                <property name="hibernate.hbm2ddl.auto" value="validate" />
                <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" />
        </properties>
</persistence-unit>

【问题讨论】:

    标签: spring ehcache java-melody


    【解决方案1】:

    问题已解决,将文件 ehcache.xml 移动到 META-INF 文件夹中。 Bean 声明更新为

    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:META-INF/ehcache.xml" />
    </bean>
    

    【讨论】:

      【解决方案2】:

      我可以通过将 ehcache 作为依赖项添加到 entityManagerFactory 来解决问题

              <bean
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
              id="entityManagerFactory" 
              depends-on="ehCache">   
      

      在我配置的 persistence.xml 中

      <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory" />
      

      我会对如何在不必使用 SingletonEhCacheRegionFactory 的情况下实现这一点的建议感兴趣。

      【讨论】:

        【解决方案3】:

        我将此作为问题发布在https://github.com/javamelody/javamelody/issues/932 并在https://github.com/evernat 的帮助下找到了解决方案。对我来说,解决方案是

        1. 设置主弹簧chacheManagertrue的属性shared
            <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
                <property name="configLocation" value="classpath:ehcache.xml"/>
                <property name="shared" value="true"/>
            </bean>
        
        1. 使用SingletonEhCacheProvider 作为hibernate.cache.provider_class
            <prop key="hibernate.cache.use_second_level_cache">true</prop>   
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.SingletonEhCacheProvider</prop>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多