aspose file tools*
The moose likes Servlets and the fly likes Re: Closing database connection in destroy metod()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Re: Closing database connection in destroy metod()?" Watch "Re: Closing database connection in destroy metod()?" New topic
Author

Re: Closing database connection in destroy metod()?

Ashwin Tadepalli
Greenhorn

Joined: Feb 01, 2001
Posts: 27
Hi,
I have a question about the destroy function in servlets. Should I call this at the end of each servlet, in order to sever the connection to the database, as I re-establish it in every servlet? or should I call this after closing my statement object?
//Destroy method
public void destroy()
{
try {
connection.close();
}
catch( Exception e ) {
System.err.println( "Problem closing the database" );
}
}
Cynthia Yao
Ranch Hand

Joined: Nov 06, 2000
Posts: 92
Based on the definition of the destroy(), This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. You should see servlet destroy often when you shut down the web server. Therefore, closing database connection only in destroy method is inappropriate.
Why don't you close the connection whenever you finish process a query or create a connection pool.
Ashwin Tadepalli
Greenhorn

Joined: Feb 01, 2001
Posts: 27
Hi Cynthia,
Thanks for the reply.I am trying to use connection pooling as an alternative but what is wrong if I simply close my connection in the destroy method.Okay it will only destroy my servlet when the
application server is down and until then it will use many resources and keep them alive,but is there any other way to acheive this other than make use of connection pooling.
Ashwin
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Connection Pooling is nothing but making n number of ready-made connections before hand and offer one by one to application. Since opening and closing of a database connection will take some time, it is not advisable to freshly open and immediately closing the connection for every db operation. This is main reason we create a connection pool in init(..) method of one of your servlets and other servlets just make use of this pool. You close all connecions in connection pool in destroy() of all servlets.
On the other hand you can simply open a connection initially , do db operations , keep it alive all the time and close it in destroy method. But if your load is high to your servlets, this one db connection can't serve all those operations. Where are you planning to declare your dbConnecion var. Instance /local var? If it were to be static or instance type then all requests will get access to this var and it can't withstand the load. If it would have been a local var, then creating and closing for each request will produce performance bottlenecks.
Connection pool is just an extended, elegant way of keeping/serving/returning/closing a pool( n number) of already opened, ready-made db connections.
I use Marty Hall's Connection Pool. You can get it from his web site.
regds
maha anna
Raghavendra Holla
Ranch Hand

Joined: Jun 02, 2000
Posts: 58
Hi,
I completely agree with maha anna. First in your problem we don't when you open connection. In init or where? And I presume that your connection object is a private member of your class. If connection object is not a static one then you may not be sure, whether all the servlet objects use same connection or not. Assuming your connection object is static private member of servlet class, you have critical section problem. (well the name critical section is, more than one process trying to acquire same resource). You don't know whether your servlet gets the connection object or not. If your servlet implements single thread interface, then you may not come across these problem.
To solve all these only you have connection pooling. If you don't want to use more than one connection in your application, then set pool size as 1. The connection pool object takes care of rest.
Regards,
Holla.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Re: Closing database connection in destroy metod()?