• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate second level caching

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
m mats
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks..

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


 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic