12.11.2014 Views

web server - Borland Technical Publications

web server - Borland Technical Publications

web server - Borland Technical Publications

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Handling of EJB exceptions<br />

Handling of EJB exceptions<br />

If a global transaction is associated with the current thread of execution do not use this<br />

method. If the global transaction is not a container-managed transaction, that is the<br />

application manages its own transactions, and a rollback is required use the JTA API to<br />

perform the rollback rather than invoking rollback() directly on the JDBC connection.<br />

Java.sql.Connection.close()<br />

As defined in the JDBC API, this method closes the database connection and all JDBC<br />

resources associated with the connection.<br />

If the thread is associated with a transaction this call simply notifies the JDBC pool that<br />

work on the connection is complete. The JDBC pool releases the connection back to<br />

the connection pool once the transaction has completed. JDBC connections opened by<br />

the JDBC pool cannot be closed explicitly by an application.<br />

Java.sql.Connection.setAutoCommit(boolean)<br />

As defined in the JDBC API, this method is used to set the auto commit mode of a<br />

transaction. The setAutoCommit() method allows Java applications to either:<br />

■<br />

Execute and commit all SQL statements as individual transactions (when set to<br />

true). This is the default mode, or<br />

■<br />

Explicitly invoke commit() or rollback() on the connection (when set to false).<br />

If the thread is associated with a transaction, the JDBC pool turns off the auto-commit<br />

mode for all connections factoried in the scope of a partition's transaction service<br />

transaction. This is because the transaction service must control transaction<br />

completion. If an application is involved with a transaction, and it attempts to set the<br />

auto commit mode to true, the java.sql.SQLException() will be raised.<br />

Enterprise JavaBeans can throw application and/or system level exceptions if they<br />

encounter errors while handling transactions. Application-level exceptions pertain to<br />

errors in the business logic and are intended to be handled by the calling application.<br />

System-level exceptions, such as runtime errors, transcend the application itself and<br />

can be handled by the application, the bean, or the bean container.<br />

The EJB declares application-level exceptions and system-level exceptions in the<br />

throws clauses of its Home and Remote interfaces. You must check for checked<br />

exceptions in your program try/catch block when calling bean methods.<br />

System-level exceptions<br />

An EJB throws a system-level exception, which is a java.ejb.EJBException (but may<br />

also be a java.rmi.RemoteException), to indicate an unexpected system-level failure.<br />

For example, it throws this exception if it cannot open a database connection. The<br />

java.ejb.EJBException is a runtime exception and does not have to be listed in the<br />

throws clause of the bean's business methods.<br />

System-level exceptions usually require the transaction to be rolled back. Often, the<br />

container managing the bean does the rollback. Other times, especially with beanmanaged<br />

transactions, the client must rollback the transaction.<br />

Chapter 19: Transaction management 179

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

Saved successfully!

Ooh no, something went wrong!