-->
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: Session and transaction problem!
PostPosted: Fri Jul 27, 2007 12:23 am 
Newbie

Joined: Wed Nov 08, 2006 5:35 am
Posts: 4
Hello everyone,
I found a problem when using hibernate with ZK which is a ajax framework.
First, when I update or save a POJO, it's ok, and the POJO is persistent to database,but the problem came when I reload it again. like findByID,findAll, etc, I always get the previous objects.
Second, I found if I execute session.close() before any find operation, this problem may be gone.
I am new to hibernate, and I am confused with this, should I close session after every update and save? Or what else should I do?
By the way, I have tried this without ZK and I found it works just OK, so if anyone has used ZK, or encountered this problem, your help will be appreciated.
Thanks very much!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 27, 2007 12:38 am 
Newbie

Joined: Sat Jul 14, 2007 11:50 am
Posts: 12
Location: the Burgh
Yeah you're supposed to close the session after every unit of work you do is complete... not necessarily after every save or update... but certainly every time your program runs. You should check this page out:

http://www.hibernate.org/42.html

I found it really helpful. I use the third code chunk on that page for my applications.

Code:
try {
    factory.getCurrentSession().beginTransaction();

    // Do some work
    factory.getCurrentSession().load(...);
    factory.getCurrentSession().persist(...);

    factory.getCurrentSession().getTransaction().commit();
}
catch (RuntimeException e) {
    factory.getCurrentSession().getTransaction().rollback();
    throw e; // or display error message
}


In that idiom you don't really need to open and close a session as the factory does that for you. There's code farther down on that link that shows you how to build that factory.

Hope this helps.


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.