Improve validation ordering, when a connection has been acquired, but validation...
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 28 Oct 2008 14:34:28 +0000 (14:34 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 28 Oct 2008 14:34:28 +0000 (14:34 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@708586 13f79535-47bb-0310-9956-ffa450edef68

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

index f2b9450..f953ec7 100644 (file)
@@ -330,7 +330,7 @@ public class ConnectionPool {
         try {\r
             con.lock();\r
             if (getPoolProperties().isLogAbandoned()) {\r
-                log.warn("Connection has been abandoned" + con + ":" +con.getStackTrace());\r
+                log.warn("Connection has been abandoned " + con + ":" +con.getStackTrace());\r
             }\r
             con.abandon();\r
         } finally {\r
@@ -409,15 +409,10 @@ 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
             con.connect();\r
             if (con.validate(PooledConnection.VALIDATE_INIT)) {\r
                 //no need to lock a new one, its not contented\r
@@ -425,6 +420,9 @@ public class ConnectionPool {
                 if (getPoolProperties().isLogAbandoned()) {\r
                     con.setStackTrace(getThreadDump());\r
                 }\r
+                if (!busy.offer(con)) {\r
+                    log.debug("Connection doesn't fit into busy array, connection will not be traceable.");\r
+                }\r
                 return con;\r
             } else {\r
                 //validation failed, make sure we disconnect\r
@@ -437,7 +435,6 @@ public class ConnectionPool {
         } finally {\r
             if (error ) {\r
                 release(con);\r
-                if (inbusy) busy.remove(con);\r
             }\r
             con.unlock();\r
         }//catch\r
@@ -449,10 +446,7 @@ public class ConnectionPool {
         boolean setToNull = false;\r
         try {\r
             con.lock();\r
-            if (con.isDiscarded()) {\r
-                //connection has already been disconnected\r
-                setToNull = true;\r
-            } else if (con.validate(PooledConnection.VALIDATE_BORROW)) {\r
+            if ((!con.isDiscarded()) && con.validate(PooledConnection.VALIDATE_BORROW)) {\r
                 //set the timestamp\r
                 con.setTimestamp(now);\r
                 if (getPoolProperties().isLogAbandoned()) {\r
@@ -463,11 +457,33 @@ public class ConnectionPool {
                     log.debug("Connection doesn't fit into busy array, connection will not be traceable.");\r
                 }\r
                 return con;\r
-            } else {\r
-                /*if the object wasn't validated, we may as well remove it*/\r
-                release(con);\r
+            }\r
+            //if we reached here, that means the connection\r
+            //is either discarded or validation failed.\r
+            //we will make one more attempt\r
+            //in order to guarantee that the thread that just acquired\r
+            //the connection shouldn't have to poll again.\r
+            try {\r
+                con.reconnect();\r
+                if (con.validate(PooledConnection.VALIDATE_INIT)) {\r
+                    //set the timestamp\r
+                    con.setTimestamp(now);\r
+                    if (getPoolProperties().isLogAbandoned()) {\r
+                        //set the stack trace for this pool\r
+                        con.setStackTrace(getThreadDump());\r
+                    }\r
+                    if (!busy.offer(con)) {\r
+                        log.debug("Connection doesn't fit into busy array, connection will not be traceable.");\r
+                    }\r
+                    return con;\r
+                } else {\r
+                    release(con);\r
+                    setToNull = true;\r
+                }\r
+            } catch (Exception x) {\r
+                release(con);                \r
                 setToNull = true;\r
-            } //end if\r
+            }\r
         } finally {\r
             con.unlock();\r
             if (setToNull) {\r