Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Unable to catch SQLException in EJB Client...

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic