Author: markt Date: Fri Oct 1 11:10:51 2010 New Revision: 1003481 URL: http://svn.apache.org/viewvc?rev=1003481&view=rev Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49667 Ensure JDBC leak prevention code doesn't trigger the very memory leak it is meant to be fixing.
Modified: tomcat/trunk/java/org/apache/catalina/loader/JdbcLeakPrevention.java tomcat/trunk/webapps/docs/changelog.xml Modified: tomcat/trunk/java/org/apache/catalina/loader/JdbcLeakPrevention.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/loader/JdbcLeakPrevention.java?rev=1003481&r1=1003480&r2=1003481&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/loader/JdbcLeakPrevention.java (original) +++ tomcat/trunk/java/org/apache/catalina/loader/JdbcLeakPrevention.java Fri Oct 1 11:10:51 2010 @@ -23,6 +23,7 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.Enumeration; +import java.util.HashSet; import java.util.List; /** @@ -41,16 +42,33 @@ public class JdbcLeakPrevention { public List<String> clearJdbcDriverRegistrations() throws SQLException { List<String> driverNames = new ArrayList<String>(); - // This will list all drivers visible to this class loader + /* + * DriverManager.getDrivers() has a nasty side-effect of registering + * drivers that are visible to this class loader but haven't yet been + * loaded. Therefore, the first call to this method a) gets the list + * of originally loaded drivers and b) triggers the unwanted + * side-effect. The second call gets the complete list of drivers + * ensuring that both original drivers and any loaded as a result of the + * side-effects are all de-registered. + */ + HashSet<Driver> originalDrivers = new HashSet<Driver>(); Enumeration<Driver> drivers = DriverManager.getDrivers(); while (drivers.hasMoreElements()) { + originalDrivers.add(drivers.nextElement()); + } + drivers = DriverManager.getDrivers(); + while (drivers.hasMoreElements()) { Driver driver = drivers.nextElement(); // Only unload the drivers this web app loaded if (driver.getClass().getClassLoader() != this.getClass().getClassLoader()) { continue; } - driverNames.add(driver.getClass().getCanonicalName()); + // Only report drivers that were originally registered. Skip any + // that were registered as a side-effect of this code. + if (originalDrivers.contains(driver)) { + driverNames.add(driver.getClass().getCanonicalName()); + } DriverManager.deregisterDriver(driver); } return driverNames; Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1003481&r1=1003480&r2=1003481&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Fri Oct 1 11:10:51 2010 @@ -66,6 +66,11 @@ (markt) </fix> <fix> + <bug>49667</bug>: Ensure that using the JDBC driver memory leak + prevention code does not cause a one of the memory leaks it is meant to + avoid. (markt) + </fix> + <fix> <bug>49670</bug>: Restore SSO functionality that was broken by Lifecycle refactoring. (markt) </fix> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org