-->
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: Hibernate is slow without calling Evict()
PostPosted: Wed Jul 25, 2007 12:56 pm 
Newbie

Joined: Wed May 23, 2007 4:09 am
Posts: 16
In a loop (let say 1000 iterations)
1) if I make just one query (session.CreateQuery()), it's fast
2) if I make just an insert (session.Save()), it's fast

but if I do both in my loop, hibernate is very very slow.

This is true with any database but it doesn't occur with other ORM or with a native ADO.NET code.

It seems the method session.Evict() fix the issue.

Dos anyone have an explanation or advices?
Thanks in advance.


Example:
Code:
for(int i=0; i<1000; i++)
{
    session.CreateQuery("from Test where name=?")
        .SetString(0, "hello")
        .UniqueResult();

    MyObj obj = new MyObj();
    session.SaveOrUpdate(obj);

    // This makes execution fast but why?
    // How can I avoid making this call?
    session.Evict(obj);
}


Please notice this occurs event with 10 iterations, and this does not occur if I do 2 loops.

Exemple of fast code running without problem:
Code:
for(int i=0; i<1000; i++)
{
    session.CreateQuery("from Test where name=?")
        .SetString(0, "hello")
        .UniqueResult();
}
for(int i=0; i<1000; i++)
{
    MyObj obj = new MyObj();
    session.SaveOrUpdate(obj);
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 11:37 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
When you save an object you are locking it into the session. When you execute a query Hibernate by default will check your objects to see if you have made any changes to them (AutoFlush behaviour).

Evicting the object removes it from the session (1st level cache) so Hibernate no longer needs to keep track of it.

If you know for a fact that you're not updating the same tables you are selecting from you should be able to get the same order of performance by setting session.setFlushMode(FlushMode.MANUAL)

Bear in mind that when you do this you will need to manually flush your session so that your saved objects are sent to the DB.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


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.