-->
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: When to close the session
PostPosted: Thu Jun 02, 2005 10:36 am 
Newbie

Joined: Thu Jun 02, 2005 10:02 am
Posts: 1
Hi,

A session closing problem. I'm using Hibernate 2.1.8.Here is the scenario.

class ABC{

public void saveOrder(Order o){
try {
Session hsession = HibernateUtil.currentSession();
Transaction tx = hsession.beginTransaction();
try {
hsession.saveOrUpdate(order);
//create default workcard.
ClassB b = new ClassB();
WorkCard wc = b.createWorkCard(order);
tx.commit();
hsession.flush();
hsession.close();
} catch (Exception e) {
tx.rollback();
hsession.close();
throw new ServiceTierException("error : "+e.getMessage(), e);
}
} catch (HibernateException e) {
try {
HibernateUtil.closeSession();
} catch (HibernateException he) {
throw new ServiceTierException("saveOrder(): closeSession() call failed", he);

}
throw new ServiceTierException("saveOrder()", e);
}
}
}


In the above code, ClassB also calls for HibernateUtil.getCurrentSession() and do a txn(saving WorkCard) with database and commit and close the session. When ClassB does that and return, Class ABC cannot commit the txn(marked red) but throws an exception saying "SessionClosed".

So, how to handle when there are txns across different classes and when we don't know when to close the session ?

Any help would be highly appreciated.

Thanks
Viji


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 12:37 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi

class ABC extends HibernateUtil{

public void saveOrder(Order o){
Session hsession=null;
try {
hsession = super.currentSession();
Transaction tx = hsession.beginTransaction();
// your code here


finally{
HibernateUtil.closeSession();
}

Try it out

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 5:23 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
caller must start and end transaction, you do not need to repeat it in helper methods and helpers doe's not know about use case demarcation (it defines transaction). Common practice is to demarcate transactions using Servlet Filter in web applications (transaction/session per http request)


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.