-->
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.  [ 2 posts ] 
Author Message
 Post subject: Slow performances in a loop
PostPosted: Wed Jul 25, 2007 5:00 am 
Newbie

Joined: Wed May 23, 2007 4:09 am
Posts: 16
nHibernate 1.2, C# 2 and PostGres

I get good performances with this code:
for (int i = 0; i < 200; i++)
{
object myObj2 = session.Get(typeof(MyObject2), myObj2Id);
MyObject1 myObj1 = new MyObj1();
session.Save(myObj1);
}

but this code is very very slow:
for (int i = 0; i < 200; i++)
{
object myObj2 = session
.CreateQuery("from MyObject2 where id=?")
.SetInt32(0, myObj2Id)
.UniqueResult();
MyObject1 myObj1 = new MyObj1();
session.Save(myObj1);
}

Can someone explain me what is going on?

Please note that the standalone query or the standalone save are very fast. It is only when using both in the loop that it becomes very slow.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 10:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
In the first case myObj2 is cached in the session, therefore, you are only hitting database once to retrieve the object with myObj2Id.

In the second case, you are hitting the database 200 times to retrieve myObj2. It is because you are issuing a query (using the criteria interface) and queries are not cached in the session, and they are only cache in the second-level cache if explicitly instructed to do so.

I explained the second-level cache mechanics here:
http://forum.hibernate.org/viewtopic.ph ... 65#2357765

_________________
Karl Chu


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