"Shun, Vadim" wrote:

> Hi,
>
> I am storing some resources used on global level, such as database
> connection broker, in ServletContext.
> I am not sure however how to destroy them.
>
> The idea is that if this is the first time call, Controller Servlet init
> function initializes global resources and puts them in servlet context, so
> that all subsequent init functions reuse those resources. However I have a
> problem on how to destroying these global resources.
>
> Currently, my ActionServlet code has:
>
>     public void destroy ()
>     {
>                 // !!! actually should not do it here, should put in servlet
> context destroy
>                 DbConnectionBroker dbConnBroker = (DbConnectionBroker )
> getServletContext().getAttribute ("dbConnBroker");
>             if ( dbConnBroker != null )
>                 dbConnBroker.destroy (); // destroy connection pool
>         super.destroy();
>     }
>
> Servlet initialization funciton has the following fragment of code:
>     // !!!init DB pool, put it in servlet context
>         DbConnectionBroker dbConnBroker = (DbConnectionBroker )
> getServletContext().getAttribute ("dbConnBroker");
>     if ( dbConnBroker == null ) {
>                 // create one here
>                 dbConnBroker = new
> DbConnectionBroker(dbDriver,dbServer,dbLogin,dbPassword,
>  minConns,maxConns,logFile, maxConnTime);
>
>                 getServletContext().setAttribute ("dbConnBroker",
> dbConnBroker);
>         }
>
> The problem is that from looking at this code I can see that every time
> servlet is destroyed, it will unload global resources from servlet context,
> thus rendering the whole servletContext approach irrelevant (and dangerous I
> would say if there are other servlets reusing those resources). Of course,
> every servlet instance during init will have to recreate it again.
>
> I cannot avoid thinking of C++ where templates allowed smart reference
> counting of classes, thus I would be able to see if global resources are
> still in use (like auto_ptr STL template class and similar). However there
> must be some easier approach in Java for this particular problem (maybe some
> API call for servletcontext).
>

With the servlet 2.2 API, this is about the best you can do.  Under most servlet
containers, your controller servlet will not get unloaded (and therefore destroy()
will not get called) until you shut down the server, so the resources you make
available -- a connection pool in your case -- will stay available throughout the
life of your application.

With the next version of the servlet spec, a better method to deal with
application events (like "startup" and "shutdown") is being looked at.

>
> Vadim Shun
> NEW Corp
> Dulles, VA
>

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to