-->
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: Enabling second level cache for find all queries
PostPosted: Thu Aug 07, 2008 4:47 am 
Newbie

Joined: Tue Dec 07, 2004 8:15 pm
Posts: 10
Location: Adelaide Australia
We have a large number of reference data tables that are used to populate drop down lists on our UI. At the moment Hibernate is executing a query against the database for every display request for every reference table. I would like to enable second level caching for these tables to improve performance.

I have enabled second level caching using EHCache as shown below but the find all queries are never hitting the second level cache. They do seem to be populating the cache but never using it. Can anyone suggest what sort of query I could use to ensure that the second level cache is checked for results?

Hibernate version:
3.2.6
Mapping documents:
Code:
      <property name="hibernate.cache.provider_class">
         net.sf.ehcache.hibernate.EhCacheProvider
      </property>

Code:
       <class-cache class="package.ClassName" usage="read-only"/>                                  

Code:
   <cache name="package.ClassName"
     maxElementsInMemory="300"
     eternal="true"
     overflowToDisk="false"
     />

Code between sessionFactory.openSession() and session.close():
Code:
    return getSession().createQuery("from " + getPersistentClass().getName()).list();

Full stack trace of any exception that occurs:
N/A
Name and version of the database you are using:
Oracle 10g
The generated SQL (show_sql=true):
N/A
Debug level Hibernate log excerpt:
N/A

_________________
Peter Kelley


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 07, 2008 5:07 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Basically there are two things you need to do:

1. Enabled the query cache by setting
Code:
hibernate.cache.use_query_cache true

in your hibernate.properties files

2. Tell Hibernate that you want to cache the result of a query by calling
Code:
Query.setCacheable(true)

Eg.
Code:
return getSession().createQuery("from " + getPersistentClass().getName()).setCacheable(true).list();


For more information read
http://www.hibernate.org/hib_docs/v3/re ... querycache


Top
 Profile  
 
 Post subject: Working
PostPosted: Thu Aug 07, 2008 6:11 pm 
Newbie

Joined: Tue Dec 07, 2004 8:15 pm
Posts: 10
Location: Adelaide Australia
That did the trick, thanks!

_________________
Peter Kelley


Top
 Profile  
 
 Post subject: Re: Working
PostPosted: Fri Oct 10, 2008 9:08 am 
Newbie

Joined: Tue Oct 02, 2007 1:47 am
Posts: 6
Hi

For me the second level cache does'nt seem to work though have enabled it.
The log for p6spy shows the queries fired everytime i need the object which ideally should have been picked up from the cache and no query should be hitting the database.

Code :
1. Query query =dbSession.createSQLQuery(queryTable).addEntity(queryTable, classObj).setCacheable(true);


2. Also hibernate.properties has been enabled to activate the second level cache

<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>

What am i missing ?

Thanks,
Pragati


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.