commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r791295 - /commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java
Date Sun, 05 Jul 2009 18:15:42 GMT
Author: markt
Date: Sun Jul  5 18:15:41 2009
New Revision: 791295

URL: http://svn.apache.org/viewvc?rev=791295&view=rev
Log:
Fix POOL-146.
Handle the case where one key has reached maxActive but other keys have not. Prior to this
patch, once a latch associated with the key that had reach the front of the allocation queue,
all allocations would be blocked until a suitable object was available. With this patch if
maxActive is set, allocate() looks at every object in the allocation queue (subject to any
totalActive limit) rather than just the first.

Modified:
    commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java

Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java?rev=791295&r1=791294&r2=791295&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java
(original)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java
Sun Jul  5 18:15:41 2009
@@ -1235,53 +1235,59 @@
         synchronized (this) {
             if (isClosed()) return;
             
-            for (;;) {
-                if (!_allocationQueue.isEmpty()) {
-                    // First use any objects in the pool to clear the queue
-                    Latch latch = (Latch) _allocationQueue.getFirst();
-                    ObjectQueue pool = (ObjectQueue)(_poolMap.get(latch.getkey()));
-                    if (null == pool) {
-                        pool = new ObjectQueue();
-                        _poolMap.put(latch.getkey(), pool);
-                        _poolList.add(latch.getkey());
-                    }
-                    latch.setPool(pool);
-                    if (!pool.queue.isEmpty()) {
-                        _allocationQueue.removeFirst();
-                        latch.setPair(
-                                (ObjectTimestampPair) pool.queue.removeFirst());
-                        pool.incrementInternalProcessingCount();
-                        _totalIdle--;
-                        synchronized (latch) {
-                            latch.notify();
-                        }
-                        // Next item in queue
-                        continue;
-                    }
-    
-                    // If there is a totalMaxActive and we are at the limit then
-                    // we have to make room
-                    if ((_maxTotal > 0) &&
-                            (_totalActive + _totalIdle + _totalInternalProcessing >= _maxTotal))
{
-                        clearOldest = true;
-                        break;
+            Iterator allocationQueueIter = _allocationQueue.iterator();
+            
+            while (allocationQueueIter.hasNext()) {
+                // First use any objects in the pool to clear the queue
+                Latch latch = (Latch) allocationQueueIter.next();
+                ObjectQueue pool = (ObjectQueue)(_poolMap.get(latch.getkey()));
+                if (null == pool) {
+                    pool = new ObjectQueue();
+                    _poolMap.put(latch.getkey(), pool);
+                    _poolList.add(latch.getkey());
+                }
+                latch.setPool(pool);
+                if (!pool.queue.isEmpty()) {
+                    allocationQueueIter.remove();
+                    latch.setPair(
+                            (ObjectTimestampPair) pool.queue.removeFirst());
+                    pool.incrementInternalProcessingCount();
+                    _totalIdle--;
+                    synchronized (latch) {
+                        latch.notify();
                     }
-    
-                    // Second utilise any spare capacity to create new objects
-                    if ((_maxActive < 0 || pool.activeCount + pool.internalProcessingCount
< _maxActive) &&
-                            (_maxTotal < 0 || _totalActive + _totalIdle + _totalInternalProcessing
< _maxTotal)) {
-                        // allow new object to be created
-                        _allocationQueue.removeFirst();
-                        latch.setMayCreate(true);
-                        pool.incrementInternalProcessingCount();
-                        synchronized (latch) {
-                            latch.notify();
-                        }
-                        // Next item in queue
-                        continue;
+                    // Next item in queue
+                    continue;
+                }
+
+                // If there is a totalMaxActive and we are at the limit then
+                // we have to make room
+                if ((_maxTotal > 0) &&
+                        (_totalActive + _totalIdle + _totalInternalProcessing >= _maxTotal))
{
+                    clearOldest = true;
+                    break;
+                }
+
+                // Second utilise any spare capacity to create new objects
+                if ((_maxActive < 0 || pool.activeCount + pool.internalProcessingCount
< _maxActive) &&
+                        (_maxTotal < 0 || _totalActive + _totalIdle + _totalInternalProcessing
< _maxTotal)) {
+                    // allow new object to be created
+                    allocationQueueIter.remove();
+                    latch.setMayCreate(true);
+                    pool.incrementInternalProcessingCount();
+                    synchronized (latch) {
+                        latch.notify();
                     }
+                    // Next item in queue
+                    continue;
+                }
+
+                // If there is no per-key limit and we reach this point we
+                // must have allocated all the objects we possibly can and there
+                // is no point looking at the rest of the allocation queue
+                if (_maxActive < 0) {
+                    break;
                 }
-                break;
             }
         }
         



Mime
View raw message