Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Enterprise JavaBeans™ Specification, Version 2.0 - Sun Microsystems.pdf
Скачиваний:
14
Добавлен:
24.05.2014
Размер:
2.71 Mб
Скачать

Sun Microsystems Inc

Client’s view of exceptions

Enterprise JavaBeans 2.0, Public Draft

Exception handling

17.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 causes 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 executed 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.

17.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.

351

5/31/00

Sun Microsystems Inc.

Exception handling

Enterprise JavaBeans 2.0, Public Draft

Client’s view of exceptions

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

17.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.

17.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.

5/31/00

352