Confusion between PreDestroy and Remove annotation
Joined: Feb 07, 2005
Hi I am able to find difference between PreDestroy and Remove callback annotation. What i know that Remove annotation is used to tell container to remove statefull bean.The PreDestroy annotation is called after Remove annotation.
Why we need PreDestroy when there is Remove anotation? We can do same thing in Remove annotation what we can do in PreDestroy annotation.
When bean is removed then how container call PreDestroy on bean which has been already removed?
Joined: Apr 11, 2007
The @PreDestroy annotation, along with the @PostConstruct is a standard lifecycle callback method for all session beans. This includes Stateful and Stateless session beans.
The @Remove annotation is only available to business methods of Stateful session beans so that the container knows when the client has finished the stateful conversation. The container can then destroy the stateful session bean and reclaim system resource. Think of the @Remove callback method as a "marker" so the container knows you have finished with the bean. The container will not destory the bean straight after it completes the @Remove method, it will still run the @PreDestroy callback. How long the stateful bean hangs around before the @PreDestroy method is actually called is up to the EJB Container vendor.
So, although it appears that the 2 callback methods do the same thing, the @PreDestroy callback method is standard for all session beans (stateful or stateless) and should be used specifically to close and release any resources such as JDBC connections.