From 541f15d2a7b06e49b9c81ada0a4a62c0a8d77c71 Mon Sep 17 00:00:00 2001 From: fhanik Date: Sat, 2 May 2009 04:57:47 +0000 Subject: [PATCH] Implement a hard limit, the 'optimistic' sizing limit is way to optimistic, and can let the pool grow a bit too fast. A hard limit stays hard. Clean up pooled connection a bit git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@770890 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tomcat/jdbc/pool/ConnectionPool.java | 12 ++++---- .../apache/tomcat/jdbc/pool/PoolProperties.java | 2 +- .../apache/tomcat/jdbc/pool/PooledConnection.java | 35 ++++++++-------------- 3 files changed, 21 insertions(+), 28 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 1989d5e88..d090aa78b 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 @@ -56,7 +56,7 @@ public class ConnectionPool { //=============================================================================== // INSTANCE/QUICK ACCESS VARIABLE //=============================================================================== - + private AtomicInteger size = new AtomicInteger(0); /** * All the information about the connection pool */ @@ -169,7 +169,7 @@ public class ConnectionPool { * @return int */ public int getSize() { - return idle.size()+busy.size(); + return size.get(); } /** @@ -409,7 +409,7 @@ public class ConnectionPool { if (jmxPool!=null) { jmxPool.notify(org.apache.tomcat.jdbc.pool.jmx.ConnectionPool.NOTIFY_ABANDON, trace); } - con.abandon(); + release(con); //we've asynchronously reduced the number of connections //we could have threads stuck in idle.poll(timeout) that will never be notified if (waitcount.get()>0) idle.offer(new PooledConnection(poolProperties,this)); @@ -429,6 +429,7 @@ public class ConnectionPool { con.lock(); con.release(); } finally { + size.addAndGet(-1); con.unlock(); } } @@ -463,7 +464,8 @@ public class ConnectionPool { //this is not 100% accurate since it doesn't use a shared //atomic variable - a connection can become idle while we are creating //a new connection - if (busy.size() < getPoolProperties().getMaxActive()) { + if (size.get() < getPoolProperties().getMaxActive()) { + size.addAndGet(1); return createConnection(now, con); } //end if @@ -757,7 +759,7 @@ public class ConnectionPool { continue; if (!con.validate(PooledConnection.VALIDATE_IDLE)) { idle.remove(con); - con.release(); + release(con); } } finally { con.unlock(); diff --git a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PoolProperties.java b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PoolProperties.java index 9d47b9b02..b28f411c3 100644 --- a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PoolProperties.java +++ b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PoolProperties.java @@ -68,7 +68,7 @@ public class PoolProperties { protected String initSQL; protected boolean testOnConnect =false; protected String jdbcInterceptors=null; - protected boolean fairQueue = true; + protected boolean fairQueue = false; protected boolean useEquals = false; protected int abandonWhenPercentageFull = 0; protected long maxAge = 0; 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 429e6af1a..5351fb7cb 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 @@ -68,35 +68,35 @@ public class PooledConnection { /** * The underlying database connection */ - protected java.sql.Connection connection; + private java.sql.Connection connection; /** * When we track abandon traces, this string holds the thread dump */ - protected String abandonTrace = null; + private String abandonTrace = null; /** * Timestamp the connection was last 'touched' by the pool */ - protected long timestamp; + private long timestamp; /** * Lock for this connection only */ - protected ReentrantReadWriteLock lock = new ReentrantReadWriteLock(false); + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(false); /** * Set to true if this connection has been discarded by the pool */ - protected boolean discarded = false; + private boolean discarded = false; /** * The Timestamp when the last time the connect() method was called successfully */ - protected volatile long lastConnected = -1; + private volatile long lastConnected = -1; /** * timestamp to keep track of validation intervals */ - protected long lastValidated = System.currentTimeMillis(); + private volatile long lastValidated = System.currentTimeMillis(); /** * The instance number for this connection */ - protected int instanceCount = 0; + private int instanceCount = 0; /** * The parent */ @@ -107,7 +107,7 @@ public class PooledConnection { * so that we don't create a new list of interceptors each time we borrow * the connection */ - protected WeakReference handler = null; + private WeakReference handler = null; public PooledConnection(PoolProperties prop, ConnectionPool parent) { @@ -116,7 +116,7 @@ public class PooledConnection { this.parent = parent; } - protected void connect() throws SQLException { + public void connect() throws SQLException { if (connection != null) { try { this.disconnect(false); @@ -178,12 +178,12 @@ public class PooledConnection { return connection!=null; } - protected void reconnect() throws SQLException { + public void reconnect() throws SQLException { this.disconnect(false); this.connect(); } //reconnect - protected void disconnect(boolean finalize) { + private void disconnect(boolean finalize) { if (isDiscarded()) { return; } @@ -215,16 +215,7 @@ public class PooledConnection { } //end if } - public boolean abandon() { - try { - disconnect(true); - } catch (Exception x) { - log.error("", x); - } //catch - return false; - } - - protected boolean doValidate(int action) { + private boolean doValidate(int action) { if (action == PooledConnection.VALIDATE_BORROW && poolProperties.isTestOnBorrow()) return true; -- 2.11.0