Greetings, I have a doubt about Predestroy lifecycle callback interceptor and timeout for Stateful Session Bean: at page 245, paragraph "Transitioning out of Method-Ready state", O'Reilly's "Enterprise JavaBeans 3.0, fifth Edition" says: "When a timeout occurs in the Method-Ready state, the container, but is not required to, call any @PreDestroy callback methods". But at paragraph 4.4.3 of the specifications, you can read that: "The following scenarios result in Predestroy lifecycle callback interceptor method(s) not being called for an instance: 1. A crash of EJB container 2. A system exception thrown from the instance's method to the container 3. A timeout of client inactivity while the instance is in the passive state. [...]"
So, during the Method-Ready state, is the PreDestroy method invoked or not? Is it a mistake from my book?
The book's paragraph you quoted reads: "When a timeout occurs in the Method-Ready state, the container may, but is not required to, call any @PreDestroy callback methods". You forgot to transcript the word "may" which is crucial in this case: it may do it, but it is not required to.
What the specs say is that under some circumstances (EJB container crash, system exception thrown inside the instance's method or passivated instance timeout) Predestroy lifecycle callback interceptor method(s) are not called. But in all other cases (not passivated timed-out instances that do not throw a system exception) they are.
Hi guys, thanks for the answer. Well, what is not clear to me is: If a stateful session bean in method-ready state timeouts, MAY or MUST the container invoke @Predestroy ? O'Reilly book says it MAY, but the specs: 1. Reports it MUST, if you look at the stateful session bean state diagram 2. Says that the @Predestroy is not called in the above scenarios (see the original post)
If a stateful session bean instance in the method ready state times out, the container will NORMALLY call @PreDestroy unless that instance has already been passivated in which case calling the @PreDestroy method is optional.
On the other hand, the exceptions you are referring to (EJB container crash, system exception from the instance's method, timeout of client inactivity while in passivated state) have to do with Predestroy lifecycle callback interceptors and not with calling the @PreDestroy method itself.
I perfectly agree with all you say. But, at this point, I should consider an error from my O'Reilly book the following: "When a timeout occurs in the Method-Ready state, the container, but is not required to, call any @PreDestroy callback methods".
I don't see it as an error: both statements are talking about two different things:
- First one talks about a session bean's @PreDestroy lifecycle callback - The second one about PreDestroy lifecycle callback interceptors methods
Please, note the emphasis on the word "interceptor". It is not the same: one is talking about a session bean's lifecycle callback and the other one about interceptors on that session bean's lifecycle callback. Two different objects: session bean vs interceptor. [ June 27, 2008: Message edited by: Sergio Tridente ]
- Session's beans @PreDestroy callback methods may be called if the bean's instance is in passivated state. - PreDestroy lifecycle callback interceptors' methods will not be called when the instance is in passivated state.
Why is @PreDestroy not called when System Exception. I tried this, but in my case, it called @PreDestroy method. But the spec says that it does not call. If it does not call, where do we write the clean up code?