-->
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: HibernateException handling
PostPosted: Mon May 23, 2005 6:16 pm 
Newbie

Joined: Mon May 23, 2005 6:13 pm
Posts: 2
Hi,

I am just starting using Hibernate and got a question about the HibernateException.

Why is HibernateException an unchecked exception ?

In the Wiki is noted that exceptions should be handled in some higher-level layer.

But when using Hibernate in a Session-Facade (CMT) I think it is desirable to throw an ApplicationException instead of a SystemException to the client in case of an (low-level) error.

Some methods like "load()" throws an HibernateException when no result was found (NULL), others like "get()" do not.

Should I simply catch HibernateException and throw something like "BusinessManagerException" as ApplicationException or just rethrow the HibernateException.

Would be interesting to know how you handle occuring HibernateExceptions in a Session-Facade.

Regards
Jan Zimmek


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 23, 2005 8:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
HibernateExceptions should certainly not be application exceptions because ... well ... they have nothing to do with "application".

They should be runtime, so that they force txn rollback.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 8:21 am 
Newbie

Joined: Mon May 23, 2005 6:13 pm
Posts: 2
Hi,

you are right.

When I thought about throwing an ApplicationException (checked, not Runtime) I totally forgot that an existing TX would not be rolled back.

Regards
Jan Zimmek


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 30, 2005 8:35 am 
Newbie

Joined: Fri Apr 08, 2005 7:15 am
Posts: 10
gavin wrote:
HibernateExceptions should certainly not be application exceptions because ... well ... they have nothing to do with "application".

They should be runtime, so that they force txn rollback.


Gavin,
I support your statement about HibernateExceptions when they represent low-level problems with the datastore or the mapping mechanism. Such exceptions are probably caused by infrastructure malfunctions or development bugs and are therefor useless for a client. They can smoothly be converted to a RemoteException or EJBException (by the EJB container).
On the other hand, i assume some HibernateExceptions can be usefull for a client. Particulary the constraint-violation-specific subclasses of JDBCException (e.g. representing a Unique Constraint) are sometimes usefull since they represent a recoverable problem which should be handled by the client (and which are for performance reasons not already validated in code). These exceptions shouldn't be converted to a RemoteException or EJBException (since it's not the responsability of the client to inspect the cause of this RemoteException).
I'm aware of the evilness of checked exceptions and I'm not advocating to re-introduce them (EJB3s EM will also throw unchecked exception) but I'm looking for a way to convert some HibernateExceptions in some cases to checked application exceptions (which will also mark the tx for rollback). My solution was to write a wrapper around the Hibernate Session which transforms the Hibernate exception into the correct application exception but this only works for the operations which are implicily called on the session and (I assume) not for the dirty checking mechanism. Correct?
- Is it possible to intercept the runtime exception which will be thrown when the first level cache gets synchronised with the DB => when the transaction commits (if the CMT was started on the session facade, the control will be already given back to the container)?
- What is your view on this kind of exception handling, based on your experience with Hibernate and in the EJB 3 expert group?

Thanx in advance,

Stijn Janssens


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 30, 2005 10:12 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
"Particulary the constraint-violation-specific subclasses of JDBCException (e.g. representing a Unique Constraint) are sometimes usefull since they represent a recoverable problem which should be handled by the client (and which are for performance reasons not already validated in code)."

Wrong


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 30, 2005 10:41 am 
Newbie

Joined: Fri Apr 08, 2005 7:15 am
Posts: 10
christian,

Why is my statement wrong?
I'll give a short example:
Consider a lottery game for which a client can pick 6 numbers. Each combination of numbers can only be assigned to one person. If a person picks an already-assigned combination, a specific error message should be shown and the user should get a chance to try again.
A lottery management system (ejb-module) containes the core functionality for registering the bets for all customers. Since the combination of numbers should be unique, a unique constraint is provided in the database.
In this case, it is possible to check the constraint in the EJB-module by launching a DB search before attempting to persist the bet. (I think this is were the validation belongs since it's a business constraint...)
However, if we add the functionality to perform bulk-bets (10000 combinations of numbers in one call on the system interface), it won't be performant to check the constraint (10000 times) before persisting this bulk bet. Isn't it more convenient to use the DB constraint instead and convert the specific JDBCException to an appropriate application exception?
Furthermore, if I use the business validation instead of the DB validation and someone (other tx) adds the same combination between the end of my validation and the end of my transaction, this would still result in a Unique Constraint exception, altough my validation was succesfull... correct?

Thanx for the reply,
Stijn


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.