From 2676d9d428772310a6355f3f3f2f61bbc8640599 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Thu, 25 Oct 2012 17:23:19 +0000
Subject: [PATCH] Fix OPENDJ-627: ConnectionPool internal state becomes invalid when stale connections are discarded

---
 opendj-sdk/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/FixedConnectionPool.java    |   20 ++++++++++----------
 opendj-sdk/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java |    2 +-
 2 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/opendj-sdk/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/FixedConnectionPool.java b/opendj-sdk/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/FixedConnectionPool.java
index 404b0f6..2104786 100644
--- a/opendj-sdk/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/FixedConnectionPool.java
+++ b/opendj-sdk/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/FixedConnectionPool.java
@@ -233,12 +233,9 @@
                 // The connection may have been disconnected by the remote
                 // server, but the server may still be available. In order to
                 // avoid leaving pending futures hanging indefinitely, we should
-                // try to reconnect immediately.
-
-                // Close the dead connection.
+                // try to reconnect immediately. No need to release/acquire
+                // currentPoolSize.
                 connection.close();
-
-                // Try to get a new connection to replace it.
                 factory.getConnectionAsync(connectionResultHandler);
 
                 if (DEBUG_LOG.isLoggable(Level.FINE)) {
@@ -543,11 +540,7 @@
 
         @SuppressWarnings("unchecked")
         AsynchronousFutureResult<Connection> getWaitingFuture() {
-            if (value instanceof AsynchronousFutureResult) {
-                return (AsynchronousFutureResult<Connection>) value;
-            } else {
-                throw new IllegalStateException();
-            }
+            return (AsynchronousFutureResult<Connection>) value;
         }
 
         boolean isWaitingFuture() {
@@ -656,6 +649,13 @@
                 } else {
                     // Close the stale connection and try again.
                     connection.close();
+                    currentPoolSize.release();
+
+                    if (DEBUG_LOG.isLoggable(Level.FINE)) {
+                        DEBUG_LOG.fine(String.format("Connection no longer valid. "
+                                + "currentPoolSize=%d, poolSize=%d", poolSize
+                                - currentPoolSize.availablePermits(), poolSize));
+                    }
                 }
             } else {
                 // Grow the pool if needed.
diff --git a/opendj-sdk/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java b/opendj-sdk/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java
index 2872a3a..be92d7c 100644
--- a/opendj-sdk/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java
+++ b/opendj-sdk/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java
@@ -373,7 +373,7 @@
      * @throws Exception
      *             If an unexpected error occurred.
      */
-    @Test(enabled = false)
+    @Test
     public void testSkipStaleConnectionsOnGetWhenAtCapacity() throws Exception {
         // Setup.
         final Connection connection1 = mock(Connection.class);

--
Gitblit v1.10.0