oracle.toplink.essentials.threetier.ReadConnectionPool Maven / Gradle / Ivy
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* glassfish/bootstrap/legal/CDDLv1.0.txt or
* https://glassfish.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
// Copyright (c) 1998, 2007, Oracle. All rights reserved.
package oracle.toplink.essentials.threetier;
import java.util.*;
import oracle.toplink.essentials.internal.databaseaccess.*;
import oracle.toplink.essentials.sessions.Login;
import oracle.toplink.essentials.exceptions.*;
/**
*
* Purpose:The read connection pool is used for read access through the server session.
* Any of the connection pools can be used for the read pool however this is the default.
* This pool allows for concurrent reads against the same JDBC connection and requires that
* the JDBC connection support concurrent read access.
*/
public class ReadConnectionPool extends ConnectionPool {
/**
* PUBLIC:
* Build a new read connection pool.
*/
public ReadConnectionPool() {
super();
}
/**
* PUBLIC:
* Build a new read connection pool.
*/
public ReadConnectionPool(String name, Login login, int minNumberOfConnections, int maxNumberOfConnections, ServerSession owner) {
super(name, login, minNumberOfConnections, maxNumberOfConnections, owner);
}
/**
* INTERNAL:
* Wait until a connection is avaiable and allocate the connection for the client.
*/
public synchronized Accessor acquireConnection() throws ConcurrencyException {
Accessor leastBusyConnection = null;
// Search for an unused connection, also find the least busy incase all are used.
for (Enumeration connectionsEnum = getConnectionsAvailable().elements();
connectionsEnum.hasMoreElements();) {
Accessor connection = (Accessor)connectionsEnum.nextElement();
if (connection.getCallCount() == 0) {
connection.incrementCallCount(getOwner());
return connection;
}
if ((leastBusyConnection == null) || (leastBusyConnection.getCallCount() > connection.getCallCount())) {
leastBusyConnection = connection;
}
}
// If still not at max, add a new connection.
if (getTotalNumberOfConnections() < getMaxNumberOfConnections()) {
Accessor connection = buildConnection();
getConnectionsAvailable().addElement(connection);
connection.incrementCallCount(getOwner());
return connection;
}
// Use the least busy connection.
leastBusyConnection.incrementCallCount(getOwner());
return leastBusyConnection;
}
/**
* INTERNAL:
* Concurrent reads are supported.
*/
public boolean hasConnectionAvailable() {
return true;
}
/**
* INTERNAL:
* Because connections are not exclusive nothing is required.
*/
public synchronized void releaseConnection(Accessor connection) throws DatabaseException {
connection.decrementCallCount();
}
}