tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Shifman <mark.shif...@yale.edu>
Subject Re: Right way to close database connection pool
Date Wed, 05 Aug 2009 17:10:20 GMT
I do it in a contextListener.
something like this:
public void contextDestroyed(ServletContextEvent event) {
			Context initContext = new InitialContext();
			Context envContext = (Context) initContext.lookup("java:/comp/env");
			((BasicDataSource) envContext.lookup("jdbc/myDS")).close();
}

Kirill Ilyukhin wrote:
> I use database connection pool which is described in webapp's
> META-INF/context.html:
> <Context>
>   <Resource
>     auth="Container"
>     scope="Unshareable"
>     name="jdbc/myDS"
>     type="javax.sql.DataSource"
>     driverClassName="org.postgresql.Driver"
>     url="jdbc:postgresql://server/db"
>     ...
>     />
> </Context>
> 
> and in WEB-INF/web.xml:
> <resource-ref>
>   <res-ref-name>jdbc/myDS</res-ref-name>
>   <res-type>javax.sql.DataSource</res-type>
>   <res-auth>Container</res-auth>
> </resource-ref>
> 
> 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 {
>     conn.close();
>   }
> 
> 
> 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,
> Kirill.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

-- 
 Mark Shifman MD. Ph.D.
 Yale Center for Medical Informatics
 Phone (203)737-5219
 mark.shifman@yale.edu

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


Mime
View raw message