Author: markt Date: Mon Dec 3 11:45:47 2012 New Revision: 1416458 URL: http://svn.apache.org/viewvc?rev=1416458&view=rev Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=54217 Prevent PoolCleaner instances retaining references to the ConnectionPool which may result in a memory leak.
Modified: tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java Modified: tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java URL: http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java?rev=1416458&r1=1416457&r2=1416458&view=diff ============================================================================== --- tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java (original) +++ tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java Mon Dec 3 11:45:47 2012 @@ -16,6 +16,7 @@ */ package org.apache.tomcat.jdbc.pool; +import java.lang.ref.WeakReference; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; @@ -1315,14 +1316,13 @@ public class ConnectionPool { return poolCleanTimer; } - protected class PoolCleaner extends TimerTask { - protected ConnectionPool pool; + protected static class PoolCleaner extends TimerTask { + protected WeakReference<ConnectionPool> pool; protected long sleepTime; - protected volatile boolean run = true; protected volatile long lastRun = 0; PoolCleaner(ConnectionPool pool, long sleepTime) { - this.pool = pool; + this.pool = new WeakReference<>(pool); this.sleepTime = sleepTime; if (sleepTime <= 0) { log.warn("Database connection pool evicter thread interval is set to 0, defaulting to 30 seconds"); @@ -1334,11 +1334,11 @@ public class ConnectionPool { @Override public void run() { - if (pool.isClosed()) { - if (pool.getSize() <= 0) { - run = false; - } - } else if ((System.currentTimeMillis() - lastRun) > sleepTime) { + ConnectionPool pool = this.pool.get(); + if (pool == null) { + stopRunning(); + } else if (!pool.isClosed() && + (System.currentTimeMillis() - lastRun) > sleepTime) { lastRun = System.currentTimeMillis(); try { if (pool.getPoolProperties().isRemoveAbandoned()) @@ -1350,9 +1350,9 @@ public class ConnectionPool { pool.testAllIdle(); } catch (Exception x) { log.error("", x); - } // catch - } // end if - } // run + } + } + } public void start() { registerCleaner(this); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org