views:

112

answers:

3

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());
+2  A: 

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?

Pascal Thivent
So just to clear the 2nd level cache can never be used unless its used with "find by primary key"? Does the crit.list(); (if cache is enabled for the is entity) populate the 2nd level cache in the example code? if it does then i can only access the cache when i use the hiberanet equivalent of "find by primary key"??
John
@John As I wrote in my answer, you need to cache the query. What is not clear with that?
Pascal Thivent
@Pascal Im still slightly confused. If I cache the query, are all them hiberante entities from the query then available in the second level cache? As i was under the impression that using the query cache was entirely different than using the hibernate 2nd level 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".
John
@John: I've updated my answer to clarify those questions. Hope it's more clear.
Pascal Thivent
A: 

@Pascal: Yes I read the article,I really appreciate your help but i'm still slightly confused. I understand that the cache does not store actaul objects, its merely just IDs (primary keys i imagine) and data. Now where im getting confused still, is (1) Can I only get a cache hit if I query by primary id?

(2) The major part where im confused is where and how the query cache vs. the 2nd level cache is used.

Can you explain what you mean when you say "(the "key" used for the cache entrie(s) is different", what key is used for the query cache, and what key is used for the 2nd level cache? From the earlier posts the primary key (id) is what should be used to query the second level cache, but what should be use to query the query cache as you've mentioned that the keys are different

John
(1) No, **but you need to cache the query** (2) The query cache works together with the L2 cache. Regarding the question *what key is used for the query cache*, the answer is given in the article: *the combination of the query and the values provided as parameters to that query*. But all this is transparent for you (except to understand how things work), Hibernate queries the cache, not you.
Pascal Thivent
But please, edit and update your question instead of posting answers that aren't answers (SO is not a forum, answers should be answers only).
Pascal Thivent
@Pascal: Sorry Im new to SO ill update the question shortly. As for(1) so to get a cache hit in the second level cache it is impossible to get a cache hit unless (a) Im using findByPrimaryID with no query level cache or (B) im using the second level cache combined with the query level cache. Apologies but these are very basic questions but ive read the article 10 times but i find it a bit confusing
John
No problem (and welcome to SO). About (1), yes, that's it. Just try it :)
Pascal Thivent
@Pascal: Cheers that makes sense. The golden rule is that the cache will never be hit unless I use findByPrimaryID exclusively (and thats if the id is in the cache already) or im using the second level cache combined with the query level cache. Has taken me the best part of a week to understand that!!! Thanks for help. How do i delete or move answers ( as i want to delete the answer i posted below as its not really needed) Thanks
John
You're welcome. There should be *edit* and *delete* links at the bottom of the answer (and question). At least, I have them on my answers.
Pascal Thivent
A: 

Im using the below to gather stats, and I have the second level cache enabled. I can see that the second level cache, for "2nd level elements in mem" I'm seeing 1000s of objects in memory. But Im seeing 0 for cache hits and 0 for cache misses for some reason. Note i have caching enabled on the hibernate mapping file using ehcache.

Statistics stats = HibernateUtil.getSession().getSessionFactory().getStatistics();

for( String s : stats.getSecondLevelCacheRegionNames() ) { System.out.println("Cache Name = " + s); }

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.someEntity").getElementCountInMemory());

System.out.println("2nd level category name: "+ stats.getSecondLevelCacheStatistics("com.someEntity").getCategoryName());

System.out.println("2nd level elements on disk: "+ stats.getSecondLevelCacheStatistics("com.someEntity").getElementCountOnDisk());

System.out.println("2nd level hit count: "+ stats.getSecondLevelCacheStatistics("com.someEntity").getHitCount());

System.out.println("2nd level miss count: "+ stats.getSecondLevelCacheStatistics("com.someEntity").getMissCount());

System.out.println("2nd level size in memory: "+ stats.getSecondLevelCacheStatistics("com.someEntity").getSizeInMemory());

System.out.println("2nd level put = " + stats.getSecondLevelCacheMissCount());

John
Please move this to the question (but I don't think it's relevant, I don't get your point here).
Pascal Thivent