From 182e027521ba230c4704ff15075d7061b9d3778b Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Fri, 21 Mar 2014 02:42:41 +1300 Subject: [PATCH] Further changes for #79 - returning/cleanup of bad connection --- .../server/lib/sql/DataSourcePool.java | 33 ++++++++++++++++--- .../server/lib/sql/PooledConnection.java | 10 +++--- .../server/lib/sql/PooledConnectionQueue.java | 4 +-- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/avaje/ebeaninternal/server/lib/sql/DataSourcePool.java b/src/main/java/com/avaje/ebeaninternal/server/lib/sql/DataSourcePool.java index de80b9c7b..f6fd4df49 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/lib/sql/DataSourcePool.java +++ b/src/main/java/com/avaje/ebeaninternal/server/lib/sql/DataSourcePool.java @@ -597,12 +597,35 @@ public class DataSourcePool implements DataSource { */ protected void returnConnection(PooledConnection pooledConnection) { - if (poolListener != null) { - poolListener.onBeforeReturnConnection(pooledConnection); - } - queue.returnPooledConnection(pooledConnection); + // return a normal 'good' connection + returnTheConnection(pooledConnection, false); } - + + /** + * This is a bad connection and must be removed from the pool's busy list and fully closed. + */ + protected void returnConnectionForceClose(PooledConnection pooledConnection) { + + returnTheConnection(pooledConnection, true); + } + + /** + * Return connection. If forceClose is true then this is a bad connection that + * must be removed and closed fully. + */ + private void returnTheConnection(PooledConnection pooledConnection, boolean forceClose) { + + if (poolListener != null && !forceClose) { + poolListener.onBeforeReturnConnection(pooledConnection); + } + queue.returnPooledConnection(pooledConnection, forceClose); + + if (forceClose) { + // Got a bad connection so check the pool + checkDataSource(); + } + } + /** * Collect statistics of a connection that is fully closing */ diff --git a/src/main/java/com/avaje/ebeaninternal/server/lib/sql/PooledConnection.java b/src/main/java/com/avaje/ebeaninternal/server/lib/sql/PooledConnection.java index cd6ade3e0..3053c56ba 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/lib/sql/PooledConnection.java +++ b/src/main/java/com/avaje/ebeaninternal/server/lib/sql/PooledConnection.java @@ -508,9 +508,8 @@ public class PooledConnection extends ConnectionDelegator { if (hadErrors) { if (!pool.validateConnection(this)) { - // the connection is BAD, close it and test the pool - closeConnectionFully(false); - pool.checkDataSource(); + // the connection is BAD, remove it, close it and test the pool + pool.returnConnectionForceClose(this); return; } } @@ -535,10 +534,9 @@ public class PooledConnection extends ConnectionDelegator { pool.returnConnection(this); } catch (Exception ex) { - // the connection is BAD, close it and test the pool + // the connection is BAD, remove it, close it and test the pool logger.warn("Error when trying to return connection to pool, closing fully.", ex); - closeConnectionFully(false); - pool.checkDataSource(); + pool.returnConnectionForceClose(this); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/lib/sql/PooledConnectionQueue.java b/src/main/java/com/avaje/ebeaninternal/server/lib/sql/PooledConnectionQueue.java index e355394d1..63713ba68 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/lib/sql/PooledConnectionQueue.java +++ b/src/main/java/com/avaje/ebeaninternal/server/lib/sql/PooledConnectionQueue.java @@ -238,7 +238,7 @@ public class PooledConnectionQueue { /** * Return a PooledConnection. */ - protected void returnPooledConnection(PooledConnection c) { + protected void returnPooledConnection(PooledConnection c, boolean forceClose) { final ReentrantLock lock = this.lock; lock.lock(); @@ -246,7 +246,7 @@ public class PooledConnectionQueue { if (!busyList.remove(c)) { logger.error("Connection [{}] not found in BusyList? ", c); } - if (c.shouldTrimOnReturn(lastResetTime, maxAgeMillis)) { + if (forceClose || c.shouldTrimOnReturn(lastResetTime, maxAgeMillis)) { c.closeConnectionFully(false); } else {