Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 2nd Level caching doesnt seem to be working

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());
like image 554
John Avatar asked Jun 29 '10 16:06

John


People also ask

How Hibernate 2nd Level cache works?

This cache only works at a session level, meaning each session object caches data independently, so there is no sharing of cached data across sessions, and the cached data is deleted when the session closes. This makes the cache only useful for repeated queries in the same session.

Which 2nd level cache is better in Hibernate?

Hibernate second level cache uses a common cache for all the session object of a session factory. It is useful if you have multiple session objects from a session factory. SessionFactory holds the second level cache data. It is global for all the session objects and not enabled by default.

Is second level caching mandatory in Hibernate?

You can extend this caching with an optional second-level cache. The first-level keeps being mandatory and is consulted first always. The second-level cache is used to cache object across sessions. For second-level caching, there are some third-party solutions which can be used with Hibernate.

What is the difference between Level 1 cache and Level 2 cache in Hibernate?

The primary difference is that the first level cache is maintained at the Session level while the second level cache is maintained at the SessionFactory level. The data stored in the first level cache is accessible to the only Session that maintains it, while the second level cache is accessible to all.


1 Answers

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:

  • Hibernate: Truly Understanding the Second-Level and Query Caches

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?

like image 197
Pascal Thivent Avatar answered Sep 28 '22 06:09

Pascal Thivent