-->
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.  [ 9 posts ] 
Author Message
 Post subject: Caching with Hibernate and JPA Discussion
PostPosted: Wed May 19, 2010 7:55 pm 
Newbie

Joined: Mon Mar 22, 2010 5:33 pm
Posts: 5
Hi All,

I am using hibernate+jpa for the database layer in my project along with Struts and Spring. The application server is websphere application server 6.1 and database being used is DB2.

The project was in good condition until we realized that the performance of the database is not really good. Later on I found out that we are using some lookup tables and some 5-6 result sets(these result sets are actually drop down in the application) which needs to be invoked in almost every request. And these values are being fetched from the database per request which is reducing the overall performance.

I want to add Catching in the application and I am kind of Naive about how to proceed and what kind of caching must be used(level1 or level2) or in my case I should use another Caching technique.

If you have some suggestions for me please do let me know.

Thanks and Regards,
Sameer


Top
 Profile  
 
 Post subject: Re: Caching with Hibernate and JPA Discussion
PostPosted: Thu May 20, 2010 5:49 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
level1: The first level's cache scope is the persistent context (= the current session (unless you call clear)).
Within this context hibernate does automatically cache the result of relation navigations.
If you could map the drop down content as OneToMany relation between 2 entity classes,
then you can benefit of this behavior be doing navigations instead of calling user-queries.

if indeed each of your request starts a new persistent context (session),
then You cannot benefit of this behavior. In this case you have to use the second level cache.

level2: The second level's cache scope is greater and includes all sessions.
If you use navigation for filling the drop down content,
then you have to configure the regarding Collection for being cached.
If you use user-query for filling the drop down content,
then you have to enable the second level query cache and to use the setCacheable(true) on regarding queries.
In each case the regarding entity class have to be configured for the entity second level cache.


Top
 Profile  
 
 Post subject: Re: Caching with Hibernate and JPA Discussion
PostPosted: Thu May 20, 2010 12:20 pm 
Newbie

Joined: Mon Mar 22, 2010 5:33 pm
Posts: 5
Thanks for the information, It was really helpful. I guess my problem still remains the same.

Here is the main problem:

I have two drop downs which accesses the same service class:

service.findByCodetype("SYSTEMACCESS");
service.findByCodetype("ADMINACCESS");


These two queries are accessing the same table (Master_REK_TBL) using the same method only the difference is the where clause but I want both of the results to be in the Cache. Like this there are 10drop downs from the same table. If I can fix this issue I could increase the performance of my application atleast 3 times.

Please advise what should I do in this scenario.

-Sameer


Top
 Profile  
 
 Post subject: Re: Caching with Hibernate and JPA Discussion
PostPosted: Fri May 21, 2010 3:15 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
service.findByCodetype("SYSTEMACCESS");


How does this service class perform the queries?
Have you the code (and can you also change the code) of this service class?


Top
 Profile  
 
 Post subject: Re: Caching with Hibernate and JPA Discussion
PostPosted: Fri May 21, 2010 10:54 am 
Newbie

Joined: Mon Mar 22, 2010 5:33 pm
Posts: 5
This is the code and yes I can change the code if needed:

Code:
public List<CodeValue> findByCodetype(String propertyName) {

      try {
         System.out.println("Calling the findByCodetype() method");
         final String queryString = "select model from CodeValue model where model.codetype"
               + "= :propertyValue" + " order by model.code";

         Query query = em.createQuery(queryString);
         if (query instanceof org.hibernate.ejb.QueryImpl) {
            ((org.hibernate.ejb.QueryImpl) query).getHibernateQuery()
                  .setCacheable(true);
         }
         query.setParameter("propertyValue", propertyName);
         return query.getResultList();

      } catch (RuntimeException re) {
         logger.error("findByCodetype failed ", re);
         throw re;
      }


Top
 Profile  
 
 Post subject: Re: Caching with Hibernate and JPA Discussion
PostPosted: Wed May 26, 2010 5:39 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi javameanslife17,

Quote:
Thanks for the information, It was really helpful. I guess my problem still remains the same.



From your code I see that you use user-query for finding and filling the drop down content,
so as already written:
you have to enable the second level cache and the second level query cache
and assert that setCacheable(true) is called on regarding queries.
The regarding entity class CodeValue has also to be configured for the entity second level cache.
Then both results service.findByCodetype("SYSTEMACCESS") and service.findByCodetype("ADMINACCESS")
should be cached in second level query cache.

Why do you guess that the problem will still remain the same ?!


Top
 Profile  
 
 Post subject: Re: Caching with Hibernate and JPA Discussion
PostPosted: Wed May 26, 2010 3:17 pm 
Newbie

Joined: Mon Mar 22, 2010 5:33 pm
Posts: 5
Thanks pb00067 for the information. I was in the impression that even if enabling the second level cache and the query level cache,
if I query service.findByCodetype("SYSTEMACCESS") and then service.findByCodetype("ADMINACCESS") hibernate will keep only the most recent info in the cache so only ADMINACCESS drop down will be there second time and SYSTEMACCESS drop down will be gone.
Having said that now I went through the documentation and Now its pretty clear that second level cache and query level cache is the solution for my problem as both of the drop downs will be there in the cache.

Really Appreciate your help.

I have implemented everything i.e. second level cache and also query level cache and seems to be working but in order to prove its working I need to see what is there in the cache. Do you know how can I access the cache just to see what objects are being cached.


Top
 Profile  
 
 Post subject: Re: Caching with Hibernate and JPA Discussion
PostPosted: Thu May 27, 2010 5:56 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Do you know how can I access the cache just to see what objects are being cached.


It depends on which implementation you choose for the second Level cache.
Several caches allow to access statistics via JMX,
so you can for example see regarding informations via jconsole on the MBeans tab folder.
JBosscache for example provides very good insight about cache data,
other implementations may rather provide only statistical data about cache usage.

Also the statistics of Hibernate are providing some informations about the second-level cache.
Also the statistics of Hibernate can be bound to JMX:

Code:
StatisticsService statsMBean = new StatisticsService();
            statsMBean.setSessionFactory(mysessionfactory);
            statsMBean.setStatisticsEnabled(true);


Top
 Profile  
 
 Post subject: Re: Caching with Hibernate and JPA Discussion
PostPosted: Thu May 27, 2010 6:13 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The simplest way to see if the cache is used or not in a given situation is to enable logging in Hibernate. Personally I usually turn on SQL logging and if nothing appears I assume that the caching worked. It is also possible to log the cache-activity itself. See http://docs.jboss.org/hibernate/stable/ ... on-logging for more information about logging.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.