Re: using finalize() to persist objects

From: John C. Bollinger (jobollin_at_indiana.edu)
Date: 02/07/05


Date: Mon, 07 Feb 2005 11:33:35 -0500

GregSmith wrote:

> The Java Servler Whitepaper
> (http://java.sun.com/products/servlet/whitepaper.html) Says...
>
> "<i>Requests are processed until the servlet is explicitly shut down by
> the web server, by calling the destroy method. The servlet's class may
> then become eligible for garbage collection. </i>"
>
> So, you should be able to serialize in the destroy() method.

No, he can't. The servlet container is free at any time to destroy() a
servlet instance and replace it with a new instance of the same servlet
class. It could even delay creating the new instance until a request is
received that maps to that servlet. In the case of a servlet that
implements SingleThreadModel, the container is even free to maintain
multiple instances concurrently, in which case it would be completely
incorrect to serialize in the servlet's destroy() method.

The servlet API defines a means for a web application to receive
notification when it is being shut down (the ServletContextListener; see
my other response). If shutdown notification is what is desired then
that API should be used, not some hack that may (or may not) be found to
work under the initial development conditions.

-- 
John Bollinger
jobollin@indiana.edu


Relevant Pages

  • Initializing Servlets
    ... servlet's init() method can be expected? ... I want to open a file that is available for any servlet thread to ... initand destroy() methods? ...
    (comp.lang.java.programmer)
  • Re: Sun one WS : clean shutdown
    ... I had a hunch I could use it. ... But how do I know which servlet is 'destroyed' last? ... I can specify in my web.xml which one gets loaded first, ... > I believe that the servlet destroy() method is what you want. ...
    (comp.lang.java.programmer)
  • Re: Sun one WS : clean shutdown
    ... The servlet engine is allowed to discard/destroy servlets arbitrarily ... can never do that if there is an active request. ... >> I believe that the servlet destroy() method is what you want. ...
    (comp.lang.java.programmer)
  • Re: ending a session in servlet
    ... i have a problem with my servlet.i had bound an rmi object with a ... servlet page.The thing i need is i have to invoke the call to an method ... destroy() method of servlet but it is invoked only when the server is ... You can use a session listener for that ...
    (comp.lang.java.programmer)
  • Clever serialization broken by CORBA RMI
    ... from an InputStream until such time that I wanted to serialize the ... The basic setup is that clients will POST data to a servlet of mine ... data into memory as a bytemember of the Request object and then let ... not read from it until the RMI layer starts to serialize it. ...
    (comp.lang.java.programmer)