Java Reference
In-Depth Information
of the
CreditCardSystemException
that would normally be wrapped as an
EJBException
because it would be interpreted as a system exception. Applying the
@ApplicationException
annotation causes it to be treated as an application excep-
tion instead.
You may have noticed the rollback attribute on the
@ApplicationException
annota-
tion. By default, application exceptions don't cause an automatic CMT rollback because the
rollback element is defaulted to
false
. But setting the element to
true
tells the container
that it should roll back the transaction before the exception is passed to the client. In
listing
temException
will result in a rollback before the exception is delivered. If the container
catches a system exception, such as an
ArrayIndexOutOfBounds
or a
NullPoint-
erException
that isn't caught, the container will still issue a rollback for these system
exceptions. But in such cases the container will assume that the bean is in an inconsistent
state and destroy it.
6.2.6. Session synchronization
The session synchronization interface enables stateful session beans to be notified of
container transaction boundaries. This interface is defined as
javax.ejb.SessionSynchronization
. To take advantage of this interface, im-
plement this interface as a part of your bean. This interface defines three methods:
•
void afterBegin()—
Called right after the container creates a new transac-
tion and before the business method is invoked.
•
void beforeCompletion()—
Invoked after a business method returns but
right before the container ends a transaction.
•
void afterCompletion(boolean committed)—
Called after the trans-
action finishes. The Boolean
committed
flag indicates whether a transaction was
committed or rolled back.
The first two methods are executed within the transactional context so you can read and
write data to the database. For example, you could use the
afterBegin()
to load cached
data into the bean and the
beforeCompletion()
to write data out to the bean.