• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How to destroy the Object of a SessionScoped bean

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have been building an application that requires me to Keep exactly one session scoped bean. But I also have another war file to be navigated to
at the end of this application. I want to destroy the Session Scoped Bean's object at the time of navigation to the second war file(i.e. clear up the memory allocated to the session scoped object ).

How can I do this ???

Would System.gc() help???

I have doubts because System.gc() doesn't guarantee garbage collection upon calling the method!!!
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't need the session you could do session.invalidate() which also removes all objects from a session

I you only want the remove the attribute you could use the removeAttribute method of the session


HttpSession
 
Dushyant Agarwal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for the reply!
But I have got a managed bean whose managed bean scope is session.
Whtaever you have suggested would have been appropriate if I had been using

HttpSession ref=(HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession();

ref.setAttribute("Key1",Value1);

However, I am declaring a variable "var" in the class (let us say )SessionBean1. Beecause of session scope the value of var will persist across the session. That will serve the same purpose as

HttpSession ref=(HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession();

ref.setAttribute("Key1",Value1);

However I am wanting to know how could I be able to destroy the Object of SessionBean1, to free the allocated memory.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try:
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().getSessionMap().remove("#{vtBbBean}");
 
Dushyant Agarwal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Thanks for the reply!

However it is not working.

See after I got your reply, I tried the following:
I built a class SessionBean1 as following:

public class Sessionbean1{
private str="testing for session destroy";

//gettter and setter of str
}

In the faces-Config I had set the scope of the above class
as session.

Now in the backing bean of a page Page1.jsp that conatins
a h:commandButton I placed he following code in the action method of the
commandbutton

public String button1Action(){
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("SessionBean1");
System.out.println(<obj>.getStr()); //here obj retreives the
//session scoped object of
//SesionBean1 class.
}


Probably this code should have given a nullPointer Exception because

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("SessionBean1");

should have destroyed the object of SessionBean1.But it did not.

My problem still stands .Please help!!
[ January 07, 2008: Message edited by: Dushyant Agarwal ]
 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

However it is not working.



Did you try it with "#{SessionBean1}"?

context.getExternalContext().getSessionMap().remove("#{SessionBean1}");


[ January 08, 2008: Message edited by: Richard Green ]
 
Dushyant Agarwal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for reply .
I finally got to get it working with "SessionBean1".
But I wanted to know whether after we execute

context.getExternalContext().getSessionMap().remove("SessionBean1");

if we try to access the reference of Session scoped bean then should we get null as a result or a new reference of the same bean. I am getting a new reference of the Session scoped bean in that case.
 
reply
    Bookmark Topic Watch Topic
  • New Topic