On Friday 07 August 2009 10:53, Christopher Schultz wrote:

> Recently, there was a post on the list regarding connection pool leakage
> from Tomcat (ref:
> http://www.nabble.com/Right-way-to-close-database-connection-pool-td24832
>197.html).

This is the code I picked up from http://wiki.apache.org/tomcat/OutOfMemory a 
while ago. 
Invoke the method from your ContextListener.contextDestroyed(). Isn't it enough?

    /**
     * Deregister hanging JDBC drivers.
     * 
     * @param servletContext
     */
    private void unloadDrivers(ServletContext servletContext)
    {
        Enumeration<Driver> drivers = DriverManager.getDrivers();
        ArrayList<Driver> driversToUnload = new ArrayList<Driver>();
        while (drivers.hasMoreElements())
        {
            Driver driver = drivers.nextElement();
            if (driver != null &&
                    driver.getClass().getClassLoader() != null &&
                    driver.getClass().getClassLoader().equals(
                    getClass().getClassLoader()))
            {
                driversToUnload.add(driver);
            }
        }
        for (Driver driver : driversToUnload)
        {
            try
            {
                DriverManager.deregisterDriver(driver);
                servletContext.log("Deregistered driver: " + driver);
            }
            catch (SQLException ex)
            {
                servletContext.log("Unable to deregister driver: " + driver, 
ex);
            }
        }

-- 
Nicholas Sushkin, Senior Software Engineer
http://www.aggex.com http://www.wealthinformationexchange.com

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to