Improved cleanup of database resources when web application is unloaded

git-svn-id: http://scm.dspace.org/svn/repo/branches/dspace-1_5_x@2926 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Graham Triggs
2008-05-07 16:15:51 +00:00
parent ba898acd29
commit b3f63045a8
4 changed files with 118 additions and 8 deletions

View File

@@ -0,0 +1,99 @@
/*
* DSpaceContextListener.java
*
* Version: $Revision: 1.3 $
*
* Date: $Date: 2006/07/05 21:39:29 $
*
* Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
* Institute of Technology. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the Hewlett-Packard Company nor the name of the
* Massachusetts Institute of Technology nor the names of their
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
package org.dspace.app.util;
import org.dspace.storage.rdbms.DatabaseManager;
import org.apache.log4j.Logger;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
import java.beans.Introspector;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Enumeration;
/**
* Class to initialise / cleanup resources used by DSpace when the web application
* is started or stopped
*/
public class DSpaceContextListener implements ServletContextListener
{
private static Logger log = Logger.getLogger(DSpaceContextListener.class);
/**
* Initialise any resources required by the application
* @param event
*/
public void contextInitialized(ServletContextEvent event)
{
}
/**
* Clean up resources used by the application when stopped
*
* @param event
*/
public void contextDestroyed(ServletContextEvent event)
{
try
{
// Remove the database pool
DatabaseManager.shutdown();
// Clean out the introspector
Introspector.flushCaches();
// Remove any drivers registered by this classloader
for (Enumeration e = DriverManager.getDrivers(); e.hasMoreElements();)
{
Driver driver = (Driver) e.nextElement();
if (driver.getClass().getClassLoader() == getClass().getClassLoader())
{
DriverManager.deregisterDriver(driver);
}
}
}
catch (Throwable e)
{
log.error("Failled to cleanup ClassLoader for webapp", e);
}
}
}

View File

@@ -1429,10 +1429,8 @@ public class DatabaseManager
try
{
// Register basic JDBC driver
Class driverClass = Class.forName(ConfigurationManager
.getProperty("db.driver"));
Driver basicDriver = (Driver) driverClass.newInstance();
DriverManager.registerDriver(basicDriver);
Class.forName(ConfigurationManager.getProperty("db.driver"));
Class.forName("org.apache.commons.dbcp.PoolingDriver");
// Read pool configuration parameter or use defaults
// Note we check to see if property is null; getIntProperty returns
@@ -1459,7 +1457,7 @@ public class DatabaseManager
{
maxIdle = -1;
}
boolean useStatementPool = ConfigurationManager.getBooleanProperty("db.statementpool",true);
// Create object pool
@@ -1529,14 +1527,15 @@ public class DatabaseManager
}
//
// Finally, we create the PoolingDriver itself...
// Finally, we get the PoolingDriver itself...
//
PoolingDriver driver = new PoolingDriver();
PoolingDriver driver = (PoolingDriver)DriverManager.getDriver("jdbc:apache:commons:dbcp:");
//
// ...and register our pool with it.
//
driver.registerPool(poolName, connectionPool);
if (driver != null)
driver.registerPool(poolName, connectionPool);
// Old SimplePool init
//DriverManager.registerDriver(new SimplePool());