You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ps...@apache.org on 2013/11/25 18:30:52 UTC

svn commit: r1545347 - in /commons/proper/pool/trunk/src: main/java/org/apache/commons/pool2/impl/GenericObjectPool.java test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java

Author: psteitz
Date: Mon Nov 25 17:30:51 2013
New Revision: 1545347

URL: http://svn.apache.org/r1545347
Log:
Implement the fix for POOL-240 in GOP suggested by markt.
JIRA: POOL-240

Modified:
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java?rev=1545347&r1=1545346&r2=1545347&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java (original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java Mon Nov 25 17:30:51 2013
@@ -561,6 +561,11 @@ public class GenericObjectPool<T> extend
                     swallowException(e);
                 }
                 updateStatsReturn(activeTime);
+                try {
+                    ensureIdle(1, false);
+                } catch (Exception e) {
+                    swallowException(e);
+                }
                 return;
             }
         }
@@ -574,6 +579,11 @@ public class GenericObjectPool<T> extend
             } catch (Exception e) {
                 swallowException(e);
             }
+            try {
+                ensureIdle(1, false);
+            } catch (Exception e) {
+                swallowException(e);
+            }
             updateStatsReturn(activeTime);
             return;
         }
@@ -626,6 +636,7 @@ public class GenericObjectPool<T> extend
                 destroy(p);
             }
         }
+        ensureIdle(1, false);
     }
 
     /**
@@ -857,12 +868,27 @@ public class GenericObjectPool<T> extend
 
     @Override
     void ensureMinIdle() throws Exception {
-        int minIdleSave = getMinIdle();
-        if (minIdleSave < 1) {
+        ensureIdle(getMinIdle(), true);
+    }
+    
+    /**
+     * Tries to ensure that {@code idleCount} idle instances exist in the pool.
+     * <p>
+     * Creates and adds idle instances until either {@link #getNumIdle()} reaches {@code idleCount}
+     * or the total number of objects (idle, checked out, or being created) reaches
+     * {@link #getMaxTotal()}. If {@code always} is false, no instances are created unless
+     * there are threads waiting to check out instances from the pool.
+     * 
+     * @param idleCount the number of idle instances desired
+     * @param always true means create instances even if the pool has no threads waiting
+     * @throws Exception if the factory's makeObject throws
+     */
+    private void ensureIdle(int idleCount, boolean always) throws Exception {
+        if (idleCount < 1 || isClosed() || (!always && !idleObjects.hasTakeWaiters())) {
             return;
         }
 
-        while (idleObjects.size() < minIdleSave) {
+        while (idleObjects.size() < idleCount) {
             PooledObject<T> p = create();
             if (p == null) {
                 // Can't create objects, no reason to think another call to

Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java?rev=1545347&r1=1545346&r2=1545347&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java (original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java Mon Nov 25 17:30:51 2013
@@ -1941,7 +1941,7 @@ public class TestGenericObjectPool exten
      * 
      * JIRA: POOL-240
      */
-    //@Test
+    @Test
     public void testInvalidateFreesCapacity()
         throws Exception {
         SimpleFactory factory = new SimpleFactory();
@@ -1971,7 +1971,7 @@ public class TestGenericObjectPool exten
      * 
      * JIRA: POOL-240
      */
-    //@Test
+    @Test
     public void testValidationFailureOnReturnFreesCapacity()
         throws Exception {
         SimpleFactory factory = new SimpleFactory();