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

Sun Microsystems Inc.

Exception handling

Enterprise JavaBeans 2.0, Public Draft

Container Provider responsibilities

Note that the javax.ejb.EJBException is a subclass of the java.lang.RuntimeException, and therefore it does not have to be listed in the throws clauses of the business methods.

The Container catches a non-application exception, logs it (which can result in alerting the System Administrator), and, unless the bean is a message-driven bean, 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-application 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.

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

17.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 the business methods of session and entity beans.

Exceptions from message-driven bean methods

Exceptions from container-invoked callbacks on the enterprise bean.

Exceptions from management of container-managed transaction demarcation.

17.3.1 Exceptions from a session or entity 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 session bean or entity bean methods: ejbCreate<METHOD>(...), ejbPostCreate<METHOD>(...), ejbRemove(), and the ejbFind<METHOD> methods.

5/31/00

344

Sun Microsystems Inc

Container Provider responsibilities

Enterprise JavaBeans 2.0, Public Draft

Exception handling

Table 12 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 17.4 describes the client’s view of exceptions in detail.)

Table 12

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.

 

 

 

 

 

345

5/31/00

Sun Microsystems Inc.

Exception handling

Enterprise JavaBeans 2.0, Public Draft

Container Provider responsibilities

Table 12

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 13 specifies how the Container must handle the exceptions thrown by the business methods for beans with bean-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 17.4 describes the client’s view of exceptions in detail.)

Table 13

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.

 

 

 

 

 

 

5/31/00

346

Sun Microsystems Inc

Container Provider responsibilities

Enterprise JavaBeans 2.0, Public Draft

Exception handling

17.3.2 Exceptions from message-driven bean methods

This section specifies the Container’s handling of exceptions thrown from a message-driven bean’s onMessage, ejbCreate(), and ejbRemove() methods.

Table 14 specifies how the Container must handle the exceptions thrown by the onMessage, ejbCreate, and ejbRemove methods for message-driven beans with container-managed transaction demarcation. The table specifies the Container’s action as a function of the condition under which the method executes and the exception thrown by the method. Message-driven bean methods, unlike the business methods of session or entity beans, do not throw application exceptions and cannot throw exceptions to the client.

Table 14

Handling of exceptions thrown by a method of a message-driven bean with container-managed

 

transaction demarcation.

 

 

 

 

 

 

 

 

 

Method condition

Method exception

Container’s action

 

 

 

 

 

 

 

 

 

 

 

 

Bean method runs in the

system exceptions

Log the exception or

 

 

 

error[Note A].

 

 

context of a transaction

 

 

 

 

 

 

 

that the Container started

 

Rollback the con-

 

 

immediately before dis-

 

tainer-started transaction.

 

 

patching the method.

 

Discardinstance[NoteB].

 

 

This case happens with

 

 

 

Required attribute.

 

 

 

 

 

 

 

 

 

Bean method runs with

system exceptions

Log the exception or

 

 

an unspecified transac-

 

error.

 

 

tion context.

 

Discard instance.

 

 

This case happens with

 

 

 

 

 

 

 

the NotSupported

 

 

 

 

attribute.

 

 

 

 

 

 

 

 

Notes:

 

 

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

[B]Discard instance means that the Container must not invoke any methods on the instance.

347

5/31/00