Hibernate 2nd Level caching doesnt seem to be working

John picture John · Jun 29, 2010 · Viewed 8.2k times · Source

Im currently trying to get hibernate working using the caching provider that comes with hibernate.

net.sf.ehcache.hibernate.SingletonEhCacheProvider

I have a default cache and a class specific cache enabled in the ecache.xml which is referenced in my hibernate.cfg.xml file. The class/mapping file specific cache is defined to handle upto 20000 objects.

However, I'm seeing no perfrormance gains since I turned on the cache mapping on one of the mapping files Im testing this with.

My test is as follows.

Load 10000 objects of the particular mapping file im testing (this should hit the DB and be a bottle neck). Next I go to load the same 10000 objects, as this point I would expect the cache to be hit and to see significant performance gains. Have tried using both "read-only" and "read-write" cache mapping on the hibernate mapping xml file Im testing with.

I'm wondering is their anything I need to be doing to ensure the cache is being hit before the DB when loading objects?

Note as part of the test im pagin through these 10000 records using something similar to below ( paging a 1000 records at time).

Criteria crit = HibernateUtil.getSession() .createCriteria( persistentClass );
       crit.setFirstResult(startIndex);
       crit.setFetchSize(fetchSize);
       return crit.list();

Have seen that criteria has a caching mode setter ( setCacheMode() ) so is there something I should be doing with that??

I notice using the below stats code that theres 10000 objects (well hiberante dehydrated onjects i imagine??) in memory but for some reason I'm getting 0 hits and more worryingly 0 misses so it looks like its not going to the cache at all when its doing a look up even though the stats code seems to be telling me that theres 10000 objects in memory.

Any ideas on what im doing worng? I take it the fact im getting misses is good as it means the cache is being used, but i cant figure out why im not getting any cache hits. Is it down to the fact im using setFirstResult() and setFetchSize() with criteria.

System.out.println("Cache Misses = " + stats.getSecondLevelCacheMissCount());
System.out.println("Cache Hits Count = " + stats.getSecondLevelCacheHitCount());

System.out.println("2nd level elements in mem "+ stats.getSecondLevelCacheStatistics("com.SomeTestEntity").getElementCountInMemory());

Answer

Pascal Thivent picture Pascal Thivent · Jun 30, 2010

The second level cache works for "find by primary key". For other queries, you need to cache the query (provided the query cache is enabled), in your case using Criteria#setCacheable(boolean):

Criteria crit = HibernateUtil.getSession().createCriteria( persistentClass );
crit.setFirstResult(startIndex);
crit.setFetchSize(fetchSize);
crit.setCachable(true); // Enable caching of this query result
return crit.list();

I suggest to read:


If I cache the query, are all them hibernate entities from the query then available in the second level cache?

Yes they will. This is explained black on white in the link I mentioned: "Note that the query cache does not cache the state of the actual entities in the result set; it caches only identifier values and results of value type. So the query cache should always be used in conjunction with the second-level cache". Did you read it?

As i was under the impression that using the query cache was entirely different than using the hibernate 2nd level cache.

It is different (the "key" used for the cache entrie(s) is different). But the query caches relies on the L2 cache.

From your answer you seem to be suggesting that the query cache and second level cache are both the same, and to generate cache hits I need to be using the "find by primary key".

I'm just saying you need to cache the query since you're not "finding by primary key". I don't get what is not clear. Did you try to call setCacheable(true) on your query or criteria object? Sorry for insisting but, did you read the link I posted?