Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to catch SQLException in EJB Client...

 
Pras Tiwari
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have developed successfully EJB's for my application. Now inside my client I am accessing one of my bean & while creating bean inside client it throws SQLException. But inside my client my Catch block with "CreateException" catches that exception though I have written a catch block with "SQLException". My create method inside EJB as follows:-
===============
public CartMasterPK ejbCreate(Timestamp Cartdate, String Customerid,Double Totalvalue, String Status) throws CreateException, SQLException {
//--------implementation-----------
}
=================

& code inside my client which is accesiong create method is as follows:-
==========================
try {
cmaster=cmhome.create(new Timestamp(System.currentTimeMillis()),request.getParameter("customerid"),new Double(request.getParameter("totalvalue")),request.getParameter("status"));
}
catch (RemoteException e) {
e.printStackTrace();
}
catch (SQLException e) {
System.out.println("SQLEXCEPTION");
System.out.println(e.getMessage());
}
catch (CreateException e) {
System.out.println(e.getMessage());
}
==========================

Now according to above when create method throes SQLException it should be catched by Catch(SQLException e) block. But here in my client it get cought by catch (CreateException e) block.

Where is the problem???

Also is it necessry that both ejbCreate method & ejbPostCreate method should throw same number of esceptions???

Please help me.

Thanx in advance.

Prash
 
Chris Brat
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You have to read the spec - different exceptions will be returned to the client depending on whether :

- the client is local or remote
- the exception is an application exception (expected) or a system (unexcepted exception such as a NullPointer).
- the operation that is being performed (create, remote, find etc.)

Take a look at this tutorial
http://www-128.ibm.com/developerworks/java/library/j-ejbexcept.html

Chris
 
MI Mohammed
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tiwari,
What you have to do is wrap the SQLException and throw it as EJBException.
It the client is remote, the container will pass a RemoteException and for local client, they will get EJBExecepion.

eg
catch( SQLException sqlex) {
// do whatever thing you want to
throw new EJBException();
}
Hope it helps.
 
Reid M. Pinchback
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that if this entity bean is a CMP bean, you aren't ever going to get a SQLException. One might be chained to the relevant javax.ejb.* exception you get, but getting SQLException itself isn't possible unless you are using a BMP bean that causes SQLExceptions to be thrown.

Also, as a rule of thumb, propagating SQLExceptions outside of an entity bean is a questionable practice. Clients of a bean shouldn't know that much about its internals, and what exactly would a client *do* about a SQLException anyways? Better to wrap it in something more consistent with the API, like a standard EJB exception like CreateException or a generic application-specific exception.
[ March 09, 2006: Message edited by: Reid M. Pinchback ]
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For an entity bean, the only exception that must be declared in ejbCreate is CreateException plus any other exceptions declared in the matching create method of the home interface - but not RemoteException. As you have declared SQLException in ejbCreate, then it must be also be declared in create - an undesirable practice as Reid says.

If only CreateException is declared, then only CreateException can be thrown by the bean's implemented class. This will include situations when SQLException is encountered.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic