Reputation: 187
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.
Resolving any of them will resolve my issue, but if both are resolved that would be great.
Thanks,
Upvotes: 2
Views: 852
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
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:
defaultCache
according to your needsUpvotes: 1