A client accesses an enterprise Bean either through the enterprise Bean's remote home and remote interfaces or through the enterprise Bean's local home and local interfaces, depending on the client view that the enterprise bean provides and whether the client is a remote client or a local client.
The remote home and remote interfaces are both Java RMI interfaces, and therefore the throws clauses of all their methods (including those inherited from superinterfaces) include the MANDATORY java.rmi.RemoteException.
The local home and local interfaces are both Java local interfaces, and the throws clauses of all their methods (including those inherited from superinterfaces) MUST NOT include the java.rmi.RemoteException. The throws clauses MAY include an arbitrary number of application exceptions.
If a client program receives an APPLICATION exception from an enterprise bean invocation, the client can continue calling the enterprise bean. An application exception DOES NOT result in the removal of the EJB object.
If a client program receives an application exception from an enterprise bean invocation while the client is associated with a transaction, the client can typically continue the transaction because an application exception DOES NOT automatically cause the Container to mark the transaction for rollback.
Although the Container does not automatically mark for rollback a transaction because of a thrown application exception, the transaction might have been marked for rollback by the enterprise bean instance before it threw the application exception.
Clients that are enterprise beans with container-managed transaction demarcation can use the getRollbackOnly() method of the javax.ejb.EJBContext object to learn if the current transaction has been marked for rollback; other clients may use the getStatus() method of the javax.transaction.UserTransaction interface to obtain the transaction status.
A remote client receives the java.rmi.RemoteException and a local client receives the javax.ejb.EJBException as an indication of a failure to invoke an enterprise bean method or to properly complete its invocation. The exception can be thrown by the Container OR by the communication subsystem between the client and the Container.
If the client receives the java.rmi.RemoteException or the javax.ejb.EJBException exception from a method invocation, the client, in general, does not know if the enterprise Bean's method has been completed or not.
If the client executes in the context of a transaction, the client's transaction MAY, OR MAY NOT, have been marked for rollback by the communication subsystem or target bean's Container.
The transaction MAY NOT necessarily be marked for rollback. This might occur, for example, when the communication subsystem on the client-side has not been able to send the request to the server.
When a client executing in a transaction context receives a RemoteException or an EJBException from an enterprise bean invocation, the client may use either of the following strategies to deal with the exception:
Discontinue the transaction. If the client is the transaction originator, it may simply rollback its transaction. If the client is not the transaction originator, it can mark the transaction for rollback or perform an action that will cause a rollback. For example, if the client is an enterprise bean, the enterprise bean may throw a RuntimeException which will cause the Container to rollback the transaction.
Continue the transaction. The client may perform additional operations on the same or other enterprise beans, and eventually attempt to commit the transaction. If the transaction was marked for rollback at the time the RemoteException or EJBException was thrown to the client, the commit will fail.
If the client chooses to continue the transaction, the client can first inquire about the transaction status to avoid fruitless computation on a transaction that has been marked for rollback. A client that is an enterprise bean with container-managed transaction demarcation can use the EJBContext.getRollbackOnly() method to test if the transaction has been marked for rollback; a client that is an enterprise bean with bean-managed transaction demarcation, and other client types, can use the UserTransaction.getStatus() method to obtain the status of the transaction.
If a client receives the javax.transaction.TransactionRolledbackException
The javax.transaction.TransactionRequiredException or javax.ejb.TransactionRequiredLocalException informs the client that the target enterprise bean must be invoked in a client's transaction, and that the client invoked the enterprise bean without a transaction context. This error usually indicates that the application was NOT PROPERLY FORMED.
The java.rmi.NoSuchObjectException is thrown to the client if a REMOTE business method cannot complete because the EJB object no longer exists.
The javax.ejb.NoSuchObjectLocalException is thrown to the client if a LOCAL business method cannot complete because the EJB object no longer exists.