Java Reference
In-Depth Information
public void onDelete(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) throws CallbackException {
}
public boolean onFlushDirty(Object entity, Serializable id,
Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types) throws CallbackException {
return false;
}
public boolean onLoad(Object entity, Serializable id,
Object[] state, String[] propertyNames, Type[] types)
throws CallbackException {
return false;
}
public void postFlush(Iterator entities) throws CallbackException {
}
public void preFlush(Iterator entities) throws CallbackException {
}
}
Our interceptor makes use of the afterTransactionBegin() method to prepare to collect
booking details, the onSave() method to collect them, and the afterTransactionCompletion()
method to report the successful bookings. This sequence guarantees that bookings will not be
reported to the users until after we are confident that they have been retained in the database.
A minor deficiency of this implementation is that the e-mail is sent outside the transac-
tion—a system failure immediately after the commit completes could cause the e-mail not to
be sent. In our scenario, this is unimportant, because e-mail is already an unreliable transport
mechanism; but there are other situations, such as the auditing example discussed earlier, in
which this may be unacceptable. In these cases, interception may not be appropriate, and an
integrated solution tied into a two-phase commit transaction may be required.
More importantly, our example assumes that the Booking object will not be altered between
its addition to the set of e-mails to be sent and their transmission. This is an extremely danger-
ous assumption! A safer approach would be to create copies of the Booking objects or, better yet,
to copy their data into a more appropriate object, as shown in Listing A-27.
Listing A-27. A Better Approach to Preparing the Mailshot
public boolean onSave(Object entity, Serializable id,
Object[] state, String[] propertyNames, Type[] types)
throws CallbackException
{
if( entity instanceof Booking ) {
Booking booking = (Booking)entity.
Mailshot mailshot = new Mailshot(booking.getName(), booking.getSeat() );
Search WWH ::




Custom Search