/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Represents a list of PooledConnections (actually, ConnectionDescs) with the
* same pool id.
* The list starts out with an initial number of connections.
* Additional PooledConnections are created lazily upon demand.
* The list has a maximum size. When the number of connections
* reaches the maximum size, a request for a PooledConnection blocks until
* a connection is returned to the list. A maximum size of zero means that
* there is no maximum: connection creation will be attempted when
* no idle connection is available.
*
* The list may also have a preferred size. If the current list size
* is less than the preferred size, a request for a connection will result in
* a PooledConnection being created (even if an idle connection is available).
* If the current list size is greater than the preferred size,
* a connection being returned to the list will be closed and removed from
* the list. A preferred size of zero means that there is no preferred size:
* connections are created only when no idle connection is available and
* a connection being returned to the list is not closed. Regardless of the
* preferred size, connection creation always observes the maximum size:
* a connection won't be created if the list size is at or exceeds the
* maximum size.
*
* @author Rosanna Lee
*/
// Package private: accessed only by Pool
private static final boolean trace =
final private int maxSize;
final private int prefSize;
/**
* @param id the identity (connection request) of the connections in the list
* @param initSize the number of connections to create initially
* @param prefSize the preferred size of the pool. The pool will try
* to maintain a pool of this size by creating and closing connections
* as needed.
* @param maxSize the maximum size of the pool. The pool will not exceed
* this size. If the pool is at this size, a request for a connection
* will block until an idle connection is released to the pool or
* when one is removed.
* @param factory The factory responsible for creating a connection
*/
if (maxSize > 0) {
// prefSize and initSize cannot exceed specified maxSize
} else {
}
// Maintain soft ref to id so that this Connections' entry in
// Pool doesn't get GC'ed prematurely
d("init size=", initSize);
d("max size=", maxSize);
d("preferred size=", prefSize);
// Create initial connections
for (int i = 0; i < initSize; i++) {
}
}
/**
* Retrieves a PooledConnection from this list of connections.
* Use an existing one if one is idle, or create one if the list's
* max size hasn't been reached. If max size has been reached, wait
* for a PooledConnection to be returned, or one to be removed (thus
* not reaching the max size any longer).
*
* @param timeout if > 0, msec to wait until connection is available
* @param factory creates the PooledConnection if one needs to be created
*
* @return A non-null PooledConnection
* @throws NamingException PooledConnection cannot be created, because this
* thread was interrupted while it waited for an available connection,
* or if it timed out while waiting, or the creation of a connection
* resulted in an error.
*/
d("get(): before");
throw new CommunicationException(
"Timeout exceeded while waiting for a connection: " +
timeout + "ms");
}
try {
d("get(): waiting");
if (waittime > 0) {
} else {
wait();
}
} catch (InterruptedException e) {
throw new InterruptedNamingException(
"Interrupted while waiting for a connection");
}
// Check whether we timed out
if (timeout > 0) {
}
}
d("get(): after");
return conn;
}
/**
* Retrieves an idle connection from this list if one is available.
* If none is available, create a new one if maxSize hasn't been reached.
* If maxSize has been reached, return null.
* Always called from a synchronized method.
*/
// If no prefSize specified, or list size already meets or
// exceeds prefSize, then first look for an idle connection
for (int i = 0; i < size; i++) {
d("get(): use ", conn);
return conn;
}
}
}
// Check if list size already at maxSize specified
return null; // List size is at limit; cannot create any more
}
return conn;
}
/**
* Releases connection back into list.
* If the list size is below prefSize, the connection may be reused.
* If the list size exceeds prefSize, then the connection is closed
* and removed from the list.
*
* public because implemented as part of PoolCallback.
*/
d("release(): ", conn);
if (loc >= 0) {
// Found entry
// If list size exceeds prefSize, close connection
d("release(): closing ", conn);
// size must be >= 2 so don't worry about empty list
} else {
d("release(): release ", conn);
// Get ConnectionDesc from list to get correct state info
// Return connection to list, ready for reuse
}
notifyAll();
d("release(): notify");
return true;
} else {
return false;
}
}
/**
* Removes PooledConnection from list of connections.
* The closing of the connection is separate from this method.
* This method is called usually when the caller encouters an error
* when using the connection and wants it removed from the pool.
*
* @return true if conn removed; false if it was not in pool
*
* public because implemented as part of PoolCallback.
*/
d("remove(): ", conn);
notifyAll();
d("remove(): notify");
// Remove softref to make pool entry eligible for GC.
// Once ref has been removed, it cannot be reinstated.
}
return true;
} else {
d("remove(): not found ", conn);
return false;
}
}
/**
* Goes through all entries in list, removes and closes ones that have been
* idle before threshold.
*
* @param threshold an entry idle since this time has expired.
* @return true if no more connections in list
*/
d("expire(): removing ", entry);
// Don't need to call notify() because we're
// removing only idle connections. If there were
// idle connections, then there should be no waiters.
}
}
}
/**
* Called when this instance of Connections has been removed from Pool.
* This means that no one can get any pooled connections from this
* Connections any longer. Expire all idle connections as of 'now'
* and leave indicator so that any in-use connections will be closed upon
* their return.
*/
synchronized void close() {
closed = true; // Close in-use connections when they are returned
}
int idle = 0;
int busy = 0;
int expired = 0;
long use = 0;
int len;
synchronized (this) {
for (int i = 0; i < len; i++) {
case ConnectionDesc.BUSY:
++busy;
break;
case ConnectionDesc.IDLE:
++idle;
break;
case ConnectionDesc.EXPIRED:
++expired;
}
}
}
}
if (debug) {
}
}
if (debug) {
d(msg + i);
}
}
if (debug) {
}
}
if (trace) { // redo test to avoid object creation
}
}
if (trace) { // redo test to avoid object creation
}
}
if (trace) {
}
}
}