0

In my application I have included the following xml configuration for EhCache (version 2.4.3 of ehcache-core)

<?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>

Cache manager is declared as

<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>

Using a bean I add parameters to this cache (by autowiring the cacheManager in Spring). There is no error in the application, but when I check the caches in JavaMelody (Data cashes section), I see that MyParamsCache appears twice. One instance is filled with content, while the other one is empty.

Does anyone have any idea why MyParamsCache appears twice and how to remove the dummy instance?

EDITED

I also have hibernate second level cache in persistence.xml. The cache bean is created again there, instead of using the already existing one. The code is

<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>
dchar
  • 1,665
  • 2
  • 19
  • 28

3 Answers3

1

Problem is solved is file ehcache.xml is moved inside META-INF folder. Bean declaration is updated as

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:META-INF/ehcache.xml" />
</bean>
dchar
  • 1,665
  • 2
  • 19
  • 28
0

i could solve the problem by adding ehcache as depends-on to the entityManagerFactory

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

and in persistence.xml i configured

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

i would be interrested in suggestions how i could achieve this without having to use SingletonEhCacheRegionFactory though.

cproinger
  • 2,258
  • 1
  • 17
  • 33
0

I posted this as an issue at https://github.com/javamelody/javamelody/issues/932 and found with the help of https://github.com/evernat the solution. For me, the solution was

  1. To set the property shared of the main spring chacheManager true
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
        <property name="shared" value="true"/>
    </bean>
  1. To use SingletonEhCacheProvider as the 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>
shaack
  • 171
  • 8