-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Are my objcts being Cached?
PostPosted: Fri Sep 01, 2006 5:49 am 
Newbie

Joined: Fri Aug 25, 2006 6:53 am
Posts: 13
Hi,

I am trying to implement caching in Hibernate. I want to use EHCache (which comes as default with Hibernate). Since it comes as default with Hibernate, in the hibernate.cfg.xml file I am only setting the following tag to enable second level caching.

Code:
<property name="hibernate.cache.use_second_level_cache">true</property>

(As you can see, I am not defining net.sf.hibernate.cache.EhCacheProvider since I am assuming that it is already set as default ... am I wrong to assume this?.

I then created the ehcache.xml file in my root. Here I defined the attributes as follows:

Code:
<ehcache>   
   <defaultCache
        maxElementsInMemory="10000"
        eternal="true"
        overflowToDisk="true"
        />

    <cache name="com.myapp.common.impl.BaseCountry"
        maxElementsInMemory="10000"
        />
</ehcache>

Finally I set the the cache option in the Country.hbm.xml file as follows:

Code:
<cache usage="read-write" />

Now when how do I know if caching is working or not. The time taken seem to be the same. Also I can see the Sql be generated on the screen. Does this mean that caching is not woring for me (I did something wrong int he configuration). Also to retrieve the objects I am using the Hibernate Criteria object. Does this need a different Caching options?

thanks in advance for any comments.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 8:59 am 
Beginner
Beginner

Joined: Wed Jun 21, 2006 10:08 am
Posts: 26
Turn SQL on and change your Log4j settings to debug. If objects are being cached in the 2nd level cache it will tell you. It also says if it found a 2nd level cache hit.

But a good rule of thumb is that on the first get/load of an item it will hit the DB (see a Hibernate SQL statement). The next time you do a get/find you should see no SQL. If you do it's not being cached.

You can also check out chapter 19 of the docs. It tells you how to use the statistics to see what's in your cache region:

Code:
org.hibernate.stat.Statistics stats = sf.getStatistics();
String[] regionNames = stats.getSecondLevelCacheRegionNames();

for(int i = 0; i < regionNames.length; i++) {
   
   Map cacheEntries = stats.getSecondLevelCacheStatistics(regionNames[i]).getEntries();
   out.println("<b>Cache Region:</b> " + regionNames[i] + "<BR/>");
   out.println("<b>Cache Stats: </b>" + stats.getSecondLevelCacheStatistics(regionNames[i]).toString() + "<BR>");
   Object[] keys = cacheEntries.keySet().toArray();
   
   for(int j = 0; j < cacheEntries.size(); j++) {

      out.println("<b>Cached Item: </b>" + cacheEntries.get(keys[j]) + "<BR/>");
   }
   out.println("<hr>");
}

_________________
- Jonathan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 9:58 am 
Newbie

Joined: Fri Aug 25, 2006 6:53 am
Posts: 13
Thanks ...

no it seems that nothing is being cached .. can not understand why however! :(

b.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 13, 2006 11:21 am 
Newbie

Joined: Wed Sep 13, 2006 11:13 am
Posts: 1
Hope this isn't too late, and I'm not sure if this will be helpful...

I notice that you set up a cache at the BaseCountry level, but you put the cache directive in an hbm file for Country. I am still using hibernate 2.1.8, but my experience is that you need to add a cache region for the specific concrete class that you want to cache, therefore:

<ehcache>
<defaultCache
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"
/>

<cache name="com.myapp.common.impl.Country"
maxElementsInMemory="10000"
/>
</ehcache>

...or wherever the actual Country class is. However, I would have expected the Country object end up in the defaultCache given your setup...you should see something to this effect in your logging from hibernate when you are setting up the SessionFactory!

Actually, debugging hibernate is fairly easy, and if you debug into the session.retrieve(...) method, you will fairly quicly come to the point where the cache is called. The persister associated with the Class object for Country will either be null or a HashMap, which indicates whether a cache has successfully been set up for that Class.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.