user manual

Chapter 19: Transaction management 211
Handling of EJB exceptions
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.
Options for continuing a transaction
When a transaction is not marked for rollback, then the client has three
options:
Rollback the transaction.
Pass the responsibility by throwing a checked exception or re-throwing the
original exception.
Retry and continue the transaction. This can entail retrying portions of the
transaction.
When a client receives a checked exception for a transaction not marked for
rollback, its safest course is to rollback the transaction. The client does this by
either marking the transaction as "rollback only" or, if the client has actually
started the transaction, calling the rollback method to actually rollback the
transaction.