Author: kwright
Date: Fri Aug 14 08:30:50 2015
New Revision: 1695839

URL: http://svn.apache.org/r1695839
Log:
Harden ConnectionPool against runtime exceptions and errors.  Part of 
CONNECTORS-1226.

Modified:
    
manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/jdbcpool/ConnectionPool.java

Modified: 
manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/jdbcpool/ConnectionPool.java
URL: 
http://svn.apache.org/viewvc/manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/jdbcpool/ConnectionPool.java?rev=1695839&r1=1695838&r2=1695839&view=diff
==============================================================================
--- 
manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/jdbcpool/ConnectionPool.java
 (original)
+++ 
manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/jdbcpool/ConnectionPool.java
 Fri Aug 14 08:30:50 2015
@@ -73,68 +73,74 @@ public class ConnectionPool
     else
       instantiationException = null;
     Connection rval = null;
-    while (true)
+    boolean returnedValue = true;
+    try
     {
-      synchronized (this)
+      while (true)
       {
-        if (freePointer > 0)
+        synchronized (this)
         {
-          if (closed)
-            throw new InterruptedException("Pool already closed");
-          rval = freeConnections[--freePointer];
-          freeConnections[freePointer] = null;
-          boolean isValid = true;
-          try
-          {
-            isValid = rval.isValid(1);
-          }
-          catch (SQLException e)
+          if (freePointer > 0)
           {
-            // Ignore this; we just can't check if handle is valid I guess.
-            // (Postgresql doesn't implement this method so it fails always)
-          }
-          if (!isValid) {
-            // If the connection is invalid, drop it on the floor, and get a 
new one.
-            activeConnections--;
-            rval.close();
-            rval = null;
-            continue;
+            if (closed)
+              throw new InterruptedException("Pool already closed");
+            rval = freeConnections[--freePointer];
+            freeConnections[freePointer] = null;
+            boolean isValid = true;
+            try
+            {
+              isValid = rval.isValid(1);
+            }
+            catch (SQLException e)
+            {
+              // Ignore this; we just can't check if handle is valid I guess.
+              // (Postgresql doesn't implement this method so it fails always)
+            }
+            if (!isValid) {
+              // If the connection is invalid, drop it on the floor, and get a 
new one.
+              // Note: Order of operations is terribly important here!!
+              final Connection closeValue = rval;
+              rval = null;
+              activeConnections--;
+              try
+              {
+                closeValue.close();
+              }
+              catch (SQLException e)
+              {
+                // Ignore SQL errors on close, and drop the connection on the 
floor
+              }
+              continue;
+            }
+            break;
           }
-          break;
-        }
-        if (activeConnections == freeConnections.length)
-        {
-          // If properly configured, we really shouldn't be getting here.
-          if (debug)
+          if (activeConnections == freeConnections.length)
           {
-            synchronized (outstandingConnections)
+            // If properly configured, we really shouldn't be getting here.
+            if (debug)
             {
-              Logging.db.warn("Out of db connections, list of outstanding ones 
follows.");
-              for (WrappedConnection c : outstandingConnections)
+              synchronized (outstandingConnections)
               {
-                Logging.db.warn("Found a possibly leaked db 
connection",c.getInstantiationException());
+                Logging.db.warn("Out of db connections, list of outstanding 
ones follows.");
+                for (WrappedConnection c : outstandingConnections)
+                {
+                  Logging.db.warn("Found a possibly leaked db 
connection",c.getInstantiationException());
+                }
               }
             }
+            // Wait until kicked; we hope something will free up...
+            this.wait();
+            continue;
           }
-          // Wait until kicked; we hope something will free up...
-          this.wait();
-          continue;
+          // Increment active connection counter, because we're about to mint 
a new connection, and break out of our loop
+          // Note: order is terribly important here!
+          activeConnections++;
+          if (userName != null)
+            rval = DriverManager.getConnection(dbURL, userName, password);
+          else
+            rval = DriverManager.getConnection(dbURL);
+          break;
         }
-        // Increment active connection counter, because we're about to mint a 
new connection, and break out of our loop
-        activeConnections++;
-        break;
-      }
-    }
-
-    boolean returnedValue = true;
-    try
-    {
-      if (rval == null)
-      {
-        if (userName != null)
-          rval = DriverManager.getConnection(dbURL, userName, password);
-        else
-          rval = DriverManager.getConnection(dbURL);
       }
 
       WrappedConnection wc = new 
WrappedConnection(this,rval,instantiationException);


Reply via email to