From a75aee8f1f25e6636742dd52c6a24f183716dcc0 Mon Sep 17 00:00:00 2001 From: fhanik Date: Fri, 10 Jul 2009 16:25:14 +0000 Subject: [PATCH] Added some comments and more thread safety around the size of the pool git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@793017 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tomcat/jdbc/pool/ConnectionPool.java | 23 ++++++++++++++++------ .../apache/tomcat/jdbc/pool/PooledConnection.java | 2 +- 2 files changed, 18 insertions(+), 7 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 739cebc45..a8ea1c5a1 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 @@ -450,7 +450,7 @@ public class ConnectionPool { //=============================================================================== -// CONNECTION POOLING IMPL +// CONNECTION POOLING IMPL LOGIC //=============================================================================== /** @@ -471,6 +471,7 @@ public class ConnectionPool { if (jmxPool!=null) { jmxPool.notify(org.apache.tomcat.jdbc.pool.jmx.ConnectionPool.NOTIFY_ABANDON, trace); } + //release the connection release(con); //we've asynchronously reduced the number of connections //we could have threads stuck in idle.poll(timeout) that will never be notified @@ -490,10 +491,10 @@ public class ConnectionPool { try { con.lock(); if (con.release()) { + //counter only decremented once size.addAndGet(-1); } } finally { - con.unlock(); } } @@ -518,9 +519,9 @@ public class ConnectionPool { while (true) { if (con!=null) { + //configure the connection and return it PooledConnection result = borrowConnection(now, con); - //validation might have failed, in which case null is returned - //should not happen anymore + //null should never be returned, but was in a previous impl. if (result!=null) return result; } @@ -529,15 +530,22 @@ public class ConnectionPool { //atomic variable - a connection can become idle while we are creating //a new connection if (size.get() < getPoolProperties().getMaxActive()) { - size.addAndGet(1); - return createConnection(now, con); + //atomic duplicate check + if (size.addAndGet(1) > getPoolProperties().getMaxActive()) { + return createConnection(now, con); + } else { + //if we got here, two threads passed through the first if + size.decrementAndGet(); + } } //end if //calculate wait time for this iteration long maxWait = wait; + //if the passed in wait time is -1, means we should use the pool property value if (wait==-1) { maxWait = (getPoolProperties().getMaxWait()<=0)?Long.MAX_VALUE:getPoolProperties().getMaxWait(); } + long timetowait = Math.max(0, maxWait - (System.currentTimeMillis() - now)); waitcount.incrementAndGet(); try { @@ -614,10 +622,13 @@ public class ConnectionPool { protected PooledConnection borrowConnection(long now, PooledConnection con) throws SQLException { //we have a connection, lets set it up + + //flag to see if we need to nullify boolean setToNull = false; try { con.lock(); if (!con.isDiscarded() && !con.isInitialized()) { + //attempt to connect con.connect(); } if ((!con.isDiscarded()) && con.validate(PooledConnection.VALIDATE_BORROW)) { diff --git a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java index 7c13581f9..e9eadc88b 100644 --- a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java +++ b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java @@ -85,7 +85,7 @@ public class PooledConnection { /** * Set to true if this connection has been discarded by the pool */ - private boolean discarded = false; + private volatile boolean discarded = false; /** * The Timestamp when the last time the connect() method was called successfully */ -- 2.11.0