EJB.12.2 Bean Provider s Responsibilities


EJB.12.2 Bean Provider's Responsibilities

This section describes the view and responsibilities of the bean provider with respect to exception handling.

EJB.12.2.1 Application Exceptions

The bean provider defines the application exceptions in the throws clauses of the methods of the remote and home interfaces. Because application exceptions are intended to be handled by the client, and not by the system administrator, they should be used only for reporting business logic exceptions, not for reporting system level problems.

The bean provider is responsible for throwing the appropriate application exception from the business method to report a business logic exception to the client. Because the application exception does not automatically result in marking the transaction for rollback, the bean provider must do one of the following to ensure data integrity before throwing an application exception from an enterprise bean instance:

  • Ensure that the instance is in a state such that a client's attempt to continue and/or commit the transaction does not result in loss of data integrity. For example, the instance throws an application exception indicating that the value of an input parameter was invalid before the instance performed any database updates.

  • Mark the transaction for rollback using the EJBContext.setRollbackOnly() method before throwing an application exception. Marking the transaction for rollback will ensure that the transaction can never commit.

An application exception must not be defined as a subclass of the java.lang.RuntimeException , or of the java.rmi.RemoteException . These are reserved for system exceptions (see next subsection).

The bean provider is also responsible for using the standard EJB application exceptions ( javax.ejb.CreateException , javax.ejb.RemoveException , javax.ejb.FinderException , and subclasses thereof) as described in Section EJB.9.1.9.

Bean providers may define subclasses of the standard EJB application exceptions and throw instances of the subclasses in the entity bean methods. A subclass will typically provide more information to the client that catches the exception.

EJB.12.2.2 System Exceptions

This subsection describes how the bean provider should handle various system-level exceptions and errors that an enterprise bean instance may encounter during the execution of a business method or a container callback method (e.g., ejbLoad ).

The enterprise bean business method and container callback methods may encounter various exceptions or errors that prevent the method from successfully completing. Typically, this happens because the exception or error is unexpected, or the exception is expected but the EJB provider does not know how to recover from it. Examples of such exceptions and errors are failure to obtain a database connection, JNDI API exceptions, unexpected RemoteException from invocation of other enterprise beans, [1] unexpected RuntimeException , Java Virtual Machine errors, and so on.

[1] Note that the enterprise bean business method may attempt to recover from a RemoteException . The text in this subsection applies only to the case when the business method does not wish to recover from the RemoteException .

If the enterprise bean method encounters a system-level exception or error that does not allow the method to complete successfully, the method should throw a suitable non-application exception that is compatible with the method's throws clause. While the EJB specification does not prescribe the exact usage of the exception, it encourages the bean provider to follow these guidelines:

  • If the bean method encounters a RuntimeException or error, it should simply propagate the error from the bean method to the container (i.e., the bean method does not have to catch the exception).

  • If the bean method performs an operation that results in a checked exception [2] that the bean method cannot recover, the bean method should throw the javax.ejb.EJBException that wraps the original exception.

    [2] A checked exception is one that is not a subclass of java.lang.RuntimeException .

  • Any other unexpected error conditions should be reported using the javax.ejb.EJBException.

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

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



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