Improvements to connection handling
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 27 Oct 2008 22:24:26 +0000 (22:24 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 27 Oct 2008 22:24:26 +0000 (22:24 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@708354 13f79535-47bb-0310-9956-ffa450edef68

modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java
modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java

index 1b594f3..f2b9450 100644 (file)
@@ -409,13 +409,15 @@ public class ConnectionPool {
     protected PooledConnection createConnection(long now, PooledConnection con) {\r
         //no connections where available we'll create one\r
         boolean error = false;\r
+        boolean inbusy = true;\r
         try {\r
             //connect and validate the connection\r
             con = create();\r
             con.lock();\r
             if (!busy.offer(con)) {\r
+                inbusy = false;\r
                 log.debug("Connection doesn't fit into busy array, connection will not be traceable.");\r
-            }\r
+            } \r
             con.connect();\r
             if (con.validate(PooledConnection.VALIDATE_INIT)) {\r
                 //no need to lock a new one, its not contented\r
@@ -424,6 +426,10 @@ public class ConnectionPool {
                     con.setStackTrace(getThreadDump());\r
                 }\r
                 return con;\r
+            } else {\r
+                //validation failed, make sure we disconnect\r
+                //and clean up\r
+                error =true;\r
             } //end if\r
         } catch (Exception e) {\r
             error = true;\r
@@ -431,7 +437,7 @@ public class ConnectionPool {
         } finally {\r
             if (error ) {\r
                 release(con);\r
-                busy.remove(con);\r
+                if (inbusy) busy.remove(con);\r
             }\r
             con.unlock();\r
         }//catch\r
index 7998391..045aa83 100644 (file)
@@ -61,7 +61,7 @@ public class PooledConnection {
     protected void connect() throws SQLException {\r
         if (connection != null) {\r
             try {\r
-                this.disconnect();\r
+                this.disconnect(false);\r
             } catch (Exception x) {\r
                 log.error("Unable to disconnect previous connection.", x);\r
             } //catch\r
@@ -90,11 +90,11 @@ public class PooledConnection {
     }\r
 \r
     protected void reconnect() throws SQLException {\r
-        this.disconnect();\r
+        this.disconnect(false);\r
         this.connect();\r
     } //reconnect\r
 \r
-    protected synchronized void disconnect() throws SQLException {\r
+    protected synchronized void disconnect(boolean finalize) throws SQLException {\r
         if (isDiscarded()) {\r
             return;\r
         }\r
@@ -103,7 +103,7 @@ public class PooledConnection {
             connection.close();\r
         }\r
         connection = null;\r
-        parent.finalize(this);\r
+        if (finalize) parent.finalize(this);\r
     }\r
 \r
 \r
@@ -121,7 +121,7 @@ public class PooledConnection {
 \r
     public boolean abandon() {\r
         try {\r
-            disconnect();\r
+            disconnect(true);\r
         } catch (SQLException x) {\r
             log.error("", x);\r
         } //catch\r
@@ -157,6 +157,10 @@ public class PooledConnection {
     }\r
 \r
     public boolean validate(int validateAction,String sql) {\r
+        if (this.isDiscarded()) {\r
+            return false;\r
+        }\r
+        \r
         if (!doValidate(validateAction)) {\r
             //no validation required, no init sql and props not set\r
             return true;\r
@@ -202,9 +206,15 @@ public class PooledConnection {
      */\r
     public void release() {\r
         try {\r
-            disconnect();\r
+            disconnect(true);\r
         } catch (SQLException x) {\r
-            //TODO\r
+            if (log.isDebugEnabled()) {\r
+                log.debug("Unable to close SQL connection",x);\r
+            }\r
+        } catch (Exception x) {\r
+            if (log.isDebugEnabled()) {\r
+                log.debug("Unable to close SQL connection",x);\r
+            }\r
         }\r
 \r
     }\r