File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes Confusion between PreDestroy  and Remove annotation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "Confusion between PreDestroy  and Remove annotation" Watch "Confusion between PreDestroy  and Remove annotation" New topic

Confusion between PreDestroy and Remove annotation

Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
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?
Steven Young
Ranch Hand

Joined: Apr 11, 2007
Posts: 36

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.
I agree. Here's the link:
subject: Confusion between PreDestroy and Remove annotation
It's not a secret anymore!