Hi,
im using ECCache as second level cache for hibernate and got some problems with the query cache.
Second level cache and query cache is activated for the SessionFactory:
Code:
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
Cache configuration:
Code:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache
name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="5"
eternal="false"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<cache
name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="true"
/>
</ehcache>
Ive got an entity A (PlanImpl) with a collection (meisterschaften) of entities B (MeisterschaftImpl). All are marked to be cached:
Code:
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
Ive got a query for getting an entity A by ID which joins the collection of Bs (the collection is initialized after retrieving the object A).
While executing the query for the first time, everything seems to be fine. The query is executed and all objects are put into the cache. Here is the log:
Code:
2009-02-09 12:11:04,687 DEBUG [http-8888-Processor24] StandardQueryCache.get - checking cached query results in region: org.hibernate.cache.StandardQueryCache
2009-02-09 12:11:04,687 DEBUG [http-8888-Processor24] EhCache.get - key: sql: select ... where this_.ID = ?; parameters: 1, ; transformer: org.hibernate.transform.DistinctRootEntityResultTransformer@d2fb67
2009-02-09 12:11:04,702 DEBUG [http-8888-Processor24] EhCache.get - Element for sql: select ... where this_.ID = ?; parameters: 1, ; transformer: org.hibernate.transform.DistinctRootEntityResultTransformer@d2fb67 is null
2009-02-09 12:11:04,718 DEBUG [http-8888-Processor24] StandardQueryCache.get - query results were not found in cache
2009-02-09 12:11:04,718 DEBUG [http-8888-Processor24] SQL.log - select ... where this_.ID = ?
2009-02-09 12:11:04,733 DEBUG [http-8888-Processor24] NonstrictReadWriteCache.put - Caching: de...model.impl.MeisterschaftImpl#32768
2009-02-09 12:11:04,733 DEBUG [http-8888-Processor24] NonstrictReadWriteCache.put - Caching: de...model.impl.PlanImpl#1
2009-02-09 12:11:04,733 DEBUG [http-8888-Processor24] NonstrictReadWriteCache.put - Caching: de...model.impl.PlanImpl.meisterschaften#1
2009-02-09 12:11:04,733 DEBUG [http-8888-Processor24] StandardQueryCache.put - caching query results in region: org.hibernate.cache.StandardQueryCache; timestamp=5055192533692416
When I execute the query for the second time, the entity is retrieved from cache instead of executing the sql on database.
The problem is that the collection (meisterschaften) is not properly set, its uninitialized, throwing a LazyInitializationExecption on iteration outside of the session scope.
Here is the log from second query execution:
Code:
2009-02-09 12:03:35,623 DEBUG [http-8888-Processor24] StandardQueryCache.get - checking cached query results in region: org.hibernate.cache.StandardQueryCache
2009-02-09 12:03:35,623 DEBUG [http-8888-Processor24] EhCache.get - key: sql: select where this_.ID = ?; parameters: 1, ; transformer: org.hibernate.transform.DistinctRootEntityResultTransformer@133926
2009-02-09 12:03:35,638 DEBUG [http-8888-Processor24] StandardQueryCache.isUpToDate - Checking query spaces for up-to-dateness: [APMS.PLAN, APMS.MEISTERSCHAFT]
2009-02-09 12:03:35,654 DEBUG [http-8888-Processor24] EhCache.get - key: APMS.PLAN
2009-02-09 12:03:35,654 DEBUG [http-8888-Processor24] EhCache.get - Element for APMS.PLAN is null
2009-02-09 12:03:35,654 DEBUG [http-8888-Processor24] EhCache.get - key: APMS.MEISTERSCHAFT
2009-02-09 12:03:35,654 DEBUG [http-8888-Processor24] EhCache.get - Element for APMS.MEISTERSCHAFT is null
2009-02-09 12:03:35,654 DEBUG [http-8888-Processor24] StandardQueryCache.get - returning cached query results
2009-02-09 12:03:35,654 DEBUG [http-8888-Processor24] NonstrictReadWriteCache.get - Cache lookup: de...model.impl.PlanImpl#1
2009-02-09 12:03:35,654 DEBUG [http-8888-Processor24] EhCache.get - key: de...model.impl.PlanImpl#1
2009-02-09 12:03:35,654 DEBUG [http-8888-Processor24] NonstrictReadWriteCache.get - Cache hit
Anybody got a clue whats going wrong here? First log shows that the collection (PlanImpl.meisterschaften) is correctly cached. But as you can see in second log the collection is not retrieved out of the cache during cache-hits.
Does anybody know how I can fix the problem?
Thanks in advance.
Ive got some warnings on startup, but I dont think they have something todo with the described problem. Log on startup contains entries like this for each cacheable Entity / Collection:
Code:
2009-02-09 12:18:43,227 DEBUG [main] CacheFactory.createCache - instantiating cache region: de...model.impl.PlanImpl usage strategy: nonstrict-read-write
> 2009-02-09 12:18:43,227 WARN [main] EhCacheProvider M[buildCache] - Could not find configuration [de...model.impl.PlanImpl]; using defaults.
2009-02-09 12:18:43,227 DEBUG [main] EhCacheProvider.buildCache - started EHCache region: de...model.impl.PlanImpl