aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate second level caching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate second level caching" Watch "Hibernate second level caching" New topic
Author

Hibernate second level caching

m mats
Greenhorn

Joined: May 12, 2011
Posts: 3
Hi,

I am trying to use second level caching. EHCache

I have added the following in the config file

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider</property>
...
<class-cache class="com.hib.ABC" usage="read-only" />
...


To the hbm file of table ABC:

<class name="ABC">
<cache usage="read-only"/>
....

I have also created an ehcache.xml file.


Now I am trying to check if caching is working

For that I am accessing the table in a loop

...
setupHibernateConfig();
for(int i = 1; i <= 5; i++) {
ABC abc= new ABC ();
Session s = HibernateUtil.getSessionFactory().openSession();
Criteria crit = s.createCriteria(ABC.class)
List<ABC> temp = new ArrayList<ABC>();
temp = crit.list();
System.out.println(temp.size()); // no of rows in table
s.close();
}
...

But I am not sure if the caching is working.
I am printing the number of rows in the cached table. To check if the hit is on the cache and not database, I tried to halt the loop for some time. Meanwhile I inserted another row into database. If caching was working fine then this the row printed should be same and should not reflect the added row. But the count is increasing. What am I doing wrong?

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You are aware that the second level cache is not a query cache? Your test will not hit the cache.

The best way to test is to load an object into the cache using get or load, update the database table directly and get the object again.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
m mats
Greenhorn

Joined: May 12, 2011
Posts: 3
My requirement is to cache the table data. Basically these are static data. Any query on that should hit the cache and not the database. How can I go about implementing this.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Any query on that should hit the cache and not the database

This is an unrealistic aim, unless you know all the queries that your application will issue. Queries are not really good candidates for caching because they are subject to a lot of different variables (i.e. are very changeable).

Hibernate has a query cache (check the docs) that you could enable. This will cache the identifiers of results from queries defined as cacheable along with the queries themselves, so used in conjunction with the second level cache this should offer something approaching what you want.

How much read only data do you have? If its a small amount I would just cache it in memory manually and control access to it through your DAO layer.
m mats
Greenhorn

Joined: May 12, 2011
Posts: 3
Thanks..

but one ques... wat exactly is the purpose of 2nd level cache then.. how does it help..


Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

It helps by caching data so reducing database round trips. If you know the queries that you want to be cached up front you can cache these and get the data form the second level cache. If you make the cache a read only cache and (if the cache implementation supports it) make it perpetual you should not hit the database. There will be edge cases where the cache is missed; unless you manually cache the data these are probably unavoidable but should be low. You can add start up code to your application that uses a CacheMode of REFRESH to populate the cache.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

It helps when you are loading objects by ID from the database, if the object is one that has been set to be cached then it will check the cache before trying to load the object from the database. If it has to load it then it will add it to the cache. You can also cache collections.

Sean


I love this place!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate second level caching