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; // JDK 1.2
29 import java.util.List;
30 import java.util.Iterator;
31
32 import java.lang.ref.Reference;
33 import java.lang.ref.SoftReference;
34
35 import javax.naming.NamingException;
36 import javax.naming.InterruptedNamingException;
37 import javax.naming.CommunicationException;
38
39 /**
40 * Represents a list of PooledConnections (actually, ConnectionDescs) with the
41 * same pool id.
42 * The list starts out with an initial number of connections.
43 * Additional PooledConnections are created lazily upon demand.
44 * The list has a maximum size. When the number of connections
45 * reaches the maximum size, a request for a PooledConnection blocks until
46 * a connection is returned to the list. A maximum size of zero means that
47 * there is no maximum: connection creation will be attempted when
48 * no idle connection is available.
49 *
50 * The list may also have a preferred size. If the current list size
274 // Remove softref to make pool entry eligible for GC.
275 // Once ref has been removed, it cannot be reinstated.
276 ref = null;
277 }
278
279 return true;
280 } else {
281 d("remove(): not found ", conn);
282 return false;
283 }
284 }
285
286 /**
287 * Goes through all entries in list, removes and closes ones that have been
288 * idle before threshold.
289 *
290 * @param threshold an entry idle since this time has expired.
291 * @return true if no more connections in list
292 */
293 synchronized boolean expire(long threshold) {
294 Iterator<ConnectionDesc> iter = conns.iterator();
295 ConnectionDesc entry;
296 while (iter.hasNext()) {
297 entry = iter.next();
298 if (entry.expire(threshold)) {
299 d("expire(): removing ", entry);
300 td("Expired ", entry);
301
302 iter.remove(); // remove from pool
303
304 // Don't need to call notify() because we're
305 // removing only idle connections. If there were
306 // idle connections, then there should be no waiters.
307 }
308 }
309 return conns.isEmpty(); // whether whole list has 'expired'
310 }
311
312 /**
313 * Called when this instance of Connections has been removed from Pool.
314 * This means that no one can get any pooled connections from this
315 * Connections any longer. Expire all idle connections as of 'now'
316 * and leave indicator so that any in-use connections will be closed upon
317 * their return.
318 */
319 synchronized void close() {
320 expire(System.currentTimeMillis()); // Expire idle connections
321 closed = true; // Close in-use connections when they are returned
322 }
323
324 String getStats() {
325 int idle = 0;
326 int busy = 0;
327 int expired = 0;
328 long use = 0;
329 int len;
|
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; // JDK 1.2
29 import java.util.List;
30
31 import java.lang.ref.Reference;
32 import java.lang.ref.SoftReference;
33
34 import javax.naming.NamingException;
35 import javax.naming.InterruptedNamingException;
36 import javax.naming.CommunicationException;
37
38 /**
39 * Represents a list of PooledConnections (actually, ConnectionDescs) with the
40 * same pool id.
41 * The list starts out with an initial number of connections.
42 * Additional PooledConnections are created lazily upon demand.
43 * The list has a maximum size. When the number of connections
44 * reaches the maximum size, a request for a PooledConnection blocks until
45 * a connection is returned to the list. A maximum size of zero means that
46 * there is no maximum: connection creation will be attempted when
47 * no idle connection is available.
48 *
49 * The list may also have a preferred size. If the current list size
273 // Remove softref to make pool entry eligible for GC.
274 // Once ref has been removed, it cannot be reinstated.
275 ref = null;
276 }
277
278 return true;
279 } else {
280 d("remove(): not found ", conn);
281 return false;
282 }
283 }
284
285 /**
286 * Goes through all entries in list, removes and closes ones that have been
287 * idle before threshold.
288 *
289 * @param threshold an entry idle since this time has expired.
290 * @return true if no more connections in list
291 */
292 synchronized boolean expire(long threshold) {
293 List<ConnectionDesc> clonedConns;
294 synchronized(this) {
295 clonedConns = new ArrayList<>(conns);
296 }
297 List<ConnectionDesc> expired = new ArrayList<>();
298
299 for (ConnectionDesc entry : clonedConns) {
300 d("expire(): ", entry);
301 if (entry.expire(threshold)) {
302 expired.add(entry);
303 td("expire(): Expired ", entry);
304 }
305 }
306
307 synchronized (this) {
308 conns.removeAll(expired);
309 // Don't need to call notify() because we're
310 // removing only idle connections. If there were
311 // idle connections, then there should be no waiters.
312 return conns.isEmpty(); // whether whole list has 'expired'
313 }
314 }
315
316 /**
317 * Called when this instance of Connections has been removed from Pool.
318 * This means that no one can get any pooled connections from this
319 * Connections any longer. Expire all idle connections as of 'now'
320 * and leave indicator so that any in-use connections will be closed upon
321 * their return.
322 */
323 synchronized void close() {
324 expire(System.currentTimeMillis()); // Expire idle connections
325 closed = true; // Close in-use connections when they are returned
326 }
327
328 String getStats() {
329 int idle = 0;
330 int busy = 0;
331 int expired = 0;
332 long use = 0;
333 int len;
|