-->
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: degrading performance with multiple transactions
PostPosted: Sun Mar 26, 2006 7:08 am 
Newbie

Joined: Sun Mar 26, 2006 6:31 am
Posts: 3
I experience degrading performance when executing
many small database transactions
such as in the following:

Code:
for (int i=0;i<5000;i++) {
   Transaction tx = null;
   Person p = new Person("Person" + i);                
   try {
   tx = hibernateSession.beginTransaction();                    
        hibernateSession.save(p);
        tx.commit();
    } catch (Exception e) {
   if (tx != null)
       tx.rollback();
       System.err.println("Insert failed. " + e.toString());
    }
    if ((i%100)==0)  ... // Performance measuring code
}      


The first 100 inserts take approx. 50ms, then the time
increases, up to approx. 1300ms for the last 100 inserts.
The bulk of the time is spent in the line "tx.commit()".
>95% of the CPU time is spent by the Java
interpreter (i.e. not by mysqld).

What could be wrong with the above code / with my
configuration? I'd expect that all inserts should take
approximately the same time.

I use Hibernate 3.1.3, MySQL 5.0.18 (same problem
with HSQL). Person is a simple table with only id and
name, there are no other tables. The above example
is an extract of a much larger application - I'm aware
that in the above example a single large transaction
would suffice.

The debug log mentions a number of other Person entities
during each commit (in addition the single one that
actually should be inserted), but I do not know if this might
be related to the problem:

6 Mrz 2006 13:01:29 DEBUG: listing entities:
26 Mrz 2006 13:01:29 DEBUG: Person{name=Person136, id=41640}
26 Mrz 2006 13:03:54 DEBUG: Person{name=Person824, id=42666}
...
26 Mrz 2006 13:01:29 DEBUG: more......

Thanks in advance and best regards,
Guido


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 26, 2006 7:43 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
You're not clearing the new Person objects out of the session, so with each commit I think that Hibernate is checking all of the previous Person objects to see if they have any changes that need to be flushed down to the database. Try adding a call to hibernateSession.clear() after the commit.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 26, 2006 8:18 am 
Newbie

Joined: Sun Mar 26, 2006 6:31 am
Posts: 3
Thanks a lot - if I add Session.clear() the inserts indeed
all take the same time.

So I only have to see where are the
best points in the code to clear the session
(I have lots of methods interacting with Hibernate
and using objects handled by Hibernate
but want to hide Hibernate itself from the users of the
methods).


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 26, 2006 9:08 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
Yep - you could also think about controlling your session 'scope' instead of clearing it or just evicting specific objects from the session when you are done with them. Take a look here: http://www.hibernate.org/42.html for a good discussion of a session as a 'unit of work'.

Cheers,
Rich.


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.