web server - Borland Technical Publications
web server - Borland Technical Publications web server - Borland Technical Publications
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
- Page 139 and 140: Container-managed persistence and R
- Page 141 and 142: Container-Managed Persistence in Bo
- Page 143 and 144: Container-Managed Persistence in Bo
- Page 145 and 146: Container-Managed Persistence in Bo
- Page 147 and 148: Container-Managed Persistence in Bo
- Page 149 and 150: Container-Managed Persistence in Bo
- Page 151 and 152: Container-Managed Persistence in Bo
- Page 153 and 154: 142 BES Developer’s Guide
- Page 155 and 156: Setting Properties J2EE 1.3 Entity
- Page 157 and 158: Setting Properties Figure 16.2 Edit
- Page 159 and 160: Setting Properties Table 16.1 ejb.m
- Page 161 and 162: Setting Properties Table 16.3 Table
- Page 163 and 164: Setting Properties Security Propert
- Page 165 and 166: Aggregate Functions in EJB-QL Selec
- Page 167 and 168: Support for ORDER BY Support for OR
- Page 169 and 170: Overriding SQL generated from EJB-Q
- Page 171 and 172: Container-managed data access suppo
- Page 173 and 174: 162 BES Developer’s Guide
- Page 175 and 176: Generating primary keys from a cust
- Page 177 and 178: Implementing primary key generation
- Page 179 and 180: Transaction manager services Consis
- Page 181 and 182: Transaction manager services When t
- Page 183 and 184: Transaction manager services Follow
- Page 185 and 186: Declarative transaction management
- Page 187 and 188: Declarative transaction management
- Page 189: JDBC API Modifications JDBC API Mod
- Page 193 and 194: 182 BES Developer’s Guide
- Page 195 and 196: Client View of an MDB Client View o
- Page 197 and 198: Clustering of MDBs This is yet anot
- Page 199 and 200: Error Recovery Redelivered messages
- Page 201 and 202: 190 BES Developer’s Guide
- Page 203 and 204: JNDI Definitions Module Important s
- Page 205 and 206: Disabling and Enabling a Deployed D
- Page 207 and 208: Configuring JDBC Datasources In the
- Page 209 and 210: Configuring JDBC Datasources To add
- Page 211 and 212: Defining the Connection Pool Proper
- Page 213 and 214: Defining the Connection Pool Proper
- Page 215 and 216: Descriptions of Borland Enterprise
- Page 217 and 218: Advanced Topics for Defining JDBC D
- Page 219 and 220: Connecting to JDBC Resources from J
- Page 221 and 222: Configuring JMS Connection Factorie
- Page 223 and 224: Defining Connection Pool Properties
- Page 225 and 226: Obtaining JMS Connection Factories
- Page 227 and 228: JMS and Transactions and its accomp
- Page 229 and 230: JMS and Transactions For instance:
- Page 231 and 232: 220 BES Developer’s Guide
- Page 233 and 234: Configuring JMS administered object
- Page 235 and 236: Tibco Creating Clustered JMS Servic
- Page 237 and 238: Sonic serverUrl String localhost:72
- Page 239 and 240: Sonic Creating Clustered JMS Servic
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