FileDocCategorySizeDatePackage
ConnectionPool.javaAPI DocGlassfish v2 API11248Tue May 22 16:54:54 BST 2007oracle.toplink.essentials.threetier

ConnectionPool

public class ConnectionPool extends Object

Purpose: Used to specify how connection should be pooled in a server session.

see
ServerSession

Fields Summary
protected boolean
isConnected
protected int
maxNumberOfConnections
protected int
minNumberOfConnections
protected Vector
connectionsAvailable
protected Vector
connectionsUsed
protected Login
login
protected String
name
protected ServerSession
owner
Constructors Summary
public ConnectionPool()
PUBLIC: A connection pool is used to specify how connection should be pooled in a server session.

        this.maxNumberOfConnections = 50;
        this.minNumberOfConnections = 3;
        resetConnections();
    
public ConnectionPool(String name, Login login, int minNumberOfConnections, int maxNumberOfConnections, ServerSession owner)
PUBLIC: A connection pool is used to specify how connection should be pooled in a server session.

        this.login = login;
        this.owner = owner;
        this.name = name;
        this.maxNumberOfConnections = maxNumberOfConnections;
        this.minNumberOfConnections = minNumberOfConnections;
        resetConnections();
    
Methods Summary
public synchronized oracle.toplink.essentials.internal.databaseaccess.AccessoracquireConnection()
INTERNAL: Wait until a connection is avaiable and allocate the connection for the client.

        while (!hasConnectionAvailable()) {
            if (getTotalNumberOfConnections() < getMaxNumberOfConnections()) {
                Accessor connection = buildConnection();
                getConnectionsUsed().addElement(connection);
                return connection;
            }
            try {
                wait();// Notify is called when connections are released.
            } catch (InterruptedException exception) {
                throw ConcurrencyException.waitFailureOnClientSession(exception);
            }
        }

        Accessor connection = (Accessor)getConnectionsAvailable().firstElement();
        getConnectionsAvailable().removeElement(connection);
        getConnectionsUsed().addElement(connection);

        getOwner().updateProfile(getName(), new Integer(getConnectionsUsed().size()));
        return connection;
    
protected oracle.toplink.essentials.internal.databaseaccess.AccessorbuildConnection()
INTERNAL: Create a new connection, accessors are used as connections.

        Login localLogin = (Login)getLogin().clone();
        Accessor connection = localLogin.buildAccessor();
        connection.connect(localLogin, getOwner());

        return connection;
    
public java.util.VectorgetConnectionsAvailable()
INTERNAL: returns the connections currently available for use in the pool

        return connectionsAvailable;
    
protected java.util.VectorgetConnectionsUsed()
Return a list of the connections that are being used.

return
java.util.Vector

        return connectionsUsed;
    
public oracle.toplink.essentials.sessions.LogingetLogin()
PUBLIC: Return the login used to create connections.

        return login;
    
public intgetMaxNumberOfConnections()
PUBLIC: Return the maximum number of connections allowed. When the max is reached clients must wait for a connection to become available.

        return maxNumberOfConnections;
    
public intgetMinNumberOfConnections()
PUBLIC: Return the minimum number of connections. These connection will be create on startup.

        return minNumberOfConnections;
    
public java.lang.StringgetName()
PUBLIC: Return the name of this pool. Pools are identified by name to allow multiple connection pools.

        return name;
    
protected oracle.toplink.essentials.threetier.ServerSessiongetOwner()
Return the ServerSession that is the owner of this connection pool.

return
oracle.toplink.essentials.threetier.ServerSession

        return owner;
    
public intgetTotalNumberOfConnections()
INTERNAL: Return the total number of connections currently in use.

        return getConnectionsUsed().size() + getConnectionsAvailable().size();
    
public booleanhasConnectionAvailable()
INTERNAL: Wait until a connection is avaiable and allocate the connection for the client.

        return !getConnectionsAvailable().isEmpty();
    
public booleanisConnected()
INTERNAL: Return if this pool has been connected to the database.

        return isConnected;
    
public booleanisThereConflictBetweenLoginAndType()
INTERNAL: Checks for a conflict between pool's type and pool's login

        return getLogin().shouldUseExternalConnectionPooling();
    
public synchronized voidreleaseConnection(oracle.toplink.essentials.internal.databaseaccess.Accessor connection)
INTERNAL: Add the connection as single that a new connection is available.

        getConnectionsUsed().removeElement(connection);

        if (getTotalNumberOfConnections() < getMinNumberOfConnections()) {
            getConnectionsAvailable().addElement(connection);
        } else {
            connection.disconnect(getOwner());
        }

        notify();
    
public voidresetConnections()
INTERNAL: Reset the connections on shutDown and when the pool is started.

        this.connectionsUsed = new Vector();
        this.connectionsAvailable = new Vector();
    
protected voidsetConnectionsAvailable(java.util.Vector connectionsAvailable)
INTERNAL: Set this list of connections available

param
java.util.Vector

        this.connectionsAvailable = connectionsAvailable;
    
protected voidsetConnectionsUsed(java.util.Vector connectionsUsed)
INTERNAL: Set the list of connections being used.

param
java.util.Vector

        this.connectionsUsed = connectionsUsed;
    
public voidsetIsConnected(boolean isConnected)
INTERNAL: Set if this pool has been connected to the database.

        this.isConnected = isConnected;
    
public voidsetLogin(oracle.toplink.essentials.sessions.Login login)
PUBLIC: Set the login used to create connections.

        this.login = login;
    
public voidsetMaxNumberOfConnections(int maxNumberOfConnections)
PUBLIC: Set the maximum number of connections allowed. When the max is reached clients must wait for a connection to become available.

        this.maxNumberOfConnections = maxNumberOfConnections;
    
public voidsetMinNumberOfConnections(int minNumberOfConnections)
PUBLIC: Set the minimum number of connections. These connection will be create on startup.

        this.minNumberOfConnections = minNumberOfConnections;
    
public voidsetName(java.lang.String name)
PUBLIC: Set the name of this pool. Pools are identified by name to allow multiple connection pools.

        this.name = name;
    
protected voidsetOwner(oracle.toplink.essentials.threetier.ServerSession owner)
Set the ServerSession that owns this connection pool

param
oracle.toplink.essentials.threetier.ServerSession

        this.owner = owner;
    
public synchronized voidshutDown()
INTERNAL: Disconnect all connections.

        setIsConnected(false);

        for (Enumeration avaiableEnum = getConnectionsAvailable().elements();
                 avaiableEnum.hasMoreElements();) {
            try {
                ((Accessor)avaiableEnum.nextElement()).disconnect(getOwner());
            } catch (DatabaseException exception) {
                // Ignore.
            }
        }

        for (Enumeration usedEnum = getConnectionsUsed().elements(); usedEnum.hasMoreElements();) {
            try {
                ((Accessor)usedEnum.nextElement()).disconnect(getOwner());
            } catch (DatabaseException exception) {
                // Ignore.
            }
        }
        resetConnections();
    
public synchronized voidstartUp()
INTERNAL: Allocate the minimum connections.

        for (int index = getMinNumberOfConnections(); index > 0; index--) {
            getConnectionsAvailable().addElement(buildConnection());
        }

        setIsConnected(true);
    
public java.lang.StringtoString()
INTERNAL: return a string representation of this connection pool

        Object[] args = { new Integer(getMinNumberOfConnections()), new Integer(getMaxNumberOfConnections()) };
        return Helper.getShortClassName(getClass()) + ToStringLocalization.buildMessage("min_max", args);