Author: kwright
Date: Mon Feb  4 13:14:31 2013
New Revision: 1442101

URL: http://svn.apache.org/viewvc?rev=1442101&view=rev
Log:
As part of CONNECTORS-638, supply debugging code that will dump a list of 
connection handles and stack traces from where they were allocated, if the 
system runs out of database handles.

Modified:
    
manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/jdbcpool/ConnectionPool.java
    
manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/jdbcpool/WrappedConnection.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=1442101&r1=1442100&r2=1442101&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
 Mon Feb  4 13:14:31 2013
@@ -40,6 +40,10 @@ public class ConnectionPool
   protected long[] connectionCleanupTimeouts;
   protected long expiration;
   
+  protected final static boolean debug = true;
+  
+  protected List<WrappedConnection> outstandingConnections = new 
ArrayList<WrappedConnection>();
+  
   /** Constructor */
   public ConnectionPool(String dbURL, String userName, String password, int 
maxConnections, long expiration)
   {
@@ -72,10 +76,22 @@ public class ConnectionPool
             throw new InterruptedException("Pool already closed");
           Connection rval = freeConnections[--freePointer];
           freeConnections[freePointer] = null;
-          return new WrappedConnection(this,rval);
+          WrappedConnection rval3 = new WrappedConnection(this,rval);
+          if (debug)
+            outstandingConnections.add(rval3);
+          return rval3;
         }
         if (activeConnections == freeConnections.length)
         {
+          // 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 (int i = 0; i < outstandingConnections.size(); i++)
+            {
+              outstandingConnections.get(i).printAllocationStackTrace();
+            }
+          }
           // Wait until kicked; we hope something will free up...
           this.wait();
           continue;
@@ -100,7 +116,15 @@ public class ConnectionPool
       if (rval2 == null)
         activeConnections--;
     }
-    return new WrappedConnection(this,rval2);
+    WrappedConnection rval4 = new WrappedConnection(this,rval2);
+    if (debug)
+    {
+      synchronized (this)
+      {
+        outstandingConnections.add(rval4);
+      }
+    }
+    return rval4;
   }
   
   /** Close down the pool.
@@ -158,11 +182,13 @@ public class ConnectionPool
     }
   }
   
-  public synchronized void releaseConnection(Connection connection)
+  public synchronized void releaseConnection(WrappedConnection connection)
   {
-    freeConnections[freePointer] = connection;
+    freeConnections[freePointer] = connection.getConnection();
     connectionCleanupTimeouts[freePointer] = System.currentTimeMillis() + 
expiration;
     freePointer++;
+    if (debug)
+      outstandingConnections.remove(connection);
     notifyAll();
   }
   

Modified: 
manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/jdbcpool/WrappedConnection.java
URL: 
http://svn.apache.org/viewvc/manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/jdbcpool/WrappedConnection.java?rev=1442101&r1=1442100&r2=1442101&view=diff
==============================================================================
--- 
manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/jdbcpool/WrappedConnection.java
 (original)
+++ 
manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/jdbcpool/WrappedConnection.java
 Mon Feb  4 13:14:31 2013
@@ -19,6 +19,7 @@
 package org.apache.manifoldcf.core.jdbcpool;
 
 import java.sql.*;
+import org.apache.manifoldcf.core.system.Logging;
 
 /** The class that represents a connection from a pool.
 */
@@ -28,12 +29,15 @@ public class WrappedConnection
 
   protected Connection connection;
   protected ConnectionPool owner;
+  /** Exception, to keep track of where the connection was allocated */
+  protected Exception instantiationException;
   
   /** Constructor */
   public WrappedConnection(ConnectionPool owner, Connection connection)
   {
     this.owner = owner;
     this.connection = connection;
+    this.instantiationException = new Exception("Possibly leaked db 
conneciton");
   }
   
   /** Get the JDBC connection object.
@@ -47,9 +51,15 @@ public class WrappedConnection
   */
   public void release()
   {
-    owner.releaseConnection(this.connection);
+    owner.releaseConnection(this);
     this.connection = null;
   }
+  
+  /** Print allocation information */
+  public void printAllocationStackTrace()
+  {
+    Logging.db.warn("Found a possibly leaked db 
connection",instantiationException);
+  }
 }
 
 


Reply via email to