How can I destroy a session-scoped bean?
The purpose of this would be to control the lifetime of the bean so it only lives when a tab in the web application is active. (Using Ajax Based Tab Navigation in the webapp)
Is there a better way to do that? (Custom Scoped Beans?)
Session scoped bean is created on first HttpRequest
involving this bean. Destroyed when session
is invalidated. You can also destroy it manually by removing it from the HttpSession
, or sessionMap
(get through FacesContext
).
The fact is that it's illogical to create Session scoped bean which will live only during the tab view. For this exact purpose defining Custom scoped bean will be better, but think about it first:
Well it could be pretty tricky, imagine user just switching between the tabs pretty often and you are recreating the bean over and over. Even worse what if you fetch data from DB in constructor
or @PostConstruct
. It won't be really efficient.
My opinion is that you should forget about it (unless you need it for some other purposes) and pick a View scoped bean. This bean gets initialized after accessing the view and lives till you don't switch to another view - so no DB fetching on each tab switch (still same view). IMO it's better to fetch huge data once than eg. 15 times... If you don't fetch any data, then go definitely for View scoped. But that's just my opinion as I said.
If you can't afford View scoped, go for Custom scoped bean, but definitely not Session scoped.
Further reading for you : BalusC on JSF 2.0
Hope it helped !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With