5

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?)

Ben
  • 10,020
  • 21
  • 94
  • 157

1 Answers1

5

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:

  • Why do you need such functionality ? Because of memory ?

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 !

Fallup
  • 2,417
  • 19
  • 30
  • 2
    A session bean is created when it is requested the first time and not automatically when the session is created. – Matt Handy Apr 15 '12 at 15:27
  • @Matt Handy yes sorry, don't know what I was thinking about when writing that, such a mistake ... Thumbs up for pointing out. Is it fine now ? – Fallup Apr 15 '12 at 15:31
  • No problem. I would not use the session scope here. – Matt Handy Apr 15 '12 at 15:41
  • Thanks @Fallup, But If I only use AJAX navigation than there's no real difference between View and Session Scopes.. Right? – Ben Apr 16 '12 at 07:45
  • @Ben If you are considering only switching between tabs then no. But if you leave the `view` where are your tabs, then `View scoped` get destroyed but `Session scoped` will still persist. – Fallup Apr 16 '12 at 12:05
  • @MattHandy I know why session scope is not good for me (took me time, I know :-) ) When I use Server State Saving Method, The Server has a state queue of about ~20 sessions. Say I have 21 tabs and I go sequentially over all of them and then return to the first 1, I will receive the view expired exception. This is not such a far fetched example. If you have 3 tabs, iterate of 2 them 20 times and then go back to the third one, you will receive the same exception. Being able to clean the session on tab change will avoid this problem. – Ben Jun 18 '12 at 13:48