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

Sun Microsystems Inc.

Exception handling

Enterprise JavaBeans v1.1, Final Release

Container Provider responsibilities

The Container catches a non-application exception, logs it (which can result in alerting the System Administrator), and throws the java.rmi.RemoteException (or subclass thereof) to the client. The Bean Provider can rely on the Container to perform the following tasks when catching a non-appli- cation exception:

The transaction in which the bean method participated will be rolled back.

No other method will be invoked on an instance that threw a non-application exception.

This means that the Bean Provider does not have to perform any cleanup actions before throwing a non-application exception. It is the Container that is responsible for the cleanup.

12.2.2.1 javax.ejb.NoSuchEntityException

The NoSuchEntityException is a subclass of EJBException. It should be thrown by the entity bean class methods to indicate that the underlying entity has been removed from the database.

An entity bean class typically throws this exception from the ejbLoad and ejbStore methods, and from the methods that implement the business methods defined in the remote interface.

12.3 Container Provider responsibilities

This section describes the responsibilities of the Container Provider for handling exceptions. The EJB architecture specifies the Container’s behavior for the following exceptions:

Exceptions from enterprise bean’s business methods.

Exceptions from container-invoked callbacks on the enterprise bean.

Exceptions from management of container-managed transaction demarcation.

12.3.1 Exceptions from an enterprise bean’s business methods

Business methods are considered to be the methods defined in the enterprise bean’s remote and home interface (including all their superinterfaces); and the following methods: ejbCreate(...), ejbPostCreate(...), ejbRemove(), and the ejbFind<METHOD> methods.

11/24/99

190

Sun Microsystem Inc

Container Provider responsibilities

Enterprise JavaBeans v1.1, Final Release

Exception handling

Table 8 specifies how the Container must handle the exceptions thrown by the business methods for beans with container-managed transaction demarcation. The table specifies the Container’s action as a function of the condition under which the business method executes and the exception thrown by the business method. The table also illustrates the exception that the client will receive and how the client can recover from the exception. (Section 12.4 describes the client’s view of exceptions in detail.)

Table 8

Handling of exceptions thrown by a business method of a bean with container-managed transaction

 

demarcation

 

 

 

 

 

 

 

 

 

Method condition

Method exception

Container’s action

Client’s view

 

 

 

 

 

 

 

 

 

 

 

 

AppException

Re-throw AppException

Receives AppException.

 

 

 

 

Can attempt to continue

 

 

 

 

computation in the trans-

 

 

 

 

action, and eventually

 

 

 

 

commit the transaction

 

 

 

 

(the commit would fail if

 

Bean method runs in the

 

 

the instance called set-

 

context of the caller’s

 

 

RollbackOnly()).

 

transaction [Note A].

 

 

 

 

 

 

Receives Transaction-

 

This case may happen

all other exceptions and

Log the exception or

 

with Required, Man-

errors

error [Note B].

RolledBackException.

 

datory, and Sup-

 

Mark the transaction for

Continuing transaction is

 

ports attributes.

 

 

 

rollback.

fruitless.

 

 

 

 

 

 

Discard instance

 

 

 

 

[Note C].

 

 

 

 

Throw Transaction-

 

 

 

 

RolledBackException to

 

 

 

 

the client.

 

 

 

 

 

 

 

 

AppException

If the instance called set-

Receives AppException.

 

 

 

RollbackOnly(), then

If the client executes in a

 

 

 

rollback the transaction,

 

 

 

transaction, the client’s

 

 

 

and re-throw AppExcep-

 

 

 

transaction is not marked

 

Bean method runs in the

 

tion.

 

 

for rollback, and client

 

context of a transaction

 

 

 

 

Otherwise, attempt to

can continue its work.

 

that the Container started

 

 

 

commit the transaction,

 

 

immediately before dis-

 

 

 

 

and then re-throw

 

 

patching the business

 

 

 

 

AppException.

 

 

method.

 

 

 

 

 

 

 

This case may happen

all other exceptions

Log the exception or

Receives RemoteExcep-

 

with Required and

 

 

error.

tion.

 

RequiresNew

 

 

 

 

 

 

attributes.

 

Rollback the con-

If the client executes in a

 

 

 

tainer-started transaction.

transaction, the client’s

 

 

 

Discard instance.

transaction is not marked

 

 

 

for rollback, and client

 

 

 

 

 

 

 

Throw RemoteException.

can continue its work.

 

 

 

 

 

191

11/24/99

Sun Microsystems Inc.

Exception handling

Enterprise JavaBeans v1.1, Final Release

Container Provider responsibilities

Table 8

Handling of exceptions thrown by a business method of a bean with container-managed transaction

 

demarcation

 

 

 

 

 

 

 

 

 

Method condition

Method exception

Container’s action

Client’s view

 

 

 

 

 

 

 

 

 

 

 

 

AppException

Re-throw AppException.

Receives AppException.

 

 

 

 

If the client executes in a

 

 

 

 

transaction, the client’s

 

Bean method runs with

 

 

transaction is not marked

 

 

 

for rollback, and client

 

an unspecified transac-

 

 

 

 

 

can continue its work.

 

tion context.

 

 

 

 

 

 

 

This case may happen

all other exceptions

Log the exception or

Receives RemoteExcep-

 

with the NotSup-

 

 

error.

tion.

 

ported, Never, and

 

 

 

 

 

 

Supports attributes.

 

Discard instance.

If the client executes in a

 

 

 

Throw RemoteException.

transaction, the client’s

 

 

 

transaction is not marked

 

 

 

 

 

 

 

 

for rollback, and client

 

 

 

 

can continue its work.

 

 

 

 

 

 

Notes:

 

 

 

[A]The caller can be another enterprise bean or an arbitrary client program.

[B]Log the exception or error means that the Container logs the exception or error so that the System Administrator is alerted of the problem.

[C]Discard instance means that the Container must not invoke any business methods or container callbacks on the instance.

Table 9 specifies how the Container must handle the exceptions thrown by the business methods for beans with bean-managed transaction demarcation[16]. The table specifies the Container’s action as a function of the condition under which the business method executes and the exception thrown by the business method. The table also illustrates the exception that the client will receive and how the client can recover from the exception. (Section 12.4 describes the client’s view of exceptions in detail.)

Table 9

Handling of exceptions thrown by a business method of a session with bean-managed transaction

 

demarcation

 

 

 

 

 

 

 

 

 

Bean method condition

Bean method exception

Container action

Client receives

 

 

 

 

 

 

 

 

 

 

 

 

AppException

Re-throw AppException

Receives AppException.

 

 

 

 

 

 

 

all other exceptions

Log the exception or

Receives RemoteExcep-

 

 

 

error.

tion.

 

Bean is stateful or state-

 

Mark for rollback a

 

 

less Session.

 

transaction that has been

 

 

 

 

started, but not yet com-

 

 

 

 

pleted, by the instance.

 

 

 

 

Discard instance.

 

 

 

 

Throw RemoteException.

 

 

 

 

 

 

[16] Note that the EJB specification allows only Session beans to use bean-managed transaction demarcation.

11/24/99

192

Sun Microsystem Inc

Container Provider responsibilities

Enterprise JavaBeans v1.1, Final Release

Exception handling

12.3.2 Exceptions from container-invoked callbacks

This subsection specifies the Container’s handling of exceptions thrown from the container-invoked callbacks on the enterprise bean. This subsection applies to the following callback methods:

The ejbActivate(), ejbLoad(), ejbPassivate(), ejbStore(), setEntityContext(EntityContext), and unsetEntityContext() methods of the EntityBean interface.

The ejbActivate(), ejbPassivate(), and setSessionContext(SessionContext) methods of the SessionBean interface.

The afterBegin(), beforeCompletion() and afterCompletion(boolean) methods of the SessionSynchronization interface.

The Container must handle all exceptions or errors from these methods as follows:

Log the exception or error to bring the problem to the attention of the System Administrator.

If the instance is in a transaction, mark the transaction for rollback.

Discard the instance (i.e. the Container must not invoke any business methods or container callbacks on the instance).

If the exception or error happened during the processing of a client invoked method, throw the java.rmi.RemoteException to the client. If the instance executed in the client’s transaction, the Container should throw the javax.transaction.TransactionRolledBackException because it provides more information to the client. (The client knows that it is fruitless to continue the transaction.)

12.3.3 javax.ejb.NoSuchEntityException

The NoSuchEntityException is a subclass of EJBException. If it is thrown by a method of an entity bean class, the Container must handle the exception using the rules for EJBException described in Sections 12.3.1 and 12.3.2.

To give the client a better indication of the cause of the error, the Container should throw the java.rmi.NoSuchObjectException to the client (which is a subclass of java.rmi.RemoteException).

12.3.4 Non-existing session object

If a client makes a call to a session object that has been removed, the Container should throw the java.rmi.NoSuchObjectException to the client (which is a subclass of java.rmi.RemoteException).

193

11/24/99