EJB.12.4 Client s View of Exceptions


EJB.12.4 Client's View of Exceptions

This section describes the client's view of exceptions received from an enterprise bean invocation.

A client accesses an enterprise bean through the enterprise bean's remote and home interfaces. Both of these interfaces are Java RMI interfaces, and therefore the throws clauses of all their methods (including those inherited from superinterfaces) include the mandatory java.rmi.RemoteException. The throws clauses may include an arbitrary number of application exceptions.

EJB.12.4.1 Application Exception

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.

For example, if a client receives the ExceedLimitException application exception from the debit method of an Account bean, the client may invoke the debit method again, possibly with a lower debit amount parameter. If the client exe-cuted in a transaction context, throwing the ExceedLimitException exception would not automatically result in rolling back, or marking for rollback, the client's transaction.

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. There are two ways to learn if a particular application exception results in transaction rollback or not:

  • Statically. Programmers can check the documentation of the enterprise bean's remote or home interface. The bean provider may have specified (although he is not required to) the application exceptions for which the enterprise bean marks the transaction for rollback before throwing the exception.

  • Dynamically. 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.

EJB.12.4.2 java.rmi.RemoteException

The client receives the java.rmi.RemoteException 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 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.

For example, the transaction would be marked for rollback if the underlying transaction service or the target bean's container doubted the integrity of the data because the business method may have been partially completed. Partial completion could happen, for example, when the target bean's method returned with a RuntimeException exception, or if the remote server crashed in the middle of executing the business method.

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 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 roll back 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 roll back 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 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.

Some implementations of EJB servers and containers may provide more detailed exception reporting by throwing an appropriate subclass of the java.rmi.RemoteException to the client. The following subsections describe the several subclasses of the java.rmi.RemoteException that may be thrown by the container to give the client more information.

EJB.12.4.2.1 javax.transaction.TransactionRolledbackException

The javax.transaction.TransactionRolledbackException is a subclass of the java.rmi.RemoteException. It is defined in the JTA standard extension.

If a client receives the javax.transaction.TransactionRolledbackException , the client knows for certain that the transaction has been marked for rollback. It would be fruitless for the client to continue the transaction because the transaction can never commit.

EJB.12.4.2.2 javax.transaction.TransactionRequiredException

The javax.transaction.TransactionRequiredException is a subclass of the java.rmi.RemoteException. It is defined in the JTA standard extension.

The javax.transaction.TransactionRequiredException 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 .

EJB.12.4.2.3 java.rmi.NoSuchObjectException

The java.rmi.NoSuchObjectException is a subclass of the java.rmi.RemoteException. It is thrown to the client if a remote business method cannot complete because the EJB object no longer exists.



Java 2 Platform, Enterprise Edition. Platform and Component Specifications
Java 2 Platform, Enterprise Edition: Platform and Component Specifications
ISBN: 0201704560
EAN: 2147483647
Year: 2000
Pages: 399

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net