Class AbstractConnectionPool

  • All Implemented Interfaces:
    ConnectionPool
    Direct Known Subclasses:
    BlockingConnectionPool

    public abstract class AbstractConnectionPool
    extends Object
    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.
    • Field Detail

      • DEFAULT_MIN_POOL_SIZE

        public static final int DEFAULT_MIN_POOL_SIZE
        Default min pool size, value is 3.
        See Also:
        Constant Field Values
      • DEFAULT_MAX_POOL_SIZE

        public static final int DEFAULT_MAX_POOL_SIZE
        Default max pool size, value is 10.
        See Also:
        Constant Field Values
      • logger

        protected final Logger logger
        Logger for this class.
      • poolLock

        protected final ReentrantLock poolLock
        Lock for the entire pool.
      • poolNotEmpty

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

        protected final ReentrantLock checkOutLock
        Lock for check outs.
      • name

        private String name
        Pool name.
      • minPoolSize

        private int minPoolSize
        Minimum pool size.
      • maxPoolSize

        private int maxPoolSize
        Maximum pool size.
      • validateOnCheckIn

        private boolean validateOnCheckIn
        Whether the ldap connection should be validated when returned to the pool.
      • validateOnCheckOut

        private boolean validateOnCheckOut
        Whether the ldap connection should be validated when given from the pool.
      • validatePeriodically

        private boolean validatePeriodically
        Whether the pool should be validated periodically.
      • pruneStrategy

        private PruneStrategy pruneStrategy
        For removing connections.
      • connectOnCreate

        private boolean connectOnCreate
        Whether to connect to the ldap on connection creation.
      • queueType

        private QueueType queueType
        Type of queue. LIFO or FIFO.
      • initialized

        private boolean initialized
        Whether initialize() has been successfully invoked.
      • failFastInitialize

        private boolean failFastInitialize
        Whether initialize() should throw if pooling configuration requirements are not met.
    • Constructor Detail

      • AbstractConnectionPool

        public AbstractConnectionPool()
    • Method Detail

      • getName

        public String getName()
        Returns the name for this pool.
        Returns:
        pool name
      • setName

        public void setName​(String s)
        Sets the name for this pool.
        Parameters:
        s - pool name
      • getMinPoolSize

        public int getMinPoolSize()
        Returns the min pool size. Default value is DEFAULT_MIN_POOL_SIZE. This value represents the size of the pool after a prune has occurred.
        Returns:
        min pool size
      • setMinPoolSize

        public void setMinPoolSize​(int size)
        Sets the min pool size.
        Parameters:
        size - min pool size
      • getMaxPoolSize

        public int getMaxPoolSize()
        Returns the max pool size. Default value is DEFAULT_MAX_POOL_SIZE. This value may or may not be strictly enforced depending on the pooling implementation.
        Returns:
        max pool size
      • setMaxPoolSize

        public void setMaxPoolSize​(int size)
        Sets the max pool size.
        Parameters:
        size - max pool size
      • isValidateOnCheckIn

        public boolean isValidateOnCheckIn()
        Returns the validate on check in flag.
        Returns:
        validate on check in
      • setValidateOnCheckIn

        public void setValidateOnCheckIn​(boolean b)
        Sets the validate on check in flag.
        Parameters:
        b - validate on check in
      • isValidateOnCheckOut

        public boolean isValidateOnCheckOut()
        Returns the validate on check out flag.
        Returns:
        validate on check in
      • setValidateOnCheckOut

        public void setValidateOnCheckOut​(boolean b)
        Sets the validate on check out flag.
        Parameters:
        b - validate on check out
      • isValidatePeriodically

        public boolean isValidatePeriodically()
        Returns the validate periodically flag.
        Returns:
        validate periodically
      • setValidatePeriodically

        public void setValidatePeriodically​(boolean b)
        Sets the validate periodically flag.
        Parameters:
        b - validate periodically
      • getValidator

        public ConnectionValidator getValidator()
        Returns the connection validator for this pool.
        Returns:
        connection validator
      • setValidator

        public void setValidator​(ConnectionValidator cv)
        Sets the connection validator for this pool.
        Parameters:
        cv - connection validator
      • getPruneStrategy

        public PruneStrategy getPruneStrategy()
        Returns the prune strategy for this pool.
        Returns:
        prune strategy
      • setPruneStrategy

        public void setPruneStrategy​(PruneStrategy ps)
        Sets the prune strategy for this pool.
        Parameters:
        ps - prune strategy
      • getDefaultConnectionFactory

        public DefaultConnectionFactory getDefaultConnectionFactory()
        Returns the connection factory for this pool.
        Returns:
        connection factory
      • setDefaultConnectionFactory

        public void setDefaultConnectionFactory​(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

        public boolean isInitialized()
        Returns whether this pool has been initialized.
        Returns:
        whether this pool has been initialized
      • throwIfNotInitialized

        protected void throwIfNotInitialized()
        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.
        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
      • 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​(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 or null
        Throws:
        IllegalStateException - if connectOnCreate is true and the connection cannot be opened
      • 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​(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 activation or validation fails
      • 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.
        Throws:
        IllegalStateException - if this pool has not been initialized
      • validate

        public void validate()
        Attempts to validate all connections in the pool.
        Throws:
        IllegalStateException - if this pool has not been initialized
      • availableCount

        public int availableCount()
        Description copied from interface: ConnectionPool
        Returns the number of connections available for use.
        Specified by:
        availableCount in interface ConnectionPool
        Returns:
        count
      • activeCount

        public int activeCount()
        Description copied from interface: ConnectionPool
        Returns the number of connections in use.
        Specified by:
        activeCount in interface ConnectionPool
        Returns:
        count
      • 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