File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes when is the destroy method called? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "when is the destroy method called?" Watch "when is the destroy method called?" New topic
Author

when is the destroy method called?

raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
i have a question regarding the destroy method of a servlet, for example a have a simple application which uses jsp as the view and servlet as a controller and this is a database driven...if closing my database connection in the destroy method of a servlet, so if the first jsp submitted the request to the servlet and the servlet makes the connection and query some data and after that forward that request (data) to the second jsp, does the destroy method ever called?
thanks...
raymond
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
Raymond
If I understand your question correctly, you're asking whether the destroy() method is called when you forward your request to another resource?
The destroy() method is only called once on a servlet instance, and when the container decides it no longer needs the instance to service requests, and only if the instance has no more request threads to service.
As for destroy(), the init() method is only called once in the life-cycle of a servlet instance. So... The init() method is a good place to do your database connection work, and the destoy() method is a good place to shut it down again.
raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
so u mean the container is the one who decides to call the destroy method? what if i closed the browser does the servlet destroy method called?
thanks...

raymond
Hari babu
Ranch Hand

Joined: Jun 25, 2001
Posts: 208
Hi,
When you close your browser window the destroy method doesnt get called. You just close your session with the server (vice versa is not true)
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

The only time destroy() is called is immediately before the object (the instance of the servlet) is removed from memory. It's like any other classes finalize() method.

The container decides when to remove servlets from service. Just because the servlet has finished processing your request, doesn't mean it won't be kept around to service another request by someone else. In fact most (all?) containers do exactly this. Even with SingleThreadModel servlets.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when is the destroy method called?
 
Similar Threads
when and how all methods of servlet are called
SQL Exception on refeshing the servlet.
Re: Closing database connection in destroy metod()?
Question related to Servlet Life cycle
Why close the connection, statement and resultset?