-->
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.  [ 3 posts ] 
Author Message
 Post subject: repeated select
PostPosted: Tue Oct 04, 2011 6:21 am 
Newbie

Joined: Tue Oct 04, 2011 4:40 am
Posts: 2
Hi all,

I recently join a new application team developpers and noticed some strange behaviors about the persistence layer of their application.
Is there any reason or any particular hibernate configuration that explains the more a simple (w\o any eargly fetched data) select query is repeatedly executed the more time is execution takes.
Here is a example of HQL query :

Code:
final StringBuilder sb = new StringBuilder();
sb.append("From ");
sb.append(classe.getSimpleName());
sb.append(" as o where o.idNode = :idNode and idParam = :idParam");
final Query qry = getHibernateUtil().getHibernateSession().createQuery(sb.toString());
qry.setParameter("idNode", idNode);
qry.setParameter("idParam", idParam);

return (AnObject) qry.uniqueResult();

and the issuing SQL query :

Code:
select
   this_.id as id69_0_,
   this_.node_id as noeuds2_69_0_,
   this_.droits_id as droits3_69_0_,
   this_.fonctions_id as fonctions4_69_0_,
   this_.param_id as id5_69_0_,
   this_.niveau as niveau69_0_
from
   fonction_droits this_
where
   this_.node_id=?
and
   this_.param_id=?


In a test when I place this query into a loop from 1 to 6500 iterations and add timers its execution time is multiplied by 4 between the 1000 first iterations and the 1000 last ones (the average execution time increase from 7ms to 28ms).
The test is executed with a single session and a single transaction.
The query parameters are chosen to return any object so it should not be a matter of objects instanciation.
I already tried the same test with a direct JDBC connection resulting with a negligible execution time so this is not a database (Oracle) issue.
We also use a C3P0 connections pool that I tried to bypass but the result is the same.

This is probably due to a bad configuration but I can't figure it out.

Many thanks in advance for your highlights.


Top
 Profile  
 
 Post subject: Re: repeated select
PostPosted: Tue Oct 04, 2011 6:40 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
If your query is selecting a different object for each iteration the problem is probably that you are filling up the first-level cache in Hibernate. While this is not a problem by itself, it may result in a performance penalty due to the automatic dirty-checking of entities. The default setting is that Hibernate do this before executing a query (FlushMode.AUTO), so for each iteration more and more entities are dirty-checked. There are basically two ways to improve this.

* Use a different flush mode, for example, FlushMode.COMMIT. The flush mode is set on the session.
* Evict entities from the session at regular intervals.


Top
 Profile  
 
 Post subject: Re: repeated select
PostPosted: Tue Oct 04, 2011 8:57 am 
Newbie

Joined: Tue Oct 04, 2011 4:40 am
Posts: 2
Hi Nordborg,

This is the case, each query select a different object and the 1st level cache is growing up quickly.
As you mentioned, after switching the AUTO default session's flush mode to COMMIT this overhead vanished.

I've been working with Hibernate for some time now but I've never been told about this problem yet.
Many thanks to figure it out.

No I've to check that within the same transaction there is nowhere some code that expect the changes performed to managed objects being automatically synchronized with the database. In such a case I think I will have to flush them manually to the database.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.