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
Joined: Dec 05, 2001
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
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>