@PreDestroy never called on @ViewScoped [duplicate]
Asked Answered
H

1

16

I have a @ViewScoped bean that has a method with an @PreDestroy annotation that should make sure some remote connections are closed. However, the method is not called when the user navigates away.

Is there anything one can do wrong? Do I have to register anything anywhere in order to make it work?

It's a simple public method (void) that throws no exception.

I'm using JSF 2 (MyFaces) with Tomcat 7.0.12. Could it be a problem with Tomcat?

UPDATE

The @PostConstruct annotation works fine.

Hyperkinesia answered 16/6, 2011 at 8:4 Comment(0)
K
14

This is a known issue and unfortunately not trivial to solve without letting all the navigation take place through the view scoped bean in question. See also JSF-impl issue 1839. This does only not cover cases where the enduser changes the URL in browser address bar or closes the window/tab. Their @PreDestroy will also not be called when the session get destroyed. An enhancement request for the specification has however been posted to get the dangling views to destroy during session destroy anyway: JSF-spec issue 905.

Kokand answered 16/6, 2011 at 12:34 Comment(10)
Ah! Is there any workaround? Is it reasonably safe to simply override finalize()? Or is this not encouraged in this environment?Hyperkinesia
The finalize() won't be executed immediately. It's up to the GC then. It may end up in accumulating too much. This is your best bet, but I won't rely on it.Kokand
Is there a way to expiry Seam 3 Faces CDI view-scoped beans on end of session? What would a non-trivial approach to such a functionality look like? I should probably raise another question for this...Pervade
@Kawu: I think so. Seam is also beyond me.Kokand
This related question stackoverflow.com/questions/30410601/… asks how one can handle "dangling" @ViewScoped beans that result from GET navigations and won't garbage collect, and includes a link to a downloadable mini NetBeans project that demonstrates the problem with an exhaustive list of JSF navigation cases and the results (impact on heap memory) for GET-style navigation using say h:link vs. POSTBACK style navigation using say h:commandButton with method expression or string action outcome.Mallet
How would it be possible for view scope beans to live without an active session -- "They will also not be destroyed when the session get destroyed"?Mythologize
@Tiny: I clarified the answer. They will be garbage collected, but the @PreDestroy won't be called, which may risk resource leaks if the @PreDestroy implementation is doing some resource cleanup.Kokand
I ran into the same problem. I have some images I am sending over to a ViewScoped Bean and when I refresh the page 3 or 4 times, I am running out of heap space (2G). Changing to RequestScoped solves this problem, but then I can not interact with the data anymore. Is there a new solution to this problem, so I can get ViewScoped working? Much appreciated!Scissile
Is this answer still 'valid'? Since stackoverflow.com/questions/6726411/…Laetitia
@Kukeltje: indeed, state of technology had changed since I posted this answer. I've closed off this Q as dupe of that Q.Kokand

© 2022 - 2024 — McMap. All rights reserved.