Rasoul
Rasoul

Reputation: 187

ehcache xml requires the defaultCache element and other caches are getting defaultCache properties

This is how my ehcache.xml look like:

<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 contains this:

<!-- 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"/>

I'm using - JPA and Hibernate 5.2.x - ehcache-2.10.3

The problem is that the timeToIdleSeconds are inherited from defaultCache and thus cache expires after 10 sec rather than 5 sec.

  1. I don't need the defaultCache, but removing it from ehcache.xml throws exception on tomcat startup. Forcing me to add it to ehcache.xml. I know per ehcache documentation it is not required, but not sure what's causing it to be required.
  2. Why timeToLiveSeconds is inherited from defaultCache.

Resolving any of them will resolve my issue, but if both are resolved that would be great.

Thanks,

Upvotes: 2

Views: 852

Answers (2)

StvnBrkdll
StvnBrkdll

Reputation: 4044

Your cache name for the Entity named PreferenceValueEntity needs to be the fully qualified class name for the entity. For instance com.my.package.PreferenceValueEntity (I don't know what PreferenceValueEntity's package name is so I am just making it up here ^^).

So your config should look like:

<cache name="com.my.package.PreferenceValueEntity" eternal="false" maxElementsInMemory="1000"
        timeToIdleSeconds="5" timeToLiveSeconds="5" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" />

This explanation in the ehcache documentation gives a good example.

This post gives a good tutorial on using Hibernate's second level cache.

Upvotes: 0

Louis Jacomet
Louis Jacomet

Reputation: 14500

When using Hibernate, quite a number of caches need to be created. Unless you define them all explicitly in your configuration, the defaultCache mechanism is used.

That means that when Hibernate requires a cache, it will request it from the CacheManager and if that cache does not exist, Ehcache will use the defaultCache definition to create it.

So two options:

  1. Configure the defaultCache according to your needs
  2. Identify all cache names that your application needs and define them all explicitly.

Upvotes: 1

Related Questions