Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

will a bean with application scope be garbage collected

 
steve Barf
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am thinking of writing a bean to hold a quantity of database information to prevent every user of a web application having to retrieve all the data.



But I have a few concerns :
  • 1) what would stop this bean from being garbage collected ?
  • 2) what would be the best way to create the bean ?


  • something like this ? :
    [CODE}
    appBean myAppBean = (appBean) getServletContext().getAttribute("myAppBean");
    if (myAppBean==null)
    {
    appBean myAppBean = new appBean();
    myAppBean.getData();
    getServletContext().setAttribute("myAppBean");
    }
    [/CODE]
    and then reference it in jsp's by :

  • 3) should the bean be created as a singleton ?


  • Steve
     
    Ulf Dittmer
    Rancher
    Pie
    Posts: 42967
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    # 1) what would stop this bean from being garbage collected ?
    # 2) what would be the best way to create the bean ?
    # 3) should the bean be created as a singleton ?


    1) the fact that there is a live reference to it in the servlet context

    2 and 3) That code should be in the init method of a servlet that's defined to be loaded at startup time (via <load-on-startup> in web.xml). That way you don't need to get it and compare it to null - you just create it and store it.
    [ August 15, 2005: Message edited by: Ulf Dittmer ]
     
    Ben Souther
    Sheriff
    Posts: 13411
    Firefox Browser Redhat VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Steve,
    Any time you accidentally create a duplicate thread, as seems to be the case here, please delete one of them. You now have slightly different answers in two different threads.
    [ August 15, 2005: Message edited by: Ben Souther ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic