ConnectionDesc.java revision 2362
14ea4bb737263733ad80a36b4f73f681c30a6b45sd/*
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd *
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * This code is free software; you can redistribute it and/or modify it
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * under the terms of the GNU General Public License version 2 only, as
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * published by the Free Software Foundation. Oracle designates this
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * particular file as subject to the "Classpath" exception as provided
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * by Oracle in the LICENSE file that accompanied this code.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd *
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * This code is distributed in the hope that it will be useful, but WITHOUT
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * version 2 for more details (a copy is included in the LICENSE file that
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * accompanied this code).
14ea4bb737263733ad80a36b4f73f681c30a6b45sd *
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * You should have received a copy of the GNU General Public License version
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * 2 along with this work; if not, write to the Free Software Foundation,
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd *
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * or visit www.oracle.com if you need additional information or have any
6876da76f91687fee15a706830b990a2c0d55157Trang Do * questions.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd */
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
14ea4bb737263733ad80a36b4f73f681c30a6b45sdpackage com.sun.jndi.ldap.pool;
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
14ea4bb737263733ad80a36b4f73f681c30a6b45sd/**
535ec645ca371f1ce298eaf76bf8feb73494f923dduvall * Represents a description of PooledConnection in Connections.
25c6ff4b77fcddf4097ce78a8277275ca603b46cstephh * Contains a PooledConnection, its state (busy, idle, expired), and idle time.
dd566498928f08e7c9a79797a40db893c6a4b9fbvn *
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * Any access or update to a descriptor's state is synchronized.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd *
dd566498928f08e7c9a79797a40db893c6a4b9fbvn * @author Rosanna Lee
dd566498928f08e7c9a79797a40db893c6a4b9fbvn */
14ea4bb737263733ad80a36b4f73f681c30a6b45sdfinal class ConnectionDesc {
dd566498928f08e7c9a79797a40db893c6a4b9fbvn private final static boolean debug = Pool.debug;
dd566498928f08e7c9a79797a40db893c6a4b9fbvn
25c6ff4b77fcddf4097ce78a8277275ca603b46cstephh // Package private because used by Pool.showStats()
dd566498928f08e7c9a79797a40db893c6a4b9fbvn static final byte BUSY = (byte)0;
14ea4bb737263733ad80a36b4f73f681c30a6b45sd static final byte IDLE = (byte)1;
14ea4bb737263733ad80a36b4f73f681c30a6b45sd static final byte EXPIRED = (byte)2;
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
14ea4bb737263733ad80a36b4f73f681c30a6b45sd final private PooledConnection conn;
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
14ea4bb737263733ad80a36b4f73f681c30a6b45sd private byte state = IDLE; // initial state
14ea4bb737263733ad80a36b4f73f681c30a6b45sd private long idleSince;
14ea4bb737263733ad80a36b4f73f681c30a6b45sd private long useCount = 0; // for stats & debugging only
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
14ea4bb737263733ad80a36b4f73f681c30a6b45sd ConnectionDesc(PooledConnection conn) {
14ea4bb737263733ad80a36b4f73f681c30a6b45sd this.conn = conn;
dd566498928f08e7c9a79797a40db893c6a4b9fbvn }
dd566498928f08e7c9a79797a40db893c6a4b9fbvn
25c6ff4b77fcddf4097ce78a8277275ca603b46cstephh ConnectionDesc(PooledConnection conn, boolean use) {
25c6ff4b77fcddf4097ce78a8277275ca603b46cstephh this.conn = conn;
dd566498928f08e7c9a79797a40db893c6a4b9fbvn if (use) {
dd566498928f08e7c9a79797a40db893c6a4b9fbvn state = BUSY;
dd566498928f08e7c9a79797a40db893c6a4b9fbvn ++useCount;
dd566498928f08e7c9a79797a40db893c6a4b9fbvn }
14ea4bb737263733ad80a36b4f73f681c30a6b45sd }
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
14ea4bb737263733ad80a36b4f73f681c30a6b45sd /**
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * Two desc are equal if their PooledConnections are the same.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * This is useful when searching for a ConnectionDesc using only its
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * PooledConnection.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd */
dd566498928f08e7c9a79797a40db893c6a4b9fbvn public boolean equals(Object obj) {
dd566498928f08e7c9a79797a40db893c6a4b9fbvn return obj != null
dd566498928f08e7c9a79797a40db893c6a4b9fbvn && obj instanceof ConnectionDesc
25c6ff4b77fcddf4097ce78a8277275ca603b46cstephh && ((ConnectionDesc)obj).conn == conn;
25c6ff4b77fcddf4097ce78a8277275ca603b46cstephh }
dd566498928f08e7c9a79797a40db893c6a4b9fbvn
dd566498928f08e7c9a79797a40db893c6a4b9fbvn /**
dd566498928f08e7c9a79797a40db893c6a4b9fbvn * Hashcode is that of PooledConnection to facilitate
dd566498928f08e7c9a79797a40db893c6a4b9fbvn * searching for a ConnectionDesc using only its PooledConnection.
dd566498928f08e7c9a79797a40db893c6a4b9fbvn */
dd566498928f08e7c9a79797a40db893c6a4b9fbvn public int hashCode() {
14ea4bb737263733ad80a36b4f73f681c30a6b45sd return conn.hashCode();
14ea4bb737263733ad80a36b4f73f681c30a6b45sd }
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
14ea4bb737263733ad80a36b4f73f681c30a6b45sd /**
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * Changes the state of a ConnectionDesc from BUSY to IDLE and
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * records the current time so that we will know how long it has been idle.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * @return true if state change occurred.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd */
14ea4bb737263733ad80a36b4f73f681c30a6b45sd synchronized boolean release() {
14ea4bb737263733ad80a36b4f73f681c30a6b45sd d("release()");
14ea4bb737263733ad80a36b4f73f681c30a6b45sd if (state == BUSY) {
14ea4bb737263733ad80a36b4f73f681c30a6b45sd state = IDLE;
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
6876da76f91687fee15a706830b990a2c0d55157Trang Do idleSince = System.currentTimeMillis();
6876da76f91687fee15a706830b990a2c0d55157Trang Do return true; // Connection released, ready for reuse
6876da76f91687fee15a706830b990a2c0d55157Trang Do } else {
6876da76f91687fee15a706830b990a2c0d55157Trang Do return false; // Connection wasn't busy to begin with
6876da76f91687fee15a706830b990a2c0d55157Trang Do }
6876da76f91687fee15a706830b990a2c0d55157Trang Do }
6876da76f91687fee15a706830b990a2c0d55157Trang Do
6876da76f91687fee15a706830b990a2c0d55157Trang Do /**
6876da76f91687fee15a706830b990a2c0d55157Trang Do * If ConnectionDesc is IDLE, change its state to BUSY and return
6876da76f91687fee15a706830b990a2c0d55157Trang Do * its connection.
6876da76f91687fee15a706830b990a2c0d55157Trang Do *
6876da76f91687fee15a706830b990a2c0d55157Trang Do * @return ConnectionDesc's PooledConnection if it was idle; null otherwise.
6876da76f91687fee15a706830b990a2c0d55157Trang Do */
6876da76f91687fee15a706830b990a2c0d55157Trang Do synchronized PooledConnection tryUse() {
6876da76f91687fee15a706830b990a2c0d55157Trang Do d("tryUse()");
6876da76f91687fee15a706830b990a2c0d55157Trang Do
6876da76f91687fee15a706830b990a2c0d55157Trang Do if (state == IDLE) {
6876da76f91687fee15a706830b990a2c0d55157Trang Do state = BUSY;
6876da76f91687fee15a706830b990a2c0d55157Trang Do ++useCount;
6876da76f91687fee15a706830b990a2c0d55157Trang Do return conn;
6876da76f91687fee15a706830b990a2c0d55157Trang Do }
6876da76f91687fee15a706830b990a2c0d55157Trang Do
6876da76f91687fee15a706830b990a2c0d55157Trang Do return null;
6876da76f91687fee15a706830b990a2c0d55157Trang Do }
6876da76f91687fee15a706830b990a2c0d55157Trang Do
6876da76f91687fee15a706830b990a2c0d55157Trang Do /**
6876da76f91687fee15a706830b990a2c0d55157Trang Do * If ConnectionDesc is IDLE and has expired, close the corresponding
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * PooledConnection.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd *
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * @param threshold a connection that has been idle before this time
dd566498928f08e7c9a79797a40db893c6a4b9fbvn * have expired.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd *
14ea4bb737263733ad80a36b4f73f681c30a6b45sd * @return true if entry is idle and has expired; false otherwise.
14ea4bb737263733ad80a36b4f73f681c30a6b45sd */
14ea4bb737263733ad80a36b4f73f681c30a6b45sd synchronized boolean expire(long threshold) {
1557e65f9d0c6fde875d807c12fc03ea20f50280vn if (state == IDLE && idleSince < threshold) {
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
dd566498928f08e7c9a79797a40db893c6a4b9fbvn d("expire(): expired");
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
14ea4bb737263733ad80a36b4f73f681c30a6b45sd state = EXPIRED;
14ea4bb737263733ad80a36b4f73f681c30a6b45sd conn.closeConnection(); // Close real connection
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
dd566498928f08e7c9a79797a40db893c6a4b9fbvn return true; // Expiration successful
14ea4bb737263733ad80a36b4f73f681c30a6b45sd } else {
14ea4bb737263733ad80a36b4f73f681c30a6b45sd d("expire(): not expired");
14ea4bb737263733ad80a36b4f73f681c30a6b45sd return false; // Expiration did not occur
dd566498928f08e7c9a79797a40db893c6a4b9fbvn }
dd566498928f08e7c9a79797a40db893c6a4b9fbvn }
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
dd566498928f08e7c9a79797a40db893c6a4b9fbvn public String toString() {
1557e65f9d0c6fde875d807c12fc03ea20f50280vn return conn.toString() + " " +
dd566498928f08e7c9a79797a40db893c6a4b9fbvn (state == BUSY ? "busy" : (state == IDLE ? "idle" : "expired"));
dd566498928f08e7c9a79797a40db893c6a4b9fbvn }
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
14ea4bb737263733ad80a36b4f73f681c30a6b45sd // Used by Pool.showStats()
14ea4bb737263733ad80a36b4f73f681c30a6b45sd int getState() {
14ea4bb737263733ad80a36b4f73f681c30a6b45sd return state;
14ea4bb737263733ad80a36b4f73f681c30a6b45sd }
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
14ea4bb737263733ad80a36b4f73f681c30a6b45sd // Used by Pool.showStats()
14ea4bb737263733ad80a36b4f73f681c30a6b45sd long getUseCount() {
14ea4bb737263733ad80a36b4f73f681c30a6b45sd return useCount;
14ea4bb737263733ad80a36b4f73f681c30a6b45sd }
14ea4bb737263733ad80a36b4f73f681c30a6b45sd
dd566498928f08e7c9a79797a40db893c6a4b9fbvn private void d(String msg) {
14ea4bb737263733ad80a36b4f73f681c30a6b45sd if (debug) {
dd566498928f08e7c9a79797a40db893c6a4b9fbvn System.err.println("ConnectionDesc." + msg + " " + toString());
14ea4bb737263733ad80a36b4f73f681c30a6b45sd }
dd566498928f08e7c9a79797a40db893c6a4b9fbvn }
14ea4bb737263733ad80a36b4f73f681c30a6b45sd}
dd566498928f08e7c9a79797a40db893c6a4b9fbvn