| Author |
will a bean with application scope be garbage collected
|
steve Barf
Greenhorn
Joined: Oct 15, 2004
Posts: 26
|
|
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 ? : and then reference it in jsp's by : 3) should the bean be created as a singleton ? Steve
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
1.) It will not be Garbage Collected (GCed) as long as there is reference to it within the JVM. Binding it to application scope will maintain this reference for the life of your app (unless you remove it). 2.) I would create this bean in a context listener so it will be ready before any servlets are initialized. 3.) A singleton would work. Your check for null would also work.
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
steve Barf
Greenhorn
Joined: Oct 15, 2004
Posts: 26
|
|
Ben, Thanks for your quick reply. I'd be grateful for your comments on my understanding of your points. 1) a) Could there be a problem that the bean is GCed between creation by the servlet and given application scope in the jsp ? b) Does the application scope implicitly generate a reference which will therefore prevent GC ? 2) Could you expand on this please or point me to a simple reference.3) Maybe I could do both. I would like to have a go at a singleton, does the following look suitable : There is another singleton method I've seen but I can't see how the getInstance() method would fit in with serrvlets/jsps Steve
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
1-a) No 1-b) Yes. As long as it's in application scope, there is a reference maintained and the object won't be released for GC. 2) I learned servlet programming (initially) with this book. http://pdf.coreservlets.com 3) You could do both. Either way, if you instanciate the object in a context listener and bind it to application (context) scope, you won't need to instanciate it from a JSP. It will already be there before your JSPs are ever run. This will be especially helpful if you are using EL.
|
 |
steve Barf
Greenhorn
Joined: Oct 15, 2004
Posts: 26
|
|
Ben, Thanks, that book seems better than others I have read on servlets/jsps. But I didn't see anything on 'context listeners' - if this is not the same as the ServletContext then this is new to me. Searches on google have resulted in articles relating to EJB's or products that use it, rather than using it in the raw. Could you supply some more guidance please ? My other option is to use the ServletContext as first mentioned : but to do it in a servlet that runs on server startup. Steve
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
Context listeners are newer than that book. The 2nd edtion "More Servlets and Java Server Pages" covers them. http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContextListener.html Try googling for a tutorial that covers them. http://www.google.com/search?hl=en&q=SERVLET+TUTORIAL+ServletContextListener&btnG=Google+Search
|
 |
 |
|
|
subject: will a bean with application scope be garbage collected
|
|
|