If the bean method runs in a transaction that the client started the client will receive a TransactionRolledbackException or ransactionRolledbackLocalException
Nileesha Bojjawar
Ranch Hand
Joined: Oct 10, 2003
Posts: 85
posted
0
Peter, I have a doubt, Did u mean IllegalArgumentException or IllegalStateException because i read that getUserTransaction() if called in a CMT Entity bean will throw IllegalStateException.
Regards,
Nileesha<br />SCJP 1.4, SCBCD, SCWCD.
Peter Warde
Ranch Hand
Joined: Aug 18, 2004
Posts: 71
posted
0
Sorry - I meant an IllegalStateException.
Nileesha Bojjawar
Ranch Hand
Joined: Oct 10, 2003
Posts: 85
posted
0
According to the Spec it says that IllegalStateException must be thrown but the container implementation might throw and more general Exception than the specific one, so it might throw a RemoteException/EJBException.I am confused for the same reason, if the exam question gives both the generic and the specific Exception as optinos,which one must be chosen?
Paul Maessen
Greenhorn
Joined: Sep 27, 2004
Posts: 12
posted
0
Hi Nileesha,
Maybe this helps:
IllegalStateException is thrown by the container to the *bean* (eg if the bean code makes a call to getUserTransaction() that is not allowed).
If the bean code doesn't handle the exception the container will throw a RemoteException/EJBException to the client.
I think the above is true, though someone in this forum somewhere recently said the exception to this was TransactionRolledBackException and it's local version. Can anybody tidy all this up to give an overall set of rules?
Paulus Maessen
Greenhorn
Joined: Sep 29, 2004
Posts: 12
posted
0
Hi Peter,
Below as I understand it (it’s a bit lengthy, but you asked for it ):
If you are calling bean methods directly, or methods on other objects like e.g. SessionContext, exceptions are thrown as-is since the container is not involved. Just plain old Java.
If you are calling bean methods through the home/component interface however, the container cicks in and calls the bean method on behalf of the client. Now the following rules apply:
- if the bean method throws an application exception the container re-throws it as-is to the client – if the bean method throws any other type of exception the container throws a RemoteException or EJBException or subclass thereof UNLESS the beans client started the transaction, in which case the container throws TransactionRolledbackException or TransactionRolledbackLocalException
An application exception is an exception that is declared to be thrown by the home/component method that is a (indirect) subclass of Exception but not of RuntimeException (+ some exceptions to this rule, see spec).
So: - runtime exceptions like IllegalStateException and NullpointerException can never be application exceptions and the container will always throw Remote/EJBException when the bean throws such an exception. - system exceptions may be re-thrown to the client, but only if they are explicitly included in the throws clause of the home/component interface - If a bean method makes a call to a remote interface, any RemoteExceptions or subclasses thereof thrown from the remote method must be handled by the bean code because bean methods are not allowed to throw RemoteExceptions - If a bean method makes a call to a local interface and the bean code doesn’t handle EJBExceptions or subclasses thereof : the container is required to throw a Remote/EJBException. I suppose that in the case of a local interface the container could just re-throw the exception, but I don’t think it is required to do so.
Hope this tidies it up,
Paul
- SCBCD
Sandesh Tathare
Ranch Hand
Joined: Jun 22, 2003
Posts: 82
posted
0
Gr8 job Paul!
I would like to comment on what you have written:
- system exceptions may be re-thrown to the client, but only if they are explicitly included in the throws clause of the home/component interface
I remember some one saying Container may throw Generic Exceptions instead of Specific Exception like CreateException instead of DuplicateKeyException. But that is certainly not applicable in the above mentioned case.
So to summarize it let me say:
System exceptions are wrapped in RemoteException and client receives RemoteException even if system exception is mentioned in throws clause of a method of a bean.
Regards,<br />Sandesh<br />(SCJCP, SCWCD, SCBCD - 99%, OCP-1)<br /> <br />Either find a way or create one.
Paulus Maessen
Greenhorn
Joined: Sep 29, 2004
Posts: 12
posted
0
Sandesh,
Thank you for correcting me: I got the terminolgy mixed up.
- system exceptions may be re-thrown to the client, but only if they are explicitly included in the throws clause of the home/component interface
should read
- checked exceptions are re-thrown to the client, but only if they are explicitly included in the throws clause of the home/component interface
just wanted to add one line here that exceptions local to the bean like SQLExceptions and others of similar nature should be avoided in throws clause of Home/Compnt Iface as they hold no meaning to the client.
This approach should be avoided even for debugging purposes.
All such exception should lead to throwing a EJBException/Remoteexception only.
Rahul Devgan<br />SCJP<br />--- one must be willing to let go of small pleasures in life to achieve greater things ---