//===============================================================================
-// CONNECTION POOLING IMPL
+// CONNECTION POOLING IMPL LOGIC
//===============================================================================
/**
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
try {
con.lock();
if (con.release()) {
+ //counter only decremented once
size.addAndGet(-1);
}
} finally {
-
con.unlock();
}
}
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;
}
//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 {
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)) {