Suppose I let my customer reserve seats on a plane using Stateful Session Bean. If the client explicitly calls my Remove method, all of his reservations will be cancelled and the bean is removed afterward.
However, in case the client is idle for some time and the Bean get passivated, if the Bean times out while being passivated, it would be deleted without calling any of my functions. Hence, I’d be very grateful if someone could show me how I can make sure that the reservations would be cancelled if the bean get deleted. If I use the @PreDestroy annotation, will it solve this problem?
Best regards,
James Tran
It is quite possible for the
@PreDestroymethod to not be invoked. The EJB 3.1 specification, explicitly states this:The specification also details how resources may be removed if the
@PreDestroymethod is not invoked in such scenarios:In your case, it would depend on how you are storing the state of your reservations. If they are persisted in the database, then I would suggest employing the same approach as mandated in the specification. You could use the EJB Timer service, to perform this activity periodically, or use a scheduler like Quartz. Note, that it is imperative to distinguish between the contents of passivated session bean instances that no longer exist, and those that will be made ready once again.