-->
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: How to handle hibernate exceptions
PostPosted: Fri Apr 21, 2006 10:56 am 
Newbie

Joined: Fri Apr 21, 2006 10:49 am
Posts: 18
I have been looking at Hibernate and I am sold on it. Now the main design problem
that I face is how to handle hibernate exceptions.

The pattern that I have been looking at is session-per-request with the use of
HibernateJTAFilter. It is nice where the DAO's are very clean and the Hibernate
unchecked exceptions are handle in a common place. It also takes care of the problem
of lazy loading and not having objects available is we were to close the session before the JSP is processed.
But what I am trouble by are the exceptions, for example if an action tries to delete a row which does not exist then again an unchecked hibernate exception is thrown which bubbles all the way up to the JTAFilter, and then what, the best I could do is forward the requestdispatcher to some error page. This would be very generic for a site that may have 200 or 300 different actions and I would like to give users specifics.
Also how would I manage two transactions in sequence in the same request. The fact that the second fail should not roll back the first or if the first on fail I still would like the second transaction to be executed.

Any comments or advise would be of great help.

Thanks

Jorge Luis


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 23, 2006 8:13 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You have to do your own exception handling. If you're happy with unchecked exceptions reaching the top level of your app, then just catch them there. There are plenty of subclasses of HibernateException to help you figure out what has gone wrong so that you can report interesting details to the user. And of course, you can catch those exceptions lower down in your data access layer and wrap them with application-specific exceptions. This would be my recommendation, as it has the added bonus of hiding hibernate-specific classes from your app.

For the two transactions in a request case: there's no problem if the second transaction aborts, as you've already committed the first transaciton and its changes are already in the database. If the first transaction aborts, you should catch the exception, log it, clear/close your session, and start a new session. Then continue on to your second transaction as normal.


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.