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
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classContains a connection that is participating in this pool. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ConnectionActivatorFor activating connections.protected Queue<PooledConnectionProxy>List of connections in use.private static final intAllowed pool size for both max and min, value is 65535.protected Queue<PooledConnectionProxy>List of available connections in the pool.protected final ReentrantLockLock for check outs.private DefaultConnectionFactoryConnection factory to create connections with.private booleanWhether to connect to the ldap on connection creation.static final intDefault max pool size, value is 10.static final intDefault min pool size, value is 3.private booleanWhetherinitialize()should throw if pooling configuration requirements are not met.private booleanWhetherinitialize()has been successfully invoked.protected final LoggerLogger for this class.private static final DurationMaximum task execution time for worker threads.private intMaximum pool size.private intMinimum pool size.private StringPool name.private ConnectionPassivatorFor passivating connections.private static final AtomicIntegerID used for pool name.private ScheduledExecutorServiceExecutor for scheduling pool tasks.protected final ReentrantLockLock for the entire pool.protected final ConditionCondition for notifying threads that a connection was returned.private PruneStrategyFor removing connections.private QueueTypeType of queue.private booleanWhether the ldap connection should be validated when returned to the pool.private booleanWhether the ldap connection should be validated when given from the pool.private booleanWhether the pool should be validated periodically.private ConnectionValidatorFor validating connections. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidAttempts to activate and validate a connection.intReturns the number of connections in use.intReturns the number of connections available for use.voidclose()Empty this pool, freeing any resources.private voidCloses all connections in the pool.private voidcloseConnections(CallableWorker<PooledConnectionProxy> callableWorker, List<PooledConnectionProxy> connections) Asynchronously closes all the connections in the supplied list.protected PooledConnectionProxycreateActiveConnection(boolean throwOnFailure) Create a new connection and place it in the active queue.protected PooledConnectionProxycreateAvailableConnection(boolean throwOnFailure) Create a new connection and place it in the available pool.protected voidcreateAvailableConnections(int count, boolean throwOnFailure, boolean throwIfNotInitialized) Asynchronously creates new connections and adds them to the available queue.private PooledConnectionProxycreateConnection(boolean throwOnFailure) Create a new connection.protected ConnectionCreates a connection proxy using the supplied pool connection.private List<PooledConnectionProxy>createConnections(CallableWorker<PooledConnectionProxy> callableWorker, int count, boolean throwOnFailure) Asynchronously creates new connections and confirms that the connections can be passivated and validated.voidfreeze()Freezes this object, making it immutable.Returns the activator for this pool.abstract ConnectionReturns a connection from the pool.booleanReturns whether connections will attempt to connect after creation.Returns the connection factory for this pool.booleanReturns whetherinitialize()should throw if pooling configuration requirements are not met.intReturns the max pool size.intReturns the min pool size.getName()Returns the name for this pool.Returns the passivator for this pool.Returns the statistics for each connection in the pool.Returns the prune strategy for this pool.Returns the type of queue used for this connection pool.Returns the connection validator for this pool.protected voidgrow(int size) Attempts to grow the pool to the supplied size.voidInitialize this pool for use.booleanReturns whether this pool has been initialized.booleanReturns the validate on check in flag.booleanReturns the validate on check out flag.booleanReturns the validate periodically flag.protected booleanAttempts to passivate and validate a connection.voidprune()Attempts to reduce the size of the pool back to its configured minimum.abstract voidReturns a connection to the pool.protected voidRemove a connection from the active pool.protected voidRemove a connection from both the available and active pools.protected voidRemove a connection from the available pool.protected PooledConnectionProxyRetrieves the invocation handler from the supplied connection proxy.voidSets the activator for this pool.voidsetConnectOnCreate(boolean b) Sets whether newly created connections will attempt to connect.voidSets the connection factory for this pool.voidsetFailFastInitialize(boolean b) Sets whetherinitialize()should throw if pooling configuration requirements are not met.voidsetMaxPoolSize(int size) Sets the max pool size.voidsetMinPoolSize(int size) Sets the min pool size.voidSets the name for this pool.voidSets the passivator for this pool.voidSets the prune strategy for this pool.voidsetQueueType(QueueType type) Sets the type of queue used for this connection pool.voidsetValidateOnCheckIn(boolean b) Sets the validate on check in flag.voidsetValidateOnCheckOut(boolean b) Sets the validate on check out flag.voidsetValidatePeriodically(boolean b) Sets the validate periodically flag.voidSets the connection validator for this pool.protected voidUsed to determine whetherinitialize()has been invoked for this pool.toString()voidvalidate()Attempts to validate all connections in the pool.Methods inherited from class org.ldaptive.AbstractFreezable
assertMutable, freeze, freeze, isFrozen
-
Field Details
-
DEFAULT_MIN_POOL_SIZE
public static final int DEFAULT_MIN_POOL_SIZEDefault min pool size, value is 3.- See Also:
-
DEFAULT_MAX_POOL_SIZE
public static final int DEFAULT_MAX_POOL_SIZEDefault max pool size, value is 10.- See Also:
-
ALLOWED_POOL_SIZE
private static final int ALLOWED_POOL_SIZEAllowed pool size for both max and min, value is 65535.- See Also:
-
MAX_WORKER_TIME
Maximum task execution time for worker threads. -
POOL_ID
ID used for pool name. -
logger
Logger for this class. -
poolLock
Lock for the entire pool. -
poolNotEmpty
Condition for notifying threads that a connection was returned. -
checkOutLock
Lock for check outs. -
available
List of available connections in the pool. -
active
List of connections in use. -
name
Pool name. -
minPoolSize
private int minPoolSizeMinimum pool size. -
maxPoolSize
private int maxPoolSizeMaximum pool size. -
validateOnCheckIn
private boolean validateOnCheckInWhether the ldap connection should be validated when returned to the pool. -
validateOnCheckOut
private boolean validateOnCheckOutWhether the ldap connection should be validated when given from the pool. -
validatePeriodically
private boolean validatePeriodicallyWhether the pool should be validated periodically. -
activator
For activating connections. -
passivator
For passivating connections. -
validator
For validating connections. -
pruneStrategy
For removing connections. -
connectionFactory
Connection factory to create connections with. -
connectOnCreate
private boolean connectOnCreateWhether to connect to the ldap on connection creation. -
queueType
Type of queue. LIFO or FIFO. -
poolExecutor
Executor for scheduling pool tasks. -
initialized
private volatile boolean initializedWhetherinitialize()has been successfully invoked. -
failFastInitialize
private boolean failFastInitializeWhetherinitialize()should throw if pooling configuration requirements are not met.
-
-
Constructor Details
-
AbstractConnectionPool
public AbstractConnectionPool()
-
-
Method Details
-
freeze
public void freeze()Description copied from interface:FreezableFreezes this object, making it immutable.- Specified by:
freezein interfaceFreezable- Overrides:
freezein classAbstractFreezable
-
getName
Returns the name for this pool.- Returns:
- pool name
-
setName
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
Returns the activator for this pool.- Returns:
- activator
-
setActivator
Sets the activator for this pool.- Parameters:
a- activator
-
getPassivator
Returns the passivator for this pool.- Returns:
- passivator
-
setPassivator
Sets the passivator for this pool.- Parameters:
p- passivator
-
getValidator
Returns the connection validator for this pool.- Returns:
- connection validator
-
setValidator
Sets the connection validator for this pool.- Parameters:
cv- connection validator
-
getPruneStrategy
Returns the prune strategy for this pool.- Returns:
- prune strategy
-
setPruneStrategy
Sets the prune strategy for this pool.- Parameters:
ps- prune strategy
-
getDefaultConnectionFactory
Returns the connection factory for this pool.- Returns:
- connection factory
-
setDefaultConnectionFactory
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
Returns the type of queue used for this connection pool.- Returns:
- queue type
-
setQueueType
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:
initializein 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) 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- Throws:
IllegalStateException- if the pool cannot grow to the supplied size andcreateAvailableConnections(int, boolean, boolean)throws
-
close
public void close()Empty this pool, freeing any resources.- Specified by:
closein interfaceConnectionPool- Throws:
IllegalStateException- if this pool has not been initialized
-
closeAllConnections
private void closeAllConnections()Closes all connections in the pool. -
closeConnections
private void closeConnections(CallableWorker<PooledConnectionProxy> callableWorker, List<PooledConnectionProxy> connections) Asynchronously closes all the connections in the supplied list.- Parameters:
callableWorker- to execute async tasksconnections- to close
-
getConnection
Returns a connection from the pool.- Specified by:
getConnectionin 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
Returns a connection to the pool.- Parameters:
c- connection- Throws:
IllegalStateException- if this pool has not been initialized
-
createConnection
Create a new connection. IfconnectOnCreateis true, the connection will be opened.- Parameters:
throwOnFailure- whether to throw illegal state exception- Returns:
- pooled connection or null
- Throws:
IllegalStateException- ifconnectOnCreateis true and the connection cannot be opened
-
createConnections
private List<PooledConnectionProxy> createConnections(CallableWorker<PooledConnectionProxy> callableWorker, int count, boolean throwOnFailure) Asynchronously creates new connections and confirms that the connections can be 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:
callableWorker- to execute async taskscount- number of connections to attempt to createthrowOnFailure- whether to throw illegal state exception on any connection creation failure- Returns:
- list of new connections
- Throws:
IllegalStateException- if throwOnFailure is true and count connections are not successfully created
-
createAvailableConnections
protected void createAvailableConnections(int count, boolean throwOnFailure, boolean throwIfNotInitialized) Asynchronously creates new connections and adds them to the available queue. SeecreateConnections(CallableWorker, int, boolean).- Parameters:
count- number of connections to attempt to createthrowOnFailure- whether to throw illegal state exception on any connection creation failurethrowIfNotInitialized- whether to throw illegal state exception if the pool is not initialized- Throws:
IllegalStateException- if throwOnFailure is true and count connections are not successfully created
-
createAvailableConnection
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
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
Remove a connection from the available pool.- Parameters:
pc- connection that is in the available pool
-
removeActiveConnection
Remove a connection from the active pool.- Parameters:
pc- connection that is in the active pool
-
removeAvailableAndActiveConnection
Remove a connection from both the available and active pools.- Parameters:
pc- connection that is in both the available and active pools
-
activateAndValidateConnection
Attempts to activate and validate a connection. Performed before a connection is returned fromgetConnection(). Validation only occurs ifvalidateOnCheckOutis 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
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 ifvalidateOnCheckInis 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:ConnectionPoolReturns the number of connections available for use.- Specified by:
availableCountin interfaceConnectionPool- Returns:
- count
-
activeCount
public int activeCount()Description copied from interface:ConnectionPoolReturns the number of connections in use.- Specified by:
activeCountin interfaceConnectionPool- Returns:
- count
-
getPooledConnectionStatistics
Description copied from interface:ConnectionPoolReturns the statistics for each connection in the pool.- Specified by:
getPooledConnectionStatisticsin interfaceConnectionPool- Returns:
- connection statistics
-
createConnectionProxy
Creates a connection proxy using the supplied pool connection.- Parameters:
pc- pool connection to create proxy with- Returns:
- connection proxy
-
retrieveConnectionProxy
Retrieves the invocation handler from the supplied connection proxy.- Parameters:
proxy- connection proxy- Returns:
- pooled connection proxy
-
toString
-