What is the default caching time when using Hibernate's 2nd level cache

cometta picture cometta · Mar 18, 2010 · Viewed 11.7k times · Source

When using Hibernate 2nd level cache and query cache and not specifying anything inside ehcache.xml, what is the default caching time?

Answer

Pascal Thivent picture Pascal Thivent · Mar 18, 2010

Taken from the documentation on Cache Configuration:

The following attributes and elements are optional.

timeToIdleSeconds:
Sets the time to idle for an element before it expires.
i.e. The maximum amount of time between accesses before an element expires
Is only used if the element is not eternal.
Optional attribute. A value of 0 means that an Element can idle for infinity.
The default value is 0.

timeToLiveSeconds:
Sets the time to live for an element before it expires.
i.e. The maximum time between creation time and when an element expires.
Is only used if the element is not eternal.
Optional attribute. A value of 0 means that and Element can live for infinity.
The default value is 0.

Note that EHCache uses a timeToLive, not an expire time and the default is 0 if not specified.


Update: While the above about defaults when configuring a cache is true, it appears that these defaults don't apply if you don't provide any ehcache.xml. So I dug a bit further and I think that EHCache may actually always use a defaultCache in that case - including for the StandardQueryCache - and this defaultCache has a timeToLive of 2 minutes:

<defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU"
        />

I can't confirm this right now but this is what I would do:

  • first, activate logging on EHCache, EHCache logs a warning when the defaultCache is used:

While the defaultCache is a great convenience, it is preferable for each Cache to be configured individually. For this reason a log warning level message is issued each time a cache is created based on the defaultCache values.