From: fhanik Date: Tue, 28 Oct 2008 14:34:28 +0000 (+0000) Subject: Improve validation ordering, when a connection has been acquired, but validation... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=0652b5559ba3376ad69c9bc5029629bc30e24488;p=tomcat7.0 Improve validation ordering, when a connection has been acquired, but validation failed, don't let the thread go back into polling mode, instead try to reconnect it. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@708586 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java index f2b94504e..f953ec700 100644 --- a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java +++ b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java @@ -330,7 +330,7 @@ public class ConnectionPool { try { con.lock(); if (getPoolProperties().isLogAbandoned()) { - log.warn("Connection has been abandoned" + con + ":" +con.getStackTrace()); + log.warn("Connection has been abandoned " + con + ":" +con.getStackTrace()); } con.abandon(); } finally { @@ -409,15 +409,10 @@ public class ConnectionPool { protected PooledConnection createConnection(long now, PooledConnection con) { //no connections where available we'll create one boolean error = false; - boolean inbusy = true; try { //connect and validate the connection con = create(); con.lock(); - if (!busy.offer(con)) { - inbusy = false; - log.debug("Connection doesn't fit into busy array, connection will not be traceable."); - } con.connect(); if (con.validate(PooledConnection.VALIDATE_INIT)) { //no need to lock a new one, its not contented @@ -425,6 +420,9 @@ public class ConnectionPool { if (getPoolProperties().isLogAbandoned()) { con.setStackTrace(getThreadDump()); } + if (!busy.offer(con)) { + log.debug("Connection doesn't fit into busy array, connection will not be traceable."); + } return con; } else { //validation failed, make sure we disconnect @@ -437,7 +435,6 @@ public class ConnectionPool { } finally { if (error ) { release(con); - if (inbusy) busy.remove(con); } con.unlock(); }//catch @@ -449,10 +446,7 @@ public class ConnectionPool { boolean setToNull = false; try { con.lock(); - if (con.isDiscarded()) { - //connection has already been disconnected - setToNull = true; - } else if (con.validate(PooledConnection.VALIDATE_BORROW)) { + if ((!con.isDiscarded()) && con.validate(PooledConnection.VALIDATE_BORROW)) { //set the timestamp con.setTimestamp(now); if (getPoolProperties().isLogAbandoned()) { @@ -463,11 +457,33 @@ public class ConnectionPool { log.debug("Connection doesn't fit into busy array, connection will not be traceable."); } return con; - } else { - /*if the object wasn't validated, we may as well remove it*/ - release(con); + } + //if we reached here, that means the connection + //is either discarded or validation failed. + //we will make one more attempt + //in order to guarantee that the thread that just acquired + //the connection shouldn't have to poll again. + try { + con.reconnect(); + if (con.validate(PooledConnection.VALIDATE_INIT)) { + //set the timestamp + con.setTimestamp(now); + if (getPoolProperties().isLogAbandoned()) { + //set the stack trace for this pool + con.setStackTrace(getThreadDump()); + } + if (!busy.offer(con)) { + log.debug("Connection doesn't fit into busy array, connection will not be traceable."); + } + return con; + } else { + release(con); + setToNull = true; + } + } catch (Exception x) { + release(con); setToNull = true; - } //end if + } } finally { con.unlock(); if (setToNull) {