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-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
> 

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

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

Reply via email to