问题
From what I understand, ViewScoped beans only get destroyed when one of the following take place:
1) JSF sends a POST request to another page with something like a <h:commandLink...>
2) The number of open beans exceeds the maximum threshold setting (default of 15)
3) The user's session expires
Here is my confusion:
Does #1 mean that if a user navigates away from the page with a GET request, the bean will stay open, even if eventually a JSF POST happens in the same browser tab on another page? Or will all active @ViewScoped instances for that browser tab be destroyed once a JSF POST is sent regardless of which page the user is on?
Does #2 mean that a user can have 15 bean instances active for each @ViewScoped class? Or is it 15 bean instances regardless of class -- meaning I could have 5 instances of Class1, 5 instances of Class2, and 5 instances of Class3, and a new bean would destroy the oldest active bean?
For #3, if STATE_SAVING_METHOD is set to "client", will that have any implications in ViewScoped beans being destroyed? From what I remember, there needs to be a way to manually control session expiration if STATE_SAVING_METHOD is set to client.
Finally, is there a way to manage active ViewScoped beans so that they can be destroyed when a user clicks "logout" for instance?
回答1:
I figured out the answers to these by adding a @PreDestroy method to each @ViewScoped bean, and logging when it gets destroyed. For others who might be curious about this:
For #1, a bean does not get destroyed if you navigate away from the page with a GET request, but then send a post request at a later time. That bean will stay in memory until the "maximum active view scopes" setting has been reached and it's the bean's turn to be destroyed, or the session has been invalidated.
For #2, class doesn't matter. You can have 5 instances of Class1, 5 instances of Class2, and 5 instances of Class3, and a new ViewScoped bean instance will destroy the oldest bean, given your threshold is 15.
For #3, it looks like the beans are destroyed once the session has been invalidated even if STATE_SAVING_METHOD is set to client.
来源:https://stackoverflow.com/questions/21236730/memory-implications-of-omnifaces-viewscoped-bean