• Post Reply Bookmark Topic Watch Topic
  • New Topic

about exception handling in EJB  RSS feed

 
Manish Malhotra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Im having on session bean, or any other EJB's & if any of its method will throw the SQLException or any checked exception.
And Im calling this bean from the same container then its giving me proper exception say if any db error occurs then giving the SQLExc...
But I want to know that if I'll a client which is on some other machine then will it be able to get the SQLException or other system level exception or it will get only RemoteException..
Please, help as quick as possible.
Im looking for the solution.
regards& thanks,
Manish
SE.
PCS.
 
Jason Stull
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always a tough question. Where do exceptions finally land? One approach I've seen is to have a Business Delegate on the client-side capture the RemoteException and then translate the exception in to something more meaningful, say a custom exception that could be displayed to the user in a meaningful form. (see J2EE Patterns Catalog: www.java.sun.com/blueprints/corej2eepatterns/Patterns/index.html)
So in your example, catch the SQLException, use the SQLException to create and throw a new EJBException, then catch it in your Business Delegate on the client side. At that point translate it in to some kind of custom exception that is displayable to the user.
Overall, I like using a Business Delegate to sheild the client from communication concerns and even from the fact that EJBs are being used! Hope this helps.
 
Manish Malhotra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"""" Always a tough question. Where do exceptions finally land? One approach I've seen is to have a Business Delegate on the client-side capture the RemoteException and then translate the exception in to something more meaningful, say a custom exception that could be displayed to the user in a meaningful form. (see J2EE Patterns Catalog: www.java.sun.com/blueprints/corej2eepatterns/Patterns/index.html)
So in your example, catch the SQLException, use the SQLException to create and throw a new EJBException, then catch it in your Business Delegate on the client side. At that point translate it in to some kind of custom exception that is displayable to the user.
Overall, I like using a Business Delegate to sheild the client from communication concerns and even from the fact that EJBs are being used! Hope this helps. """"""""
You said is quite correct but my problem is that if I throw EJBException when SQLExc. occured then could I get the errorCode from the EJBException object to identify the SQL error occured to show the pick a proper error message from the property file.
And my stateless session bean is Bean Managed therefore DB transaction stuff Im handling in the bean methods itself and dependent on the Container so that it will make rollback or commit when container get any System Level Exception.
Please reply as soon as possible.
Im waiting right now on net.
Manish
 
Jason Stull
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I do. Others may strongly disagree.
Rollback the transaction on an exception (bean managed in your case) to your Business Delegate. Create a custom exception class that takes the Remote Exception as a constructor. Make the custom class interpret the error for you, so you can make a decision as to how you want to handle it. Also, make the custom exception create a user-friendly message for you, if that is what's required.
When interpreting the original exception, look in the RemoteException's message for "CausedByException is: Some SQL Error", which will give you the initial SQL exception message. Good job for StringTokenizer.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!