-->
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: try catch finally...
PostPosted: Thu Dec 16, 2004 9:22 pm 
Newbie

Joined: Thu Dec 16, 2004 7:20 pm
Posts: 2
Hello All,

I've just been working with hibernate for a short while and haven't quite figured out the best way to work with try, catch, finally statements.

Here is an idea I've been kicking around for a little while now...

Code:
try
{
  Session session = SessionFactory.getSessionFactory().openSession();
  try
  {
    //do database stuff here like loads, saves, updates...
    session.load(object, new Integer(1));
  }
  finally
  {
    session.close();
  }
}
catch(Exception e)
{
  //Now handle any exceptions here...
}


Does anyone see any thing wrong with this approach? Does anyone do it differently?

Thanks for your time,

Nate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 16, 2004 9:34 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The online docs and the APIs show a transaction Idiom you should apply.
For example a typical transaction should use the following idiom:

Code:
Session sess = factory.openSession();
Transaction tx;
try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
}
catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
}
finally {
     sess.close();
}



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.