Search code examples
jsf-2java-ee-6cdiwebsphere-8

@PreDestroy not called after session expired


This is a JSF 2 app running on WAS8.0. Here is the code for the "backing" bean of one page.

    @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 web.xml is smaller than the bean's timeout. When I run the app, I see the printout from @PostConstruct but never see the one from @PreDestroy. I have tried the following 2 scenarios: 1. logout - invalidateSession; 2. simply wait until the session expires.

I'm not the designer of the app. The designer insists to make all the backing bean as a stateful session bean. I think the more mainstream approach would be just making them CDI bean. But anyway, when I do change the annotation to CDI only, I start getting the printout from @PreDestroy as well

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

My question is, what is the reason that I'm not getting the @PreDestroy method call in the first case? If I can't see the @PreDestroy get called, is there any other ways that I can trace the lifecycle of the "backing" beans (in this case, a stateful session bean). Thanks!


Solution

  • Look at this section of the Java EE 6 Tutorial, it shows the lifecycle of stateful session beans. The PreDestroy method is only called when the bean is explicitly removed from the client code, calling a method annotated with @Remove.