@PreDestroy is not called after the session ends

This is a JSF 2 application running on WAS8.0. Here is the code to support one page bean.

@Named("mySessionBean") @SessionScoped @Stateful @LocalBean @StatefulTimeout(unit = TimeUnit.MINUTES, value = 10) public class MySessionBean implements Serializable { @PostConstruct public void init() { System.out.println("start MySessionBean: " + this.hashCode()); } @PreDestroy public void cleanup() { System.out.println("destroy MySessionBean: " + this.hashCode()); } .... } 

The session timeout value set in the web.xml file is less than the bean timeout. When I launch the application, I see a printout from @PostConstruct, but I never see it from @PreDestroy. I tried the following 2 scripts: 1. logout - invalidateSession; 2. Just wait until the session ends.

I am not an application developer. The designer insists on making the bean support a session with a bean state. I think a more general approach would be to simply make them a CDI bean. But anyway, when I change the annotation only to CDI, I also start getting a printout from @PreDestroy

  @Named("mySessionBean") @SessionScoped public class MySessionBean implements Serializable { ..... 

My question is, why am I not getting the @PreDestroy method call in the first case? If I donโ€™t see how the @PreDestroy call is called, are there other ways I can track the beans โ€œsupportโ€ life cycle (in this case, a session with a bean state). Thanks!

+7
source share
2 answers

Take a look at this section of the Java EE 6 Tutorial , it shows the session life cycle with beans state. The PreDestroy method is called only when the bean is explicitly removed from the client code, calling the method annotated with @Remove.

+4
source

Another answer refers to the Java EE 6 Tutorial , which does not even mention the existence of timeouts. I also do not think that the tutorial clearly states that the @PreDestroy method is called only after the @Remove method is explicitly called. Causation is not specified, it simply describes two events that are not necessarily directly related:

At the end of the life cycle, the client calls the annotated @Remove method, and the EJB container calls the annotated @PreDestroy method, if any.

WebSphere docs mention the following:

The PreDestroy Lifecycle Interceptor callbacks are called for a session with a bean state when the delete method is called. Also keep in mind that PreDestroy lifecycle interceptor callbacks are not called if a session with a bean state expires during a passive state or if an unexpected exception occurs during a method call on the bean and bean are discarded.

Now itโ€™s clearly stated here that PreDestroy callbacks are called when the remove method is called. At the same time, it clearly states that PreDestroy callbacks do not call if the SFSB is in a passive state from time to time. Does this mean that they are called when the SFSB is not in a passive state?

Now let's look at JBoss.
Firstly, there is a JBoss problem that PreDestroy is not called when the delete timeout occurs, if removeTimeout <idleTimeout. It seems to be fixed in 5.2.
Secondly, I just tested JBoss 4.3, what happens when a passivated SFSB is deleted during a timeout - it is activated before destruction.

Thus, it seems that there are many interpretations of how timeouts and bean should behave.

+2
source

All Articles