From 5016359f72c7e351884bf002bebf2627b2f70f64 Mon Sep 17 00:00:00 2001 From: fhanik Date: Sat, 22 Nov 2008 23:13:05 +0000 Subject: [PATCH] Add ability to customize wait plan git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@719936 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/tomcat/jdbc/pool/ConnectionPool.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) 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 35e783b6a..ff737404f 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 @@ -123,7 +123,7 @@ public class ConnectionPool { */ public Connection getConnection() throws SQLException { //check out a connection - PooledConnection con = (PooledConnection)borrowConnection(); + PooledConnection con = (PooledConnection)borrowConnection(-1); return setupConnection(con); } @@ -316,7 +316,7 @@ public class ConnectionPool { PooledConnection[] initialPool = new PooledConnection[poolProperties.getInitialSize()]; try { for (int i = 0; i < initialPool.length; i++) { - initialPool[i] = this.borrowConnection(); + initialPool[i] = this.borrowConnection(0); //don't wait, should be no contention } //for } catch (SQLException x) { @@ -376,10 +376,12 @@ public class ConnectionPool { /** * Thread safe way to retrieve a connection from the pool + * @param wait - time to wait, overrides the maxWait from the properties, + * set to -1 if you wish to use maxWait, 0 if you wish no wait time. * @return PooledConnection * @throws SQLException */ - protected PooledConnection borrowConnection() throws SQLException { + protected PooledConnection borrowConnection(int wait) throws SQLException { if (isClosed()) { throw new SQLException("Connection pool closed."); @@ -405,7 +407,10 @@ public class ConnectionPool { } //end if //calculate wait time for this iteration - long maxWait = (getPoolProperties().getMaxWait()<=0)?Long.MAX_VALUE:getPoolProperties().getMaxWait(); + long maxWait = wait; + if (wait==-1) { + maxWait = (getPoolProperties().getMaxWait()<=0)?Long.MAX_VALUE:getPoolProperties().getMaxWait(); + } long timetowait = Math.max(0, maxWait - (System.currentTimeMillis() - now)); try { //retrieve an existing connection @@ -413,6 +418,9 @@ public class ConnectionPool { } catch (InterruptedException ex) { Thread.currentThread().interrupted(); } + if (maxWait==0) { //no wait, return one if we have one + return con; + } //we didn't get a connection, lets see if we timed out if (con == null) { if ((System.currentTimeMillis() - now) >= maxWait) { -- 2.11.0