< prev index next >

src/java.naming/share/classes/com/sun/jndi/ldap/pool/Pool.java

Print this page

        

@@ -23,15 +23,15 @@
  * questions.
  */
 
 package com.sun.jndi.ldap.pool;
 
+import java.util.ArrayList;
 import java.util.Map;
 import java.util.WeakHashMap;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Iterator;
 import java.util.LinkedList;
 
 import java.io.PrintStream;
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;

@@ -164,21 +164,29 @@
      *
      * @param threshold connections idle before 'threshold' should be closed
      *          and removed.
      */
     public void expire(long threshold) {
+        Collection<ConnectionsRef> copy;
         synchronized (map) {
-            Iterator<ConnectionsRef> iter = map.values().iterator();
+            copy = new ArrayList<>(map.values());
+        }
+
+        ArrayList<ConnectionsRef> removed = new ArrayList<>();
             Connections conns;
-            while (iter.hasNext()) {
-                conns = iter.next().getConnections();
+        for (ConnectionsRef ref : copy) {
+            conns = ref.getConnections();
                 if (conns.expire(threshold)) {
                     d("expire(): removing ", conns);
-                    iter.remove();
+                removed.add(ref);
                 }
             }
+
+        synchronized (map) {
+            map.values().removeAll(removed);
         }
+
         expungeStaleConnections();
     }
 
     /*
      * Closes the connections contained in the ConnectionsRef object that
< prev index next >