-->
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.  [ 5 posts ] 
Author Message
 Post subject: Exceptionhandling with CMT
PostPosted: Wed Jul 04, 2007 6:48 am 
Newbie

Joined: Wed Jul 04, 2007 5:04 am
Posts: 2
Location: Essen - Germany
Dear all,

we are running are Hibernate 3 in WebSphere Application Server 6.1 and expose services through a stateless session bean to the frontend of the
application. We decided to use CMT for transaction demarcation, since we were under the impression that this is a recommended pattern.

Well, all that works fine unless it comes to a reasonable exception handling.
The issue is very simple: the scope of a service method in the bean demarcates a transaction. Therefore the transaction manager of the EJB container issues the commit AFTER we have left our bean method. If an exception is thrown, there is no way to catch and process it properly !

However this is what we need to do. We rely on database constraints to identify business errors like duplicate identification codes etc.

We are fully aware that there are many ways to use Hibernate. However my question is: Is there a way to catch exceptions on the server side running Hibernate with CMT in an EJB container ?

Since we were running out of time we opted to use BMT instead. However we like CMT in principle ;-)

Any comments are very much appreciated.


THANK YOU !!

-Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 12, 2007 5:34 pm 
Beginner
Beginner

Joined: Wed Oct 15, 2003 1:31 pm
Posts: 25
Hi Stefan,

Sorry, I don't have a solution for you, but I just ran into the same issue. Have you discovered a way to deal with this?

Many Thanks,

_________________
Rick Horowitz
---------
Please remember to rate...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 12, 2007 6:12 pm 
Newbie

Joined: Wed Jul 04, 2007 5:04 am
Posts: 2
Location: Essen - Germany
Hi Rick,

I am sorry, but we didn't manage to solve that problem.
We finally refactored the code to use BMT.

-Stefan

rickhoro234 wrote:
Hi Stefan,

Sorry, I don't have a solution for you, but I just ran into the same issue. Have you discovered a way to deal with this?

Many Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 11:57 am 
Beginner
Beginner

Joined: Wed Oct 15, 2003 1:31 pm
Posts: 25
Thanks Stephan.

Please read...I raise additional questions below.

After reading other postings on similar topics, I figured this would be the case. I think the EJB standards guys made a mistake here. imho, there should be a way to catch an exception in the EJB session bean when an SQL/JDBC exception occurs. I understand that the transaction is automatically trashed, but there still should be the option to catch and process the exception and then re-throw the exception.

As others have noted in these forums, this makes it impossible to catch a SQL constraint violation, e.g. duplicate value in a field defined as unique="true". How does one then issue an error message to the user? This seems to require doing a query first and check for all possible unique fields -- what an invitation for errors!

In addition, I could not figure out how to get the base Exception from the caller to the session bean method (e.g. the JSF managed bean). Anyone out there who managed to do this? If so, how?

Also, one of my applications is using Seam -- there are no JSF managed beans that are not also EJB session beans. In this case, there doesn't appear to be any way to catch SQL/JDBC exceptions at all.

Am I correct on all of this, or am I missing something?

Best Regards,

_________________
Rick Horowitz
---------
Please remember to rate...


Top
 Profile  
 
 Post subject: Re: Exceptionhandling with CMT
PostPosted: Fri Apr 29, 2011 6:06 pm 
Newbie

Joined: Thu Dec 01, 2005 1:02 pm
Posts: 5
Location: Nijmegen, Nederland (nl)
I struggled with this myself and eventually found that the trick is to use two beans. Place the code to do the persist in one bean and call that from the next. When the call in the first bean returns CMT commits the transaction and you will be able to catch EJBException in the other bean. Than just inspect the cause of that exception to figure out whether a constraint was violated or something else happened.

Code:
try {
   firstBean.call();
}
catch(EJBException ex) {
   Throwable cause = ex;
   while ((cause = cause.getCause()) != null) {
      if (cause instanceof SQLIntegrityConstraintViolationException) {
         // handle unique constraint violation etc
      }
   }
   throw ex;
}


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