aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
If the remote client does not extend Remote, will the client get RemoteException? or rather in what cases the client will get remote exception.


SCJP 1.4, SCWCD 1.4, SCBCD 1.5
vitesse wei
Ranch Hand

Joined: Sep 07, 2007
Posts: 100
I think whether client get RemoteException is not determined by client implement Remote or not,is determined by if the business method is remote and did it implement Remote interface(please reference master ejb3 eppendix E,too long to refer here)


SCJP 5.0<br />SCWCD1.4<br />SCBCD5
Benoît de Chateauvieux
Ranch Hand

Joined: Aug 10, 2007
Posts: 183
Hi Anu Tilwalli,

In EJB2, the remote interface of a Session Bean implements java.rmi.Remote and all the methods must throw the java.rmi.RemoteException.
So, a remote client of an EJB2 Session Bean will possibly get a java.rmi.RemoteException.

In EJB3, a remote business interface must not implement java.rmi.Remote.
And the throws clauses should not include the java.rmi.RemoteException.
So, a remote client of an EJB3 Session Bean will never get java.rmi.RemoteException.

Hope it helps,

Beno�t


SCJP5 | SCBCD5 | SCEA5 Part 1
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
But in some of the books it says that the container wraps the exception into RemoteException for remote clients.
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
Read the specs

1. For MDB, Other beans it throws javax.ejb.EJBException
2. For webservice client it throws javax.rmi.RemoteException
3. For EJB 2.1 Client
throws RemoteException if its remote client
throws EJBException if its local client
4. Throws EJB 3.0 Client
If the remote extends java.rmi.REMOTE remote exception in thrown.
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
If bean method runs in the transaction started by the container[Required or Require New]

If any application exception is raised

Containers Action
1. Should rethrow the app exception
2. If instance calls setRollBack, then rollback or do rollback depending up @ApplicationAnnotation type & rethrow the exception.

Client View
1. Receive application exception
1. If the client executes in a transaction, the client transaction is not marked for roll back

Would the transaction is still not marked for rollback if the client initiated the transaction. I mean if the client had initiated, the transaction would have been propogated to the bean method[In case of Required].

The exception chart in the spec is a bit confusing.
Derick Potgieter
Ranch Hand

Joined: Feb 19, 2004
Posts: 53
From what i understand this is the rule

[ April 03, 2008: Message edited by: Derick Potgieter ]

SCJP, SCJD, SCWCD, SCBCD (JEE)
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
This is what I thought. But if you the see the spec, it says that this would happen only if the remote interface implements Remote. But when I tried this, I always got EJBException.

Any thoughts on it?
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
This is what I thought. But if you the see the spec, it says that this would happen only if the remote interface implements Remote. But when I tried this, I always got EJBException.

Any thoughts on it?
Witt Lilavivat
Greenhorn

Joined: Jan 27, 2005
Posts: 13
hi i read from Manning book EJB 3 in Action
page 88 about the Remote business interface.

A remote business interface may extend java.rmi.Remote as we�ve done here,
although this is optional. Typically the container will perform byte-code enhancements during deployment to extend java.rmi.Remote if your bean interface does not extend it. Remote business interface methods are not required to throw java.rmi.RemoteException unless the business interface extends the java.rmi.Remote interface.


Is that mean if I explicit extends the Remote business interface with java.rmi.RemoteExcepton , client will get RemoteException instead of EJBException ?
[ July 04, 2008: Message edited by: Witt Lilavivat ]

SCJA SCJP1.4 SCJP5 SCWCD1.4 SCBCD1.3 SCBCD5
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Accordingly to O'reilly's book (page 397):

System exceptions include java.lang.RuntimeException and its subclasses. EJBException is a subclass of RuntimeException, so it is considered a system exception. System exceptions also include java.rmi.RemoteException and its subclasses. The RuntimeException and RemoteException subclasses differ in that they can be turned into application exceptions using the @javax.ejb.ApplicationException annotation.


Later in the same chapter (page 399):

In session beans, when a system exception occurs and the instance is discarded, a RuntimeException is always thrown whether the client is a remote or local invocation. If the client started the transaction, which was then propagated to the EJB, a system exception (thrown by the enterprise bean method) will be caught by the container and rethrown as a javax.ejb.EJBTransactionRolledbackException. EJBTransactionRolledbackException is a subtype of RuntimeException and gives a more explicit indication to the client that a rollback occurred. If the client did not propagate a transaction to the EJB, the system exception will be caught and rethrown as an EJBException.


I think this explains well why Anu was always getting an EJBException: the RemoteException is caught and rethwon as a EJBException (or an EJBTransactionRolledbackException if the transactions originated from the client).


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

After reading the relevant chapter in the spces, I am taking back what I wrote: there is something wrong with O'Reilly's book regarding RemoteException.

I did some tests on JBoss 4.2.2GA and this is what I got:

Case 1: no transaction on client, EJB initiated transaction
- When the business method throws a java.lang.Exception, the client receives a java.lang.Exception
- When the business method throws a java.lang.RuntimeException, the client receives a javax.ejb.EJBException
- When the business method throws a java.lang.ArithmeticException (subclass of RuntimeException), the client receives a javax.ejb.EJBException
- When the business method throws a java.rmi.RemoteException, the client receives a java.rmi.RemoteException
- When the business method throws a java.rmi.ServerException (subclass of RemoteException), the client receives a java.rmi.ServerException

Case 2: client initiated transaction propagated to the EJB
- When the business method throws a java.lang.Exception, the client receives a java.lang.Exception
- When the business method throws a java.lang.RuntimeException, the client receives a javax.ejb.EJBTransactionRolledbackException
- When the business method throws a java.lang.ArithmeticException, the client receives a javax.ejb.EJBTransactionRolledbackException
- When the business method throws a java.rmi.RemoteException, the client receives a javax.ejb.EJBTransactionRolledbackException
- When the business method throws a java.rmi.ServerException, the client receives a javax.ejb.EJBTransactionRolledbackException

Case 3: client initiated transaction that does not propagate to the client (TransactionAttributeType.NOT_SUPPORTED)
- When the business method throws a java.lang.Exception, the client receives a java.lang.Exception
- When the business method throws a java.lang.RuntimeException, the client receives a java.lang.RuntimeException
- When the business method throws a java.lang.ArithmeticException, the client receives a java.lang.ArithmeticException
- When the business method throws a java.rmi.RemoteException, the client receives a java.rmi.RemoteException
- When the business method throws a java.rmi.ServerException, the client receives a java.rmi.ServerException

This does not follow what the ejb-core specs say. I guess it is a problem with JBoss. I will install Glassfish anf try it again. I will post back the results.
[ July 06, 2008: Message edited by: Sergio Tridente ]
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

I tried the same three cases in Glassfish. Here are the results:

Case 1: no transaction on client, EJB initiated transaction
- When the business method throws a java.lang.Exception, the client receives a java.lang.Exception
- When the business method throws a java.lang.RuntimeException, the client receives a java.rmi.ServerException
- When the business method throws a java.lang.ArithmeticException (subclass of RuntimeException), the client receives a java.rmi.ServerException
- When the business method throws a java.rmi.RemoteException, the client receives a java.rmi.ServerException
- When the business method throws a java.rmi.ServerException (subclass of RemoteException), the client receives a java.rmi.ServerException

Case 2: client initiated transaction propagated to the EJB
- When the business method throws a java.lang.Exception, the client receives a java.lang.Exception
- When the business method throws a java.lang.RuntimeException, the client receives a javax.transaction.TransactionRolledbackException
- When the business method throws a java.lang.ArithmeticException, the client receives a javax.transaction.TransactionRolledbackException
- When the business method throws a java.rmi.RemoteException, the client receives a javax.transaction.TransactionRolledbackException
- When the business method throws a java.rmi.ServerException, the client receives a javax.transaction.TransactionRolledbackException

Case 3: client initiated transaction that does not propagate to the client (TransactionAttributeType.NOT_SUPPORTED)
- When the business method throws a java.lang.Exception, the client receives a java.lang.Exception
- When the business method throws a java.lang.RuntimeException, the client receives a java.rmi.ServerException
- When the business method throws a java.lang.ArithmeticException, the client receives a java.rmi.ServerException
- When the business method throws a java.rmi.RemoteException, the client receives a java.rmi.ServerException
- When the business method throws a java.rmi.ServerException, the client receives a java.rmi.ServerException

This definitely works more like what's written in the specifications. It looks like a Jboss problem to me.
[ July 06, 2008: Message edited by: Sergio Tridente ]
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I get remote Exception for the remote client only when the bean method throws Remote Exception.

But in spec & in many mocks, it says that if its a remote client it should throw remote exception.

Which is correct?
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

The specs, of course. As I said, it works fine on Glassfish: it throws ServerException, which is a subclass of RemoteException, when the Bean's interface extends Remote. But it throws EJBException when it does not extend Remote, even if it is annotated with @Remote.
 
 
subject: Exceptions