wood burning stoves 2.0*
The moose likes Servlets and the fly likes Calling a Destroy method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling a Destroy method" Watch "Calling a Destroy method" New topic
Author

Calling a Destroy method

Sam Tilley
Ranch Hand

Joined: Dec 05, 2001
Posts: 160
Hi,
I have created a web application that uses servlets to access a database access servlet which creates a connection, logs onto a database, formats and then puts the data back into another servlet for output.
However i am trying to make sure that when the browser is closed the database connection is closed, now i thought i would do this in my destroy() method but for some reason i don't seem to be accessing it. I am using Forte. If i put a System.out.println statement in the destroy method then surely when i close the browser it would print the line to my Output Window in Forte??? Then any connection.close() statement i put in the destroy() method should close my connection ok?? Or is there a better way
Any help would be good as i have developed this big program for work but i seem to be leaving database connections open all over the place even when the reports are not being used
Thanks
Sam


Sam Tilley SCJP, SCWCD
Sam Tilley
Ranch Hand

Joined: Dec 05, 2001
Posts: 160
Hi, Okay i have done a bit of research as i should of in the first place and answered some of my own questions, the destroy() method seems to be called when the server closes down, but as only the servlet will close down not the server i am not closing down my connection.
I don't want to close and open the connection down each time someone uses it, only when they have finished with it but im not sure about the best way.
Thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
You really should investigate connection pools. There are a number of implementations and they remove all those worries.
Bill
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
in addition, a servlet is not destroyed when the browser window is closed - you application is completely unaware of it. HTTP does not have any concept of sessions built into it. In addition, a servlet is reused many times- it is a very long lived bit of code that is often loaded into memory and then not unloaded unless the server is restarted therefore the connection will be hanging around until it goes stale or the database reclaims it.


chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a Destroy method