web server - Borland Technical Publications

web server - Borland Technical Publications web server - Borland Technical Publications

techpubs.borland.com
from techpubs.borland.com More from this publisher
12.11.2014 Views

Handling of EJB exceptions Handling of EJB exceptions If a global transaction is associated with the current thread of execution do not use this method. If the global transaction is not a container-managed transaction, that is the application manages its own transactions, and a rollback is required use the JTA API to perform the rollback rather than invoking rollback() directly on the JDBC connection. Java.sql.Connection.close() As defined in the JDBC API, this method closes the database connection and all JDBC resources associated with the connection. If the thread is associated with a transaction this call simply notifies the JDBC pool that work on the connection is complete. The JDBC pool releases the connection back to the connection pool once the transaction has completed. JDBC connections opened by the JDBC pool cannot be closed explicitly by an application. Java.sql.Connection.setAutoCommit(boolean) As defined in the JDBC API, this method is used to set the auto commit mode of a transaction. The setAutoCommit() method allows Java applications to either: ■ Execute and commit all SQL statements as individual transactions (when set to true). This is the default mode, or ■ Explicitly invoke commit() or rollback() on the connection (when set to false). If the thread is associated with a transaction, the JDBC pool turns off the auto-commit mode for all connections factoried in the scope of a partition's transaction service transaction. This is because the transaction service must control transaction completion. If an application is involved with a transaction, and it attempts to set the auto commit mode to true, the java.sql.SQLException() will be raised. Enterprise JavaBeans can throw application and/or system level exceptions if they encounter errors while handling transactions. Application-level exceptions pertain to errors in the business logic and are intended to be handled by the calling application. System-level exceptions, such as runtime errors, transcend the application itself and can be handled by the application, the bean, or the bean container. The EJB declares application-level exceptions and system-level exceptions in the throws clauses of its Home and Remote interfaces. You must check for checked exceptions in your program try/catch block when calling bean methods. System-level exceptions An EJB throws a system-level exception, which is a java.ejb.EJBException (but may also be a java.rmi.RemoteException), to indicate an unexpected system-level failure. For example, it throws this exception if it cannot open a database connection. The java.ejb.EJBException is a runtime exception and does not have to be listed in the throws clause of the bean's business methods. System-level exceptions usually require the transaction to be rolled back. Often, the container managing the bean does the rollback. Other times, especially with beanmanaged transactions, the client must rollback the transaction. Chapter 19: Transaction management 179

Handling of EJB exceptions Application-level exceptions An EJB throws an application-level exception to indicate application-specific error conditions, that is, business logic errors and not system problems. These applicationlevel exceptions are exceptions other than java.ejb.EJBException. Application-level exceptions are checked exceptions, which means you must check for them when you call a method that potentially can throw this exception. The EJB's business methods use application exceptions to report abnormal application conditions, such as unacceptable input values or amounts beyond acceptable limits. For example, a bean method that debits an account balance can throw an application exception to report that the account balance is not sufficient to permit a particular debit operation. A client can often recover from these application-level errors without having to rollback the entire transaction. The application or calling program gets back the same exception that was thrown and this allows the calling program to know the precise nature of the problem. When an application-level exception occurs, the EJB instance does not automatically rollback the client's transaction. The client now has the knowledge and the opportunity to evaluate the error message, take the necessary steps to correct the situation, and recover the transaction. Otherwise, the client can abort the transaction. Handling application exceptions Because application-level exceptions report business logic errors, the client is expected to handle these exceptions. While these exceptions can require transaction rollback, they do not automatically mark the transaction for rollback. You often have the option to retry the transaction, though there are times when you must abort and rollback the transaction. The bean Provider is responsible for ensuring that the state of the bean is such that, if the client continues with the transaction, there is no loss of data integrity. If the Provider cannot ensure this degree of integrity, then the bean marks the transaction for rollback. Transaction rollback When your client program gets an application exception, you must first check if the current transaction has been marked for “rollback only”. For example, a client can receive a javax.transaction.TransactionRolledbackException. This exception indicates that the helper enterprise bean failed and the transaction has been aborted or marked “rollback only”. In general, the client does not know the transaction context within which the called enterprise bean operated. The called bean may have operated in its own transaction context separate from the calling program's transaction context, or it may have operated in the calling program's context. If the EJB operated in the same transaction context as the calling program, then the bean itself (or its container) may have already marked the transaction for rollback. When the EJB container has marked a transaction for rollback, the client should stop all work on the transaction. Normally, a client using declarative transactions will get an appropriate exception, such as javax.transaction.TransactionRolledbackException. Note that declarative transactions are those transactions where the container manages the transaction details. A client that is itself an EJB calls the javax.ejb.EJBContext.getRollbackOnly method to determine if its own transaction has been marked for rollback or not. For bean-managed transactions--those transactions managed explicitly by the client-- the client should rollback the transaction by calling the rollback method from the java.transaction.UserTransaction interface. 180 BES Developer’s Guide

Handling of EJB exceptions<br />

Application-level exceptions<br />

An EJB throws an application-level exception to indicate application-specific error<br />

conditions, that is, business logic errors and not system problems. These applicationlevel<br />

exceptions are exceptions other than java.ejb.EJBException. Application-level<br />

exceptions are checked exceptions, which means you must check for them when you<br />

call a method that potentially can throw this exception.<br />

The EJB's business methods use application exceptions to report abnormal application<br />

conditions, such as unacceptable input values or amounts beyond acceptable limits.<br />

For example, a bean method that debits an account balance can throw an application<br />

exception to report that the account balance is not sufficient to permit a particular debit<br />

operation. A client can often recover from these application-level errors without having<br />

to rollback the entire transaction.<br />

The application or calling program gets back the same exception that was thrown and<br />

this allows the calling program to know the precise nature of the problem. When an<br />

application-level exception occurs, the EJB instance does not automatically rollback<br />

the client's transaction. The client now has the knowledge and the opportunity to<br />

evaluate the error message, take the necessary steps to correct the situation, and<br />

recover the transaction. Otherwise, the client can abort the transaction.<br />

Handling application exceptions<br />

Because application-level exceptions report business logic errors, the client is<br />

expected to handle these exceptions. While these exceptions can require transaction<br />

rollback, they do not automatically mark the transaction for rollback. You often have the<br />

option to retry the transaction, though there are times when you must abort and<br />

rollback the transaction.<br />

The bean Provider is responsible for ensuring that the state of the bean is such that, if<br />

the client continues with the transaction, there is no loss of data integrity. If the Provider<br />

cannot ensure this degree of integrity, then the bean marks the transaction for rollback.<br />

Transaction rollback<br />

When your client program gets an application exception, you must first check if the<br />

current transaction has been marked for “rollback only”. For example, a client can<br />

receive a javax.transaction.TransactionRolledbackException. This exception indicates<br />

that the helper enterprise bean failed and the transaction has been aborted or marked<br />

“rollback only”. In general, the client does not know the transaction context within which<br />

the called enterprise bean operated. The called bean may have operated in its own<br />

transaction context separate from the calling program's transaction context, or it may<br />

have operated in the calling program's context.<br />

If the EJB operated in the same transaction context as the calling program, then the<br />

bean itself (or its container) may have already marked the transaction for rollback.<br />

When the EJB container has marked a transaction for rollback, the client should stop<br />

all work on the transaction. Normally, a client using declarative transactions will get an<br />

appropriate exception, such as javax.transaction.TransactionRolledbackException.<br />

Note that declarative transactions are those transactions where the container manages<br />

the transaction details.<br />

A client that is itself an EJB calls the javax.ejb.EJBContext.getRollbackOnly method to<br />

determine if its own transaction has been marked for rollback or not.<br />

For bean-managed transactions--those transactions managed explicitly by the client--<br />

the client should rollback the transaction by calling the rollback method from the<br />

java.transaction.UserTransaction interface.<br />

180 BES Developer’s Guide

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!