8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.jndi.ldap.pool;
27
28 import java.util.Map;
29 import java.util.WeakHashMap;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.Iterator;
33 import java.util.LinkedList;
34
35 import java.io.PrintStream;
36 import java.lang.ref.Reference;
37 import java.lang.ref.ReferenceQueue;
38 import javax.naming.NamingException;
39
40 /**
41 * A map of pool ids to Connections.
42 * Key is an object that uniquely identifies a PooledConnection request
43 * (typically information needed to create the connection).
44 * The definitions of the key's equals() and hashCode() methods are
45 * vital to its unique identification in a Pool.
46 *
47 * Value is a ConnectionsRef, which is a reference to Connections,
48 * a list of equivalent connections.
49 *
50 * Supports methods that
51 * - retrieves (or creates as necessary) a connection from the pool
52 * - removes expired connections from the pool
149 }
150
151 return conns.get(timeout, factory); // get one connection from list
152 }
153
154 private Connections getConnections(Object id) {
155 ConnectionsRef ref = map.get(id);
156 return (ref != null) ? ref.getConnections() : null;
157 }
158
159 /**
160 * Goes through the connections in this Pool and expires ones that
161 * have been idle before 'threshold'. An expired connection is closed
162 * and then removed from the pool (removePooledConnection() will eventually
163 * be called, and the list of pools itself removed if it becomes empty).
164 *
165 * @param threshold connections idle before 'threshold' should be closed
166 * and removed.
167 */
168 public void expire(long threshold) {
169 synchronized (map) {
170 Iterator<ConnectionsRef> iter = map.values().iterator();
171 Connections conns;
172 while (iter.hasNext()) {
173 conns = iter.next().getConnections();
174 if (conns.expire(threshold)) {
175 d("expire(): removing ", conns);
176 iter.remove();
177 }
178 }
179 }
180 expungeStaleConnections();
181 }
182
183 /*
184 * Closes the connections contained in the ConnectionsRef object that
185 * is going to be reclaimed by the GC. Called by getPooledConnection()
186 * and expire() methods of this class.
187 */
188 private static void expungeStaleConnections() {
189 ConnectionsWeakRef releaseRef = null;
190 while ((releaseRef = (ConnectionsWeakRef) queue.poll())
191 != null) {
192 Connections conns = releaseRef.getConnections();
193
194 if (debug) {
195 System.err.println(
196 "weak reference cleanup: Closing Connections:" + conns);
197 }
198
199 // cleanup
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.jndi.ldap.pool;
27
28 import java.util.ArrayList;
29 import java.util.Map;
30 import java.util.WeakHashMap;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.LinkedList;
34
35 import java.io.PrintStream;
36 import java.lang.ref.Reference;
37 import java.lang.ref.ReferenceQueue;
38 import javax.naming.NamingException;
39
40 /**
41 * A map of pool ids to Connections.
42 * Key is an object that uniquely identifies a PooledConnection request
43 * (typically information needed to create the connection).
44 * The definitions of the key's equals() and hashCode() methods are
45 * vital to its unique identification in a Pool.
46 *
47 * Value is a ConnectionsRef, which is a reference to Connections,
48 * a list of equivalent connections.
49 *
50 * Supports methods that
51 * - retrieves (or creates as necessary) a connection from the pool
52 * - removes expired connections from the pool
149 }
150
151 return conns.get(timeout, factory); // get one connection from list
152 }
153
154 private Connections getConnections(Object id) {
155 ConnectionsRef ref = map.get(id);
156 return (ref != null) ? ref.getConnections() : null;
157 }
158
159 /**
160 * Goes through the connections in this Pool and expires ones that
161 * have been idle before 'threshold'. An expired connection is closed
162 * and then removed from the pool (removePooledConnection() will eventually
163 * be called, and the list of pools itself removed if it becomes empty).
164 *
165 * @param threshold connections idle before 'threshold' should be closed
166 * and removed.
167 */
168 public void expire(long threshold) {
169 Collection<ConnectionsRef> copy;
170 synchronized (map) {
171 copy = new ArrayList<>(map.values());
172 }
173
174 ArrayList<ConnectionsRef> removed = new ArrayList<>();
175 Connections conns;
176 for (ConnectionsRef ref : copy) {
177 conns = ref.getConnections();
178 if (conns.expire(threshold)) {
179 d("expire(): removing ", conns);
180 removed.add(ref);
181 }
182 }
183
184 synchronized (map) {
185 map.values().removeAll(removed);
186 }
187
188 expungeStaleConnections();
189 }
190
191 /*
192 * Closes the connections contained in the ConnectionsRef object that
193 * is going to be reclaimed by the GC. Called by getPooledConnection()
194 * and expire() methods of this class.
195 */
196 private static void expungeStaleConnections() {
197 ConnectionsWeakRef releaseRef = null;
198 while ((releaseRef = (ConnectionsWeakRef) queue.poll())
199 != null) {
200 Connections conns = releaseRef.getConnections();
201
202 if (debug) {
203 System.err.println(
204 "weak reference cleanup: Closing Connections:" + conns);
205 }
206
207 // cleanup
|