tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kirill Ilyukhin <>
Subject Right way to close database connection pool
Date Wed, 05 Aug 2009 16:43:41 GMT
I use database connection pool which is described in webapp's 

and in WEB-INF/web.xml:

Then I do the following in my code:
   InitialContext ctx = new InitialContext();
   DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myDS");
   Connection conn;
   try {
     conn = ds.getConnection();
   } finally {

The problem is that after I stop the webapp the connections to databae 
server are staying alive. And when I start the webapp again I see new 
connections are created.
As far as I understood from Tomcat bugs discussion this is the expected 
behaviour. If so, am I supposed to close the DataSource in my code? 
Surely I can do this in contextDestroyed(...) method of my 
ServletContextListener, but
1. DataSource has no exposed close() method and the only way to close it 
is calling close() through reflection. Looks like dirty hack...
2. The connection pool has been created somewhere inside Tomcat. I don't 
think it's correct to close it from my code.

What is the right way to close such a database connection pool? Is there 
a way to make Tomcat close the pool when the context is being destroyed?

Thanks in advance,

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message