tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicholas Sushkin <nsush...@openfinance.com>
Subject Re: JNDI DataSource Leaks
Date Fri, 07 Aug 2009 20:39:51 GMT
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

Mime
View raw message