Package org.ldaptive.pool
Class AbstractConnectionPool
- java.lang.Object
-
- org.ldaptive.AbstractFreezable
-
- org.ldaptive.pool.AbstractConnectionPool
-
- All Implemented Interfaces:
Freezable
,ConnectionPool
- Direct Known Subclasses:
BlockingConnectionPool
public abstract class AbstractConnectionPool extends AbstractFreezable 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 viagetConnection()
exist in the available queue. Connections that are actively in use exist in the active queue. This implementation uses FIFO operations for each queue.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
AbstractConnectionPool.DefaultPooledConnectionProxy
Contains a connection that is participating in this pool.
-
Field Summary
Fields Modifier and Type Field Description private ConnectionActivator
activator
For activating connections.protected Queue<PooledConnectionProxy>
active
List of connections in use.protected Queue<PooledConnectionProxy>
available
List of available connections in the pool.protected ReentrantLock
checkOutLock
Lock for check outs.private DefaultConnectionFactory
connectionFactory
Connection factory to create connections with.private boolean
connectOnCreate
Whether to connect to the ldap on connection creation.static int
DEFAULT_MAX_POOL_SIZE
Default max pool size, value is 10.static int
DEFAULT_MIN_POOL_SIZE
Default min pool size, value is 3.private boolean
failFastInitialize
Whetherinitialize()
should throw if pooling configuration requirements are not met.private boolean
initialized
Whetherinitialize()
has been successfully invoked.protected Logger
logger
Logger for this class.private int
maxPoolSize
Maximum pool size.private int
minPoolSize
Minimum pool size.private String
name
Pool name.private ConnectionPassivator
passivator
For passivating connections.private static AtomicInteger
POOL_ID
ID used for pool name.private ScheduledExecutorService
poolExecutor
Executor for scheduling pool tasks.protected ReentrantLock
poolLock
Lock for the entire pool.protected Condition
poolNotEmpty
Condition for notifying threads that a connection was returned.private PruneStrategy
pruneStrategy
For removing connections.private QueueType
queueType
Type of queue.private boolean
validateOnCheckIn
Whether the ldap connection should be validated when returned to the pool.private boolean
validateOnCheckOut
Whether the ldap connection should be validated when given from the pool.private boolean
validatePeriodically
Whether the pool should be validated periodically.private ConnectionValidator
validator
For validating connections.
-
Constructor Summary
Constructors Constructor Description AbstractConnectionPool()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description 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.private void
closeAllConnections()
Closes all connections in the pool.protected PooledConnectionProxy
createActiveConnection(boolean throwOnFailure)
Create a new connection and place it in the active queue.protected PooledConnectionProxy
createAvailableConnection(boolean throwOnFailure)
Create a new connection and place it in the available pool.protected void
createAvailableConnections(int count, boolean throwOnFailure)
Asynchronously creates new connections and adds them to the available queue if the connection can be successfully passivated and validated.protected PooledConnectionProxy
createConnection(boolean throwOnFailure)
Create a new connection.protected Connection
createConnectionProxy(PooledConnectionProxy pc)
Creates a connection proxy using the supplied pool connection.void
freeze()
Freezes this object, making it immutable.ConnectionActivator
getActivator()
Returns the activator for this pool.abstract Connection
getConnection()
Returns a connection from the pool.boolean
getConnectOnCreate()
Returns whether connections will attempt to connect after creation.DefaultConnectionFactory
getDefaultConnectionFactory()
Returns the connection factory for this pool.boolean
getFailFastInitialize()
Returns whetherinitialize()
should throw if pooling configuration requirements are not met.int
getMaxPoolSize()
Returns the max pool size.int
getMinPoolSize()
Returns the min pool size.String
getName()
Returns the name for this pool.ConnectionPassivator
getPassivator()
Returns the passivator for this pool.Set<PooledConnectionStatistics>
getPooledConnectionStatistics()
Returns the statistics for each connection in the pool.PruneStrategy
getPruneStrategy()
Returns the prune strategy for this pool.QueueType
getQueueType()
Returns the type of queue used for this connection pool.ConnectionValidator
getValidator()
Returns the connection validator for this pool.protected void
grow(int size, boolean throwOnFailure)
Attempts to grow the pool to the supplied size.void
initialize()
Initialize this pool for use.boolean
isInitialized()
Returns whether this pool has been initialized.boolean
isValidateOnCheckIn()
Returns the validate on check in flag.boolean
isValidateOnCheckOut()
Returns the validate on check out flag.boolean
isValidatePeriodically()
Returns the validate periodically flag.protected boolean
passivateAndValidateConnection(PooledConnectionProxy pc)
Attempts to passivate and validate a connection.void
prune()
Attempts to reduce the size of the pool back to its 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
setActivator(ConnectionActivator a)
Sets the activator for this pool.void
setConnectOnCreate(boolean b)
Sets whether newly created connections will attempt to connect.void
setDefaultConnectionFactory(DefaultConnectionFactory cf)
Sets the connection factory for this pool.void
setFailFastInitialize(boolean b)
Sets whetherinitialize()
should throw if pooling configuration requirements are not met.void
setMaxPoolSize(int size)
Sets the max pool size.void
setMinPoolSize(int size)
Sets the min pool size.void
setName(String s)
Sets the name for this pool.void
setPassivator(ConnectionPassivator p)
Sets the passivator for this pool.void
setPruneStrategy(PruneStrategy ps)
Sets the prune strategy for this pool.void
setQueueType(QueueType type)
Sets the type of queue used for this connection pool.void
setValidateOnCheckIn(boolean b)
Sets the validate on check in flag.void
setValidateOnCheckOut(boolean b)
Sets the validate on check out flag.void
setValidatePeriodically(boolean b)
Sets the validate periodically flag.void
setValidator(ConnectionValidator cv)
Sets the connection validator for this pool.protected void
throwIfNotInitialized()
Used to determine whetherinitialize()
has been invoked for this pool.String
toString()
void
validate()
Attempts to validate all connections in the pool.-
Methods inherited from class org.ldaptive.AbstractFreezable
assertMutable, freeze, freeze, isFrozen
-
-
-
-
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
-
POOL_ID
private static final AtomicInteger POOL_ID
ID used for pool name.
-
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.
-
available
protected Queue<PooledConnectionProxy> available
List of available connections in the pool.
-
active
protected Queue<PooledConnectionProxy> active
List of connections in use.
-
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.
-
activator
private ConnectionActivator activator
For activating connections.
-
passivator
private ConnectionPassivator passivator
For passivating connections.
-
validator
private ConnectionValidator validator
For validating connections.
-
pruneStrategy
private PruneStrategy pruneStrategy
For removing connections.
-
connectionFactory
private DefaultConnectionFactory connectionFactory
Connection factory to create connections with.
-
connectOnCreate
private boolean connectOnCreate
Whether to connect to the ldap on connection creation.
-
queueType
private QueueType queueType
Type of queue. LIFO or FIFO.
-
poolExecutor
private ScheduledExecutorService poolExecutor
Executor for scheduling pool tasks.
-
initialized
private boolean initialized
Whetherinitialize()
has been successfully invoked.
-
failFastInitialize
private boolean failFastInitialize
Whetherinitialize()
should throw if pooling configuration requirements are not met.
-
-
Method Detail
-
freeze
public void freeze()
Description copied from interface:Freezable
Freezes this object, making it immutable.- Specified by:
freeze
in interfaceFreezable
- Overrides:
freeze
in classAbstractFreezable
-
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 isDEFAULT_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, greater than or equal to zero
-
getMaxPoolSize
public int getMaxPoolSize()
Returns the max pool size. Default value isDEFAULT_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, greater than or equal to zero
-
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
-
getActivator
public ConnectionActivator getActivator()
Returns the activator for this pool.- Returns:
- activator
-
setActivator
public void setActivator(ConnectionActivator a)
Sets the activator for this pool.- Parameters:
a
- activator
-
getPassivator
public ConnectionPassivator getPassivator()
Returns the passivator for this pool.- Returns:
- passivator
-
setPassivator
public void setPassivator(ConnectionPassivator p)
Sets the passivator for this pool.- Parameters:
p
- passivator
-
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 whetherinitialize()
should throw if pooling configuration requirements are not met.- Returns:
- whether
initialize()
should throw
-
setFailFastInitialize
public void setFailFastInitialize(boolean b)
Sets whetherinitialize()
should throw if pooling configuration requirements are not met.- Parameters:
b
- whetherinitialize()
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 whetherinitialize()
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 interfaceConnectionPool
- 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 its minimum size is greater than zero
-
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 tothrowOnFailure
- whether to throw illegal state exception- Throws:
IllegalStateException
- if the pool cannot grow to the supplied size andcreateAvailableConnection(boolean)
throws
-
close
public void close()
Empty this pool, freeing any resources.- Specified by:
close
in interfaceConnectionPool
- Throws:
IllegalStateException
- if this pool has not been initialized
-
closeAllConnections
private void closeAllConnections()
Closes all connections in the pool.
-
getConnection
public abstract Connection getConnection() throws PoolException
Returns a connection from the pool.- Specified by:
getConnection
in interfaceConnectionPool
- Returns:
- connection
- Throws:
PoolException
- if this operation failsBlockingTimeoutException
- if this pool is configured with a block time and it occursIllegalStateException
- 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(boolean throwOnFailure)
Create a new connection. IfconnectOnCreate
is true, the connection will be opened.- Parameters:
throwOnFailure
- whether to throw illegal state exception- Returns:
- pooled connection or null
- Throws:
IllegalStateException
- ifconnectOnCreate
is true and the connection cannot be opened
-
createAvailableConnections
protected void createAvailableConnections(int count, boolean throwOnFailure)
Asynchronously creates new connections and adds them to the available queue if the connection can be successfully passivated and validated. SeepassivateAndValidateConnection(PooledConnectionProxy)
. This method can make up to (count * 2) attempts in a best effort to create the number of connections requested.- Parameters:
count
- number of connections to attempt to createthrowOnFailure
- whether to throw illegal state exception on any connection creation failure- Throws:
IllegalStateException
- if throwOnFailure is true and count connections are not successfully created
-
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
- ifcreateConnection(boolean)
throws
-
createActiveConnection
protected PooledConnectionProxy createActiveConnection(boolean throwOnFailure)
Create a new connection and place it in the active queue. This method creates the connection and then attempts to acquire the pool lock in order to add the connection to the active queue. Therefore, this method can be invoked both with and without acquiring the pool lock.- Parameters:
throwOnFailure
- whether to throw illegal state exception on connection creation failure- Returns:
- connection that was placed in the active pool
- Throws:
IllegalStateException
- ifcreateConnection(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 fromgetConnection()
. Validation only occurs ifvalidateOnCheckOut
is true. If a connection fails either activation or validation it is removed from the pool.- Parameters:
pc
- connection- Throws:
PoolException
- if either activation or validation fails
-
passivateAndValidateConnection
protected boolean passivateAndValidateConnection(PooledConnectionProxy pc)
Attempts to passivate and validate a connection. Performed when a connection is given toputConnection(Connection)
and when a new connection enters the pool. Validation only occurs ifvalidateOnCheckIn
is true.- Parameters:
pc
- connection- Returns:
- whether both passivation and validation succeeded
-
prune
public void prune()
Attempts to reduce the size of the pool back to its 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 interfaceConnectionPool
- Returns:
- count
-
activeCount
public int activeCount()
Description copied from interface:ConnectionPool
Returns the number of connections in use.- Specified by:
activeCount
in interfaceConnectionPool
- Returns:
- count
-
getPooledConnectionStatistics
public Set<PooledConnectionStatistics> getPooledConnectionStatistics()
Description copied from interface:ConnectionPool
Returns the statistics for each connection in the pool.- Specified by:
getPooledConnectionStatistics
in interfaceConnectionPool
- 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
-
-