Author: markt
Date: Wed Sep 24 10:26:51 2014
New Revision: 1627270

URL: http://svn.apache.org/r1627270
Log:
Improve fix in r1626998 (itself an improvement on the fix for POOL-161)
Handle the case where the developer has explicitly set the TCCL to null during 
the construction of the pool.

Modified:
    
commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java

Modified: 
commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
URL: 
http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java?rev=1627270&r1=1627269&r2=1627270&view=diff
==============================================================================
--- 
commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
 (original)
+++ 
commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
 Wed Sep 24 10:26:51 2014
@@ -93,8 +93,8 @@ public abstract class BaseGenericObjectP
     private Evictor evictor = null; // @GuardedBy("evictionLock")
     Iterator<PooledObject<T>> evictionIterator = null; // 
@GuardedBy("evictionLock")
     /*
-     * Class loader for evictor thread to use since in a J2EE or similar
-     * environment the context class loader for the evictor thread may have
+     * Class loader for evictor thread to use since, in a JavaEE or similar
+     * environment, the context class loader for the evictor thread may not 
have
      * visibility of the correct factory. See POOL-161. Uses a weak reference 
to
      * avoid potential memory leaks if the Pool is discarded rather than 
closed.
      */
@@ -138,9 +138,14 @@ public abstract class BaseGenericObjectP
         // Populate the creation stack trace
         this.creationStackTrace = getStackTrace(new Exception());
 
-        // save the current CCL to be used later by the evictor Thread
-        factoryClassLoader = new WeakReference<ClassLoader>(
-                Thread.currentThread().getContextClassLoader());
+        // save the current TCCL (if any) to be used later by the evictor 
Thread
+        ClassLoader cl = Thread.currentThread().getContextClassLoader();
+        if (cl == null) {
+            factoryClassLoader = null;
+        } else {
+            factoryClassLoader = new WeakReference<ClassLoader>(cl);
+        }
+
         fairness = config.getFairness();
     }
 
@@ -1000,15 +1005,18 @@ public abstract class BaseGenericObjectP
             ClassLoader savedClassLoader =
                     Thread.currentThread().getContextClassLoader();
             try {
-                // Set the class loader for the factory
-                ClassLoader cl = factoryClassLoader.get();
-                if (cl == null) {
-                    // The pool has been dereferenced and the class loader 
GC'd.
-                    // Cancel this timer so the pool can be GC'd as well.
-                    cancel();
-                    return;
+                if (factoryClassLoader != null) {
+                    // Set the class loader for the factory
+                    ClassLoader cl = factoryClassLoader.get();
+                    if (cl == null) {
+                        // The pool has been dereferenced and the class loader
+                        // GC'd. Cancel this timer so the pool can be GC'd as
+                        // well.
+                        cancel();
+                        return;
+                    }
+                    Thread.currentThread().setContextClassLoader(cl);
                 }
-                Thread.currentThread().setContextClassLoader(cl);
 
                 // Evict from the pool
                 try {


Reply via email to