tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Edoardo Panfili <edoa...@aspix.it>
Subject Re: How to close open connections after application stop?
Date Sun, 22 Feb 2009 18:46:20 GMT
Alan Chaney ha scritto:
> I don't think so. Let me recap your problem:
> 
> When you undeploy an application from tomcat (using the DBCP pooling 
> mechanism) you can't make STRUCTURAL changes to the database because it 
> complains that connections are still in use.
> 
> This is exactly what one would expect. I've encountered the same 
> problem. When an application finishes with a database connection it is 
> returned to the pool. That's exactly what a connection pool is for!
> 
> As far as I can see by looking at the tomcat source code the connection 
> pool is created at startup and remains active until TC shutdown. Once a 
> connection has been obtained from the pool it may stay 'active' for the 
> entire duration of the TC session (that is, from TC start to TC stop)
> 
> Obviously, depending upon your usage, it is possible for more than one 
> application in the same container to be reusing the same connection 
> pool. Your original post indicates that only one app. is using the 
> database.
> 
> It seems to me that:
> .....

> 3. As I assume you are using DBCP in Tomcat, carefully read the DBCP 
> docs, configure your system so that you can directly access the POOLED 
> connections, keep a list of ALL the connections you use and then shut 
> them down at the end. This is fraught with difficulty.

Using this code in destroy() method of a servlet marked as 
<load-on-startup>1</load-on-startup>
seems work
-------------------
try {
   Context ambiente = (Context) new
                      InitialContext().lookup("java:comp/env");
   DataSource pool = (DataSource) ambiente.lookup("jdbc/anArchive");
   if(pool instanceof org.apache.tomcat.dbcp.dbcp.BasicDataSource){
     org.apache.tomcat.dbcp.dbcp.BasicDataSource source =
	 (org.apache.tomcat.dbcp.dbcp.BasicDataSource) pool;
     System.out.println("closing source...");
     source.close();
     System.out.println("closed.");
     // System.out.println("maxIdle:"+source.getMaxIdle());
     // source.setMaxIdle(0);
     // System.out.println("maxIdle:"+source.getMaxIdle());
   }
} catch (Exception e) {
   e.printStackTrace();
}
-------------------

"source.setMaxIdle(0);" is not necessary.
also accessToUnderlyingConnectionAllowed="true" in context is not necessary.


Edoardo


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message