Search code examples
javajsfjakarta-eeview-scope

Java EE 6 - @PreDestroy in ViewScoped & Session beans never called


As I wrote in this topic I need to invoke some action when user close or change page. I thought I could use @PreDestroy in my view scoped bean, but it is never called (even when session expires). Is this a bug? I use Glassfish 3.1.2.

Any other suggestions how can I call bean method when user leave page? Is javascript window.onunload good idea?


Solution

  • Even if the bean is view scoped, it's the container's responsibility to decide when to collect and destroy unused resources.

    The annotations used for defining a scope indicate how long a managed bean has to survive, but you are not guaranteed about when they'll be destroyed.

    As you have suggested, JavaScript can be a solution for your problem. See also this answer: How to detect unsaved data in form when user leaves the page?