import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.Enumeration;
+import java.util.List;
/**
* This class is loaded by the {@link WebappClassLoader} to enable it to
*/
public class JdbcLeakPrevention {
- public void clearJdbcDriverRegistrations() throws SQLException {
+ /*
+ * This driver is visible to all classloaders but is loaded by the system
+ * class loader so there is no need to unload it.
+ */
+ private static final String JDBC_ODBC_BRIDGE_DRIVER =
+ "sun.jdbc.odbc.JdbcOdbcDriver";
+
+ public List<String> clearJdbcDriverRegistrations() throws SQLException {
+ List<String> driverNames = new ArrayList<String>();
+
// Unregister any JDBC drivers loaded by the class loader that loaded
// this class - ie the webapp class loader
Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
Driver driver = drivers.nextElement();
+ if (JDBC_ODBC_BRIDGE_DRIVER.equals(
+ driver.getClass().getCanonicalName())) {
+ continue;
+ }
+ driverNames.add(driver.getClass().getCanonicalName());
DriverManager.deregisterDriver(driver);
}
-
+ return driverNames;
}
}
webappClassLoader.jdbcRemoveStreamError=Exception closing input stream during JDBC driver de-registration
webappClassLoader.stopped=Illegal access: this web application instance has been stopped already. Could not load {0}. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
webappClassLoader.readError=Resource read error: Could not load {0}.
+webappClassLoader.uncleareredReferenceJbdc=A web application registered the JBDC driver [{0}] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
webappClassLoader.wrongVersion=(unable to load class {0})
webappLoader.addRepository=Adding repository {0}
webappLoader.deploy=Deploying class repositories to work directory {0}
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
+import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.jar.Attributes;
defineClass("org.apache.catalina.loader.JdbcLeakPrevention",
classBytes, 0, offset);
Object obj = lpClass.newInstance();
- obj.getClass().getMethod(
+ @SuppressWarnings("unchecked")
+ List<String> driverNames = (List<String>) obj.getClass().getMethod(
"clearJdbcDriverRegistrations").invoke(obj);
+ for (String name : driverNames) {
+ log.error(sm.getString(
+ "webappClassLoader.uncleareredReferenceJbdc", name));
+ }
} catch (Exception e) {
// So many things to go wrong above...
log.warn(sm.getString("webappClassLoader.jdbcRemoveFailed"), e);