commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1569016 - in /commons/proper/pool/trunk/src: changes/ main/java/org/apache/commons/pool2/impl/ test/java/org/apache/commons/pool2/impl/
Date Mon, 17 Feb 2014 15:11:40 GMT
Author: markt
Date: Mon Feb 17 15:11:40 2014
New Revision: 1569016

URL: http://svn.apache.org/r1569016
Log:
Add an option inspired by DBCP-249 to validate an object when it is created

Modified:
    commons/proper/pool/trunk/src/changes/changes.xml
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMXBean.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMXBean.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java

Modified: commons/proper/pool/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/changes/changes.xml?rev=1569016&r1=1569015&r2=1569016&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/changes/changes.xml (original)
+++ commons/proper/pool/trunk/src/changes/changes.xml Mon Feb 17 15:11:40 2014
@@ -44,7 +44,8 @@ The <action> type attribute can be add,u
   </properties>
   <body>
   <release version="2.2" date="TBD" description=
-"TBD">
+"This is a maintenance release that add a new testOnCreate configuration option
+and fixes a small number of bugs.">
     <action issue="POOL-248" dev="markt" type="fix" due-to="Warren Chen">
       Ensure that if an attempt is made to return an object multiple times that
       the current active and idle object counts are not corrupted.
@@ -55,6 +56,10 @@ The <action> type attribute can be add,u
     <action dev="markt" type="fix">
       Fix the remaining Javadoc warnings.
     </action>
+    <action dev="markt" type="add">
+      Add a new validation configuration option testOnCreate that tests an
+      object immediately after it is created.
+    </action>
   </release>
    <release version="2.1" date="2013-12-30" description=
 "This is a maintenance release that includes a small number of new features as well as 

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=1569016&r1=1569015&r2=1569016&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
Mon Feb 17 15:11:40 2014
@@ -68,6 +68,8 @@ public abstract class BaseGenericObjectP
     private volatile long maxWaitMillis =
             BaseObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS;
     private volatile boolean lifo = BaseObjectPoolConfig.DEFAULT_LIFO;
+    private volatile boolean testOnCreate =
+            BaseObjectPoolConfig.DEFAULT_TEST_ON_CREATE;
     private volatile boolean testOnBorrow =
             BaseObjectPoolConfig.DEFAULT_TEST_ON_BORROW;
     private volatile boolean testOnReturn =
@@ -271,6 +273,39 @@ public abstract class BaseGenericObjectP
     }
 
     /**
+     * Returns whether objects created for the pool will be validated before
+     * being returned from the <code>borrowObject()</code> method. Validation
is
+     * performed by the <code>validateObject()</code> method of the factory
+     * associated with the pool. If the object fails to validate, then
+     * <code>borrowObject()</code> will fail.
+     *
+     * @return <code>true</code> if newly created objects are validated before
+     *         being returned from the <code>borrowObject()</code> method
+     *
+     * @see #setTestOnCreate
+     */
+    public final boolean getTestOnCreate() {
+        return testOnCreate;
+    }
+
+    /**
+     * Sets whether objects created for the pool will be validated before
+     * being returned from the <code>borrowObject()</code> method. Validation
is
+     * performed by the <code>validateObject()</code> method of the factory
+     * associated with the pool. If the object fails to validate, then
+     * <code>borrowObject()</code> will fail.
+     *
+     * @param testOnCreate  <code>true</code> if newly created objects should
be
+     *                      validated before being returned from the
+     *                      <code>borrowObject()</code> method
+     *
+     * @see #getTestOnCreate
+     */
+    public final void setTestOnCreate(boolean testOnCreate) {
+        this.testOnCreate = testOnCreate;
+    }
+
+    /**
      * Returns whether objects borrowed from the pool will be validated before
      * being returned from the <code>borrowObject()</code> method. Validation
is
      * performed by the <code>validateObject()</code> method of the factory

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java?rev=1569016&r1=1569015&r2=1569016&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java
(original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java
Mon Feb 17 15:11:40 2014
@@ -69,6 +69,13 @@ public abstract class BaseObjectPoolConf
     public static final int DEFAULT_NUM_TESTS_PER_EVICTION_RUN = 3;
 
     /**
+     * The default value for the {@code testOnCreate} configuration attribute.
+     * @see GenericObjectPool#getTestOnCreate()
+     * @see GenericKeyedObjectPool#getTestOnCreate()
+     */
+    public static final boolean DEFAULT_TEST_ON_CREATE = false;
+
+    /**
      * The default value for the {@code testOnBorrow} configuration attribute.
      * @see GenericObjectPool#getTestOnBorrow()
      * @see GenericKeyedObjectPool#getTestOnBorrow()
@@ -153,6 +160,8 @@ public abstract class BaseObjectPoolConf
 
     private String evictionPolicyClassName = DEFAULT_EVICTION_POLICY_CLASS_NAME;
 
+    private boolean testOnCreate = DEFAULT_TEST_ON_CREATE;
+
     private boolean testOnBorrow = DEFAULT_TEST_ON_BORROW;
 
     private boolean testOnReturn = DEFAULT_TEST_ON_RETURN;
@@ -317,6 +326,34 @@ public abstract class BaseObjectPoolConf
     }
 
     /**
+     * Get the value for the {@code testOnCreate} configuration attribute for
+     * pools created with this configuration instance.
+     *
+     * @return  The current setting of {@code testOnCreate} for this
+     *          configuration instance
+     *
+     * @see GenericObjectPool#getTestOnCreate()
+     * @see GenericKeyedObjectPool#getTestOnCreate()
+     */
+    public boolean getTestOnCreate() {
+        return testOnCreate;
+    }
+
+    /**
+     * Set the value for the {@code testOnCreate} configuration attribute for
+     * pools created with this configuration instance.
+     *
+     * @param testOnCreate The new setting of {@code testOnCreate}
+     *        for this configuration instance
+     *
+     * @see GenericObjectPool#getTestOnCreate()
+     * @see GenericKeyedObjectPool#getTestOnCreate()
+     */
+    public void setTestOnCreate(boolean testOnCreate) {
+        this.testOnCreate = testOnCreate;
+    }
+
+    /**
      * Get the value for the {@code testOnBorrow} configuration attribute for
      * pools created with this configuration instance.
      *

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java?rev=1569016&r1=1569015&r2=1569016&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
(original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
Mon Feb 17 15:11:40 2014
@@ -243,6 +243,7 @@ public class GenericKeyedObjectPool<K,T>
         setMinIdlePerKey(conf.getMinIdlePerKey());
         setMaxWaitMillis(conf.getMaxWaitMillis());
         setBlockWhenExhausted(conf.getBlockWhenExhausted());
+        setTestOnCreate(conf.getTestOnCreate());
         setTestOnBorrow(conf.getTestOnBorrow());
         setTestOnReturn(conf.getTestOnReturn());
         setTestWhileIdle(conf.getTestWhileIdle());
@@ -298,7 +299,8 @@ public class GenericKeyedObjectPool<K,T>
      * <code>maxTotalPerKey</code> and the total number of instances in
      * circulation (under all keys) is less than <code>maxTotal</code>, a new
      * instance is created, activated and (if applicable) validated and returned
-     * to the caller.
+     * to the caller. If validation fails, a <code>NoSuchElementException</code>
+     * will be thrown.
      * <p>
      * If the associated sub-pool is exhausted (no available idle instances and
      * no capacity to create new ones), this method will either block
@@ -401,7 +403,7 @@ public class GenericKeyedObjectPool<K,T>
                             throw nsee;
                         }
                     }
-                    if (p != null && getTestOnBorrow()) {
+                    if (p != null && (getTestOnBorrow() || create && getTestOnCreate()))
{
                         boolean validate = false;
                         Throwable validationThrowable = null;
                         try {

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMXBean.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMXBean.java?rev=1569016&r1=1569015&r2=1569016&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMXBean.java
(original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMXBean.java
Mon Feb 17 15:11:40 2014
@@ -21,7 +21,7 @@ import java.util.Map;
 
 /**
  * Defines the methods that will be made available via JMX.
- * 
+ *
  * NOTE: This interface is subject to change between major version releases
  * of commons pool.
  *
@@ -89,6 +89,11 @@ public interface GenericKeyedObjectPoolM
      */
     int getNumTestsPerEvictionRun();
     /**
+     * See {@link GenericKeyedObjectPool#getTestOnCreate()}
+     * @return See {@link GenericKeyedObjectPool#getTestOnCreate()}
+     */
+    boolean getTestOnCreate();
+    /**
      * See {@link GenericKeyedObjectPool#getTestOnBorrow()}
      * @return See {@link GenericKeyedObjectPool#getTestOnBorrow()}
      */

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=1569016&r1=1569015&r2=1569016&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 Feb 17 15:11:40 2014
@@ -306,6 +306,7 @@ public class GenericObjectPool<T> extend
         setMaxTotal(conf.getMaxTotal());
         setMaxWaitMillis(conf.getMaxWaitMillis());
         setBlockWhenExhausted(conf.getBlockWhenExhausted());
+        setTestOnCreate(conf.getTestOnCreate());
         setTestOnBorrow(conf.getTestOnBorrow());
         setTestOnReturn(conf.getTestOnReturn());
         setTestWhileIdle(conf.getTestWhileIdle());
@@ -378,7 +379,8 @@ public class GenericObjectPool<T> extend
      * <code>borrowMaxWaitMillis</code> parameter. If the number of instances
      * checked out from the pool is less than <code>maxTotal,</code> a new
      * instance is created, activated and (if applicable) validated and returned
-     * to the caller.
+     * to the caller. If validation fails, a <code>NoSuchElementException</code>
+     * is thrown.
      * <p>
      * If the pool is exhausted (no available idle instances and no capacity to
      * create new ones), this method will either block (if
@@ -479,7 +481,7 @@ public class GenericObjectPool<T> extend
                         throw nsee;
                     }
                 }
-                if (p != null && getTestOnBorrow()) {
+                if (p != null && (getTestOnBorrow() || create && getTestOnCreate()))
{
                     boolean validate = false;
                     Throwable validationThrowable = null;
                     try {

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMXBean.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMXBean.java?rev=1569016&r1=1569015&r2=1569016&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMXBean.java
(original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMXBean.java
Mon Feb 17 15:11:40 2014
@@ -20,7 +20,7 @@ import java.util.Set;
 
 /**
  * Defines the methods that will be made available via JMX.
- * 
+ *
  * NOTE: This interface is subject to change between major version releases
  * of commons pool.
  *
@@ -81,6 +81,11 @@ public interface GenericObjectPoolMXBean
      */
     int getNumTestsPerEvictionRun();
     /**
+     * See {@link GenericObjectPool#getTestOnCreate()}
+     * @return See {@link GenericObjectPool#getTestOnCreate()}
+     */
+    boolean getTestOnCreate();
+    /**
      * See {@link GenericObjectPool#getTestOnBorrow()}
      * @return See {@link GenericObjectPool#getTestOnBorrow()}
      */

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=1569016&r1=1569015&r2=1569016&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 Feb 17 15:11:40 2014
@@ -861,6 +861,7 @@ public class TestGenericObjectPool exten
         expected.setMaxWaitMillis(5L);
         expected.setMinEvictableIdleTimeMillis(7L);
         expected.setNumTestsPerEvictionRun(9);
+        expected.setTestOnCreate(true);
         expected.setTestOnBorrow(true);
         expected.setTestOnReturn(true);
         expected.setTestWhileIdle(true);
@@ -1589,6 +1590,8 @@ public class TestGenericObjectPool exten
     private SimpleFactory factory = null;
 
     private void assertConfiguration(GenericObjectPoolConfig expected, GenericObjectPool<?>
actual) throws Exception {
+        assertEquals("testOnCreate",Boolean.valueOf(expected.getTestOnCreate()),
+                Boolean.valueOf(actual.getTestOnCreate()));
         assertEquals("testOnBorrow",Boolean.valueOf(expected.getTestOnBorrow()),
                 Boolean.valueOf(actual.getTestOnBorrow()));
         assertEquals("testOnReturn",Boolean.valueOf(expected.getTestOnReturn()),



Mime
View raw message