-->
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.  [ 6 posts ] 
Author Message
 Post subject: DAO Exeption Handling
PostPosted: Wed May 28, 2008 8:38 pm 
Newbie

Joined: Tue May 13, 2008 2:44 pm
Posts: 5
I have a question regarding (Generic Data Access Objects)
http://www.hibernate.org/328.html

Specifically, the line about not forgetting exception handling:
// Plain JDBC: HibernateUtil.getCurrentSession().getTransaction().commit(); // Don't forget exception handling

I've implement the examples described in the URL above (everything works perfectly), but have yet to add exception handling.

How do I add hibernate exception handling to this block of code? Is it needed?
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
MemberInviteDAO memberInviteDAO = FACTORY.getMemberInviteDAO();
memberInviteDAO.makePersistent(memberInvite);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();

Do I need to catch a runtime exception as stated in the Hibernate API or is my code fine as above?

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();
}

Depending on the answers to above, if I'm not persisting objects to the database (only using load), do I need to wrap my code in a try/catch block?

Thanks,
Michael


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 29, 2008 4:09 am 
Beginner
Beginner

Joined: Thu Nov 15, 2007 11:27 am
Posts: 34
A runtime exception is an exception, so you code above handle them.

If you do only 'load', no real need to have a transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 29, 2008 10:08 am 
Newbie

Joined: Tue May 13, 2008 2:44 pm
Posts: 5
Yoann56 wrote:
A runtime exception is an exception, so you code above handle them.

If you do only 'load', no real need to have a transaction.


You always need to begin a transaction, but the question is, do I need to catch a runtime exception and ROLLBACK the transaction when doing a load?

Here is my implementation:

Transaction tx = null;
try {
tx = HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
MemberPreferenceDAO memberPreferenceDAO = FACTORY.getMemberPreferenceDAO();
memberPreferenceDAO.makePersistent(memberPreference);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
} catch (RuntimeException e) {
if (tx != null) tx.rollback();
throw e;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 5:07 am 
Beginner
Beginner

Joined: Thu Nov 15, 2007 11:27 am
Posts: 34
When you open a transaction, it's better (cleaner) to close it (commit or rollback) so you will have clear boundaries for your transaction.

So yes rollback.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 9:45 am 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
It's fun to experiment, but for production code, you might want to seriously consider an enterprise framework like Spring or EJB or Seam to manage this for you. Saves you from having to write a lot of boiler-plate code like this.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 10:02 am 
Newbie

Joined: Tue May 13, 2008 2:44 pm
Posts: 5
rhasselbaum wrote:
It's fun to experiment, but for production code, you might want to seriously consider an enterprise framework like Spring or EJB or Seam to manage this for you. Saves you from having to write a lot of boiler-plate code like this.


This is what I have heard, but I want to work with Hibernate exclusively (I want to understand well enough) before adding another technology on top of it. This code will likely have to go into production, but I will refactor it at a later point to integrate spring.

Thanks for confirming my question regarding transactions.

Michael


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.