org.ldaptive.pool
Class AbstractConnectionPool

java.lang.Object
  extended by org.ldaptive.pool.AbstractPool<Connection>
      extended by org.ldaptive.pool.AbstractConnectionPool
All Implemented Interfaces:
ConnectionPool
Direct Known Subclasses:
BlockingConnectionPool

public abstract class AbstractConnectionPool
extends AbstractPool<Connection>
implements ConnectionPool

Contains the base implementation for pooling connections. The main design objective for the supplied pooling implementations is to provide a pool that does not block on connection creation or destruction. This is what accounts for the multiple locks available on this class. The pool is backed by two queues, one for available connections and one for active connections. Connections that are available via getConnection() exist in the available queue. Connections that are actively in use exist in the active queue. This implementation uses FIFO operations for each queue.

Version:
$Revision: 3120 $ $Date: 2015-10-01 11:50:02 -0400 (Thu, 01 Oct 2015) $
Author:
Middleware Services

Nested Class Summary
protected  class AbstractConnectionPool.DefaultPooledConnectionProxy
          Contains a connection that is participating in this pool.
 
Field Summary
protected  Queue<PooledConnectionProxy> active
          List of connections in use.
protected  Queue<PooledConnectionProxy> available
          List of available connections in the pool.
protected  ReentrantLock checkInLock
          Lock for check ins.
protected  ReentrantLock checkOutLock
          Lock for check outs.
protected  ReentrantLock poolLock
          Lock for the entire pool.
protected  Condition poolNotEmpty
          Condition for notifying threads that a connection was returned.
 
Fields inherited from class org.ldaptive.pool.AbstractPool
logger
 
Constructor Summary
AbstractConnectionPool()
           
 
Method Summary
protected  void activateAndValidateConnection(PooledConnectionProxy pc)
          Attempts to activate and validate a connection.
 int activeCount()
          Returns the number of connections in use.
 int availableCount()
          Returns the number of connections available for use.
 void close()
          Empty this pool, freeing any resources.
protected  PooledConnectionProxy createActiveConnection()
          Create a new connection and place it in the active pool.
protected  PooledConnectionProxy createActiveConnection(boolean throwOnFailure)
          Create a new connection and place it in the active pool.
protected  PooledConnectionProxy createAvailableConnection()
          Create a new connection and place it in the available pool.
protected  PooledConnectionProxy createAvailableConnection(boolean throwOnFailure)
          Create a new connection and place it in the available pool.
protected  PooledConnectionProxy createConnection()
          Create a new connection.
protected  PooledConnectionProxy createConnection(boolean throwOnFailure)
          Create a new connection.
protected  Connection createConnectionProxy(PooledConnectionProxy pc)
          Creates a connection proxy using the supplied pool connection.
protected  void finalize()
          Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
abstract  Connection getConnection()
          Returns a connection from the pool.
 DefaultConnectionFactory getConnectionFactory()
          Returns the connection factory for this pool.
 boolean getConnectOnCreate()
          Returns whether connections will attempt to connect after creation.
 boolean getFailFastInitialize()
          Returns whether initialize() should throw if pooling configuration requirements are not met.
 Set<PooledConnectionStatistics> getPooledConnectionStatistics()
          Returns the statistics for each connection in the pool.
 QueueType getQueueType()
          Returns the type of queue used for this connection pool.
protected  void grow(int size)
          Attempts to grow the pool to the supplied size.
protected  void grow(int size, boolean throwOnFailure)
          Attempts to grow the pool to the supplied size.
 void initialize()
          Initialize this pool for use.
protected  void isInitialized()
          Used to determine whether initialize() has been invoked for this pool.
 void prune()
          Attempts to reduce the size of the pool back to it's configured minimum.
abstract  void putConnection(Connection c)
          Returns a connection to the pool.
protected  void removeActiveConnection(PooledConnectionProxy pc)
          Remove a connection from the active pool.
protected  void removeAvailableAndActiveConnection(PooledConnectionProxy pc)
          Remove a connection from both the available and active pools.
protected  void removeAvailableConnection(PooledConnectionProxy pc)
          Remove a connection from the available pool.
protected  PooledConnectionProxy retrieveConnectionProxy(Connection proxy)
          Retrieves the invocation handler from the supplied connection proxy.
 void setConnectionFactory(DefaultConnectionFactory cf)
          Sets the connection factory for this pool.
 void setConnectOnCreate(boolean b)
          Sets whether newly created connections will attempt to connect.
 void setFailFastInitialize(boolean b)
          Sets whether initialize() should throw if pooling configuration requirements are not met.
 void setQueueType(QueueType type)
          Sets the type of queue used for this connection pool.
 String toString()
          
 void validate()
          Attempts to validate all objects in the pool.
protected  boolean validateAndPassivateConnection(PooledConnectionProxy pc)
          Attempts to validate and passivate a connection.
 
Methods inherited from class org.ldaptive.pool.AbstractPool
activate, getActivator, getName, getPassivator, getPoolConfig, getPruneStrategy, getValidator, passivate, setActivator, setName, setPassivator, setPoolConfig, setPruneStrategy, setValidator, validate
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.ldaptive.pool.ConnectionPool
getActivator, getPassivator, getValidator, setActivator, setPassivator, setValidator
 

Field Detail

poolLock

protected final ReentrantLock poolLock
Lock for the entire pool.


poolNotEmpty

protected final Condition poolNotEmpty
Condition for notifying threads that a connection was returned.


checkInLock

protected final ReentrantLock checkInLock
Lock for check ins.


checkOutLock

protected final ReentrantLock checkOutLock
Lock for check outs.


available

protected Queue<PooledConnectionProxy> available
List of available connections in the pool.


active

protected Queue<PooledConnectionProxy> active
List of connections in use.

Constructor Detail

AbstractConnectionPool

public AbstractConnectionPool()
Method Detail

getConnectionFactory

public DefaultConnectionFactory getConnectionFactory()
Returns the connection factory for this pool.

Returns:
connection factory

setConnectionFactory

public void setConnectionFactory(DefaultConnectionFactory cf)
Sets the connection factory for this pool.

Parameters:
cf - connection factory

getConnectOnCreate

public boolean getConnectOnCreate()
Returns whether connections will attempt to connect after creation. Default is true.

Returns:
whether connections will attempt to connect after creation

setConnectOnCreate

public void setConnectOnCreate(boolean b)
Sets whether newly created connections will attempt to connect. Default is true.

Parameters:
b - connect on create

getQueueType

public QueueType getQueueType()
Returns the type of queue used for this connection pool.

Returns:
queue type

setQueueType

public void setQueueType(QueueType type)
Sets the type of queue used for this connection pool. This property may have an impact on the success of the prune strategy.

Parameters:
type - of queue

getFailFastInitialize

public boolean getFailFastInitialize()
Returns whether initialize() should throw if pooling configuration requirements are not met.

Returns:
whether initialize() should throw

setFailFastInitialize

public void setFailFastInitialize(boolean b)
Sets whether initialize() should throw if pooling configuration requirements are not met.

Parameters:
b - whether initialize() should throw

isInitialized

protected void isInitialized()
Used to determine whether initialize() has been invoked for this pool.

Throws:
IllegalStateException - if this pool has not been initialized

initialize

public void initialize()
Initialize this pool for use. Once invoked the pool config is made immutable. See AbstractConfig.makeImmutable().

Specified by:
initialize in interface ConnectionPool
Throws:
IllegalStateException - if this pool has already been initialized, the pooling configuration is inconsistent or the pool does not contain at least one connection and it's minimum size is greater than zero

grow

protected void grow(int size)
Attempts to grow the pool to the supplied size. If the pool size is greater than or equal to the supplied size, this method is a no-op.

Parameters:
size - to grow the pool to

grow

protected void grow(int size,
                    boolean throwOnFailure)
Attempts to grow the pool to the supplied size. If the pool size is greater than or equal to the supplied size, this method is a no-op.

Parameters:
size - to grow the pool to
throwOnFailure - whether to throw illegal state exception
Throws:
IllegalStateException - if the pool cannot grow to the supplied size and createAvailableConnection(boolean) throws

close

public void close()
Empty this pool, freeing any resources.

Specified by:
close in interface ConnectionPool
Throws:
IllegalStateException - if this pool has not been initialized

getConnection

public abstract Connection getConnection()
                                  throws PoolException
Returns a connection from the pool.

Specified by:
getConnection in interface ConnectionPool
Returns:
connection
Throws:
PoolException - if this operation fails
BlockingTimeoutException - if this pool is configured with a block time and it occurs
PoolInterruptedException - if this pool is configured with a block time and the current thread is interrupted
IllegalStateException - if this pool has not been initialized

putConnection

public abstract void putConnection(Connection c)
Returns a connection to the pool.

Parameters:
c - connection
Throws:
IllegalStateException - if this pool has not been initialized

createConnection

protected PooledConnectionProxy createConnection()
Create a new connection. If connectOnCreate is true, the connection will be opened.

Returns:
pooled connection

createConnection

protected PooledConnectionProxy createConnection(boolean throwOnFailure)
Create a new connection. If connectOnCreate is true, the connection will be opened.

Parameters:
throwOnFailure - whether to throw illegal state exception
Returns:
pooled connection
Throws:
IllegalStateException - if connectOnCreate is true and the connection cannot be opened

createAvailableConnection

protected PooledConnectionProxy createAvailableConnection()
Create a new connection and place it in the available pool.

Returns:
connection that was placed in the available pool

createAvailableConnection

protected PooledConnectionProxy createAvailableConnection(boolean throwOnFailure)
Create a new connection and place it in the available pool.

Parameters:
throwOnFailure - whether to throw illegal state exception
Returns:
connection that was placed in the available pool
Throws:
IllegalStateException - if createConnection(boolean) throws

createActiveConnection

protected PooledConnectionProxy createActiveConnection()
Create a new connection and place it in the active pool.

Returns:
connection that was placed in the active pool

createActiveConnection

protected PooledConnectionProxy createActiveConnection(boolean throwOnFailure)
Create a new connection and place it in the active pool.

Parameters:
throwOnFailure - whether to throw illegal state exception
Returns:
connection that was placed in the active pool
Throws:
IllegalStateException - if createConnection(boolean) throws

removeAvailableConnection

protected void removeAvailableConnection(PooledConnectionProxy pc)
Remove a connection from the available pool.

Parameters:
pc - connection that is in the available pool

removeActiveConnection

protected void removeActiveConnection(PooledConnectionProxy pc)
Remove a connection from the active pool.

Parameters:
pc - connection that is in the active pool

removeAvailableAndActiveConnection

protected void removeAvailableAndActiveConnection(PooledConnectionProxy pc)
Remove a connection from both the available and active pools.

Parameters:
pc - connection that is in both the available and active pools

activateAndValidateConnection

protected void activateAndValidateConnection(PooledConnectionProxy pc)
                                      throws PoolException
Attempts to activate and validate a connection. Performed before a connection is returned from getConnection().

Parameters:
pc - connection
Throws:
PoolException - if this method fails
ActivationException - if the connection cannot be activated
ValidationException - if the connection cannot be validated

validateAndPassivateConnection

protected boolean validateAndPassivateConnection(PooledConnectionProxy pc)
Attempts to validate and passivate a connection. Performed when a connection is given to putConnection(Connection).

Parameters:
pc - connection
Returns:
whether both validate and passivation succeeded

prune

public void prune()
Attempts to reduce the size of the pool back to it's configured minimum. PoolConfig.setMinPoolSize(int).

Throws:
IllegalStateException - if this pool has not been initialized

validate

public void validate()
Attempts to validate all objects in the pool. PoolConfig.setValidatePeriodically(boolean).

Throws:
IllegalStateException - if this pool has not been initialized

availableCount

public int availableCount()
Returns the number of connections available for use.

Specified by:
availableCount in interface ConnectionPool
Returns:
count

activeCount

public int activeCount()
Returns the number of connections in use.

Specified by:
activeCount in interface ConnectionPool
Returns:
count

getPooledConnectionStatistics

public Set<PooledConnectionStatistics> getPooledConnectionStatistics()
Returns the statistics for each connection in the pool.

Specified by:
getPooledConnectionStatistics in interface ConnectionPool
Returns:
connection statistics

createConnectionProxy

protected Connection createConnectionProxy(PooledConnectionProxy pc)
Creates a connection proxy using the supplied pool connection.

Parameters:
pc - pool connection to create proxy with
Returns:
connection proxy

retrieveConnectionProxy

protected PooledConnectionProxy retrieveConnectionProxy(Connection proxy)
Retrieves the invocation handler from the supplied connection proxy.

Parameters:
proxy - connection proxy
Returns:
pooled connection proxy

finalize

protected void finalize()
                 throws Throwable
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Overrides:
finalize in class Object
Throws:
Throwable - if an exception is thrown by this method

toString

public String toString()

Overrides:
toString in class Object


Copyright © 2003-2015 Virginia Tech. All Rights Reserved.