Index: C:/home/holger/Projects/h2oWorkspace/commons-pool/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java =================================================================== --- C:/home/holger/Projects/h2oWorkspace/commons-pool/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java (revision 474163) +++ C:/home/holger/Projects/h2oWorkspace/commons-pool/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java (working copy) @@ -1032,8 +1032,7 @@ return pool != null ? pool.queue.size() : 0; } - public synchronized void returnObject(Object key, Object obj) throws Exception { - + public void returnObject(Object key, Object obj) throws Exception { // if we need to validate this object, do so boolean success = true; // whether or not this object passed validation if(_testOnReturn && !_factory.validateObject(key, obj)) { @@ -1061,23 +1060,26 @@ } boolean shouldDestroy = false; - // grab the pool (list) of objects associated with the given key - ObjectQueue pool = (ObjectQueue) (_poolMap.get(key)); - // if it doesn't exist, create it - if(null == pool) { - pool = new ObjectQueue(); - _poolMap.put(key, pool); + + synchronized (this) { + // grab the pool (list) of objects associated with the given key + ObjectQueue pool = (ObjectQueue) (_poolMap.get(key)); + // if it doesn't exist, create it + if(null == pool) { + pool = new ObjectQueue(); + _poolMap.put(key, pool); + } + pool.decrementActiveCount(); + // if there's no space in the pool, flag the object for destruction + // else if we passivated succesfully, return it to the pool + if(_maxIdle >= 0 && (pool.queue.size() >= _maxIdle)) { + shouldDestroy = true; + } else if(success) { + pool.queue.addLast(new ObjectTimestampPair(obj)); + _totalIdle++; + } + notifyAll(); } - pool.decrementActiveCount(); - // if there's no space in the pool, flag the object for destruction - // else if we passivated succesfully, return it to the pool - if(_maxIdle >= 0 && (pool.queue.size() >= _maxIdle)) { - shouldDestroy = true; - } else if(success) { - pool.queue.addLast(new ObjectTimestampPair(obj)); - _totalIdle++; - } - notifyAll(); if(shouldDestroy) { try { @@ -1088,19 +1090,21 @@ } } - public synchronized void invalidateObject(Object key, Object obj) throws Exception { + public void invalidateObject(Object key, Object obj) throws Exception { try { _factory.destroyObject(key, obj); } catch (Exception e) { // swallowed } finally { - ObjectQueue pool = (ObjectQueue) (_poolMap.get(key)); - if(null == pool) { - pool = new ObjectQueue(); - _poolMap.put(key, pool); + synchronized (this) { + ObjectQueue pool = (ObjectQueue) (_poolMap.get(key)); + if(null == pool) { + pool = new ObjectQueue(); + _poolMap.put(key, pool); + } + pool.decrementActiveCount(); + notifyAll(); // _totalActive has changed } - pool.decrementActiveCount(); - notifyAll(); // _totalActive has changed } }