Java tutorial
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is Data Dictionary * * The Initial Owner of the Original Code is European Environment * Agency. Portions created by TripleDev are Copyright * (C) European Environment Agency. All Rights Reserved. * * Contributor(s): * TripleDev */ package eionet.meta; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Enumeration; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.mysql.jdbc.AbandonedConnectionCleanupThread; /** * Class to close database connections to prevent memory leaks. * * @author enver */ public class ContainerContextClosedHandler implements ServletContextListener { /** Logger instance. */ private static final Log LOGGER = LogFactory.getLog(ContainerContextClosedHandler.class); @Override public void contextInitialized(ServletContextEvent servletContextEvent) { // nothing to do } /** * Unregisters mysql connections. * * @param servletContextEvent * servlet context */ @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { Enumeration<Driver> drivers = DriverManager.getDrivers(); Driver driver = null; // clear drivers while (drivers.hasMoreElements()) { try { driver = drivers.nextElement(); DriverManager.deregisterDriver(driver); } catch (SQLException ex) { // deregistration failed LOGGER.warn(ex.getMessage()); } } // MySQL driver leaves around a thread. This static method cleans it up. try { // throw new InterruptedException(); AbandonedConnectionCleanupThread.shutdown(); } catch (InterruptedException ex) { // again failure LOGGER.warn(ex.getMessage()); } } // end of method contextDestroyed } // end of class ContainerContextClosedHandler