Package com.sun.enterprise.resource.pool
Class ConnectionPool
- java.lang.Object
-
- com.sun.enterprise.resource.pool.ConnectionPool
-
- All Implemented Interfaces:
ConnectionLeakListener,PoolProperties,ResourceHandler,ResourcePool
- Direct Known Subclasses:
AssocWithThreadResourcePool,UnpooledResource
public class ConnectionPool extends Object implements ResourcePool, ConnectionLeakListener, ResourceHandler, PoolProperties
Connection Pool for Connector & JDBC resources- Author:
- Jagadish Ramu
-
-
Field Summary
-
Constructor Summary
Constructors Constructor Description ConnectionPool(PoolInfo poolInfo, Hashtable env)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddResource(ResourceAllocator alloc)add a resource with status busy and not enlistedvoidblockRequests(long waitTimeout)block any new requests to the pool Used for transparent dynamic reconfiguration of the poolvoidcancelResizerTask()protected booleancleanupResource(ResourceHandle handle)ResourceHandlecreateResource(ResourceAllocator alloc)create a new resource using the given resource-allocatorvoidcreateResourceAndAddToPool()create a new resource and add it to pool (using default resource-allocator)protected ResourceHandlecreateSingleResource(ResourceAllocator resourceAllocator)Method to be used to create resource, instead of calling ResourceAllocator.createConfigBean().protected ResourceHandlecreateSingleResourceAndAdjustPool(ResourceAllocator alloc, ResourceSpec spec)This method will be called from the getUnenlistedResource method if we detect a failAllConnection flag.voiddeleteResource(ResourceHandle resourceHandle)destroys the given resourcevoiddumpPoolStatus()This method can be used for debugging purposesvoidemptyFreeConnectionsInPool()voidemptyPool()booleanflushConnectionPool()Reinitialize connections established in the connection pool and bring the pool to steady pool size.protected voidfreeResource(ResourceHandle resourceHandle)protected voidfreeUnenlistedResource(ResourceHandle h)longgetIdleTimeout()gets the idle connection timeout valueSetgetInvalidConnections(Set connections)gets the invalid connections from the given connections setintgetMaxPoolSize()Gets the max-pool-size attribute of this pool.protected ConnectorConnectionPoolgetPoolConfigurationFromJndi(Hashtable env)PoolInfogetPoolInfo()query the name of this pool.PoolStatusgetPoolStatus()Get Connection Pool status by computing the free/used values of the connections in the pool.PoolWaitQueuegetPoolWaitQueue()returns pool-wait-queuePoolWaitQueuegetReconfigWaitQueue()returns wait-queue used during transparent dynamic reconfigurationlonggetReconfigWaitTime()returns the reconfig-wait-timeintgetResizeQuantity()gets the resize quantity of the poolResourceHandlegetResource(ResourceSpec spec, ResourceAllocator alloc, jakarta.transaction.Transaction txn)returns resource from the pool.protected ResourceHandlegetResourceFromPool(ResourceAllocator alloc, ResourceSpec spec)return resource in free list.intgetSteadyPoolSize()Gets the steady-pool-size attribute of this pool.protected ResourceHandlegetUnenlistedResource(ResourceSpec spec, ResourceAllocator alloc, jakarta.transaction.Transaction tran)To provide an unenlisted, valid, matched resource from pool.intgetWaitQueueLength()gets the current wait queue lengthprotected voidinitializePoolDataStructure()protected voidinitializePoolWaitQueue()protected ResizerinitializeResizer()protected voidinitializeResourceSelectionStrategy()protected voidinitPool(ResourceAllocator allocator)protected ResourceHandleinternalGetResource(ResourceSpec spec, ResourceAllocator alloc, jakarta.transaction.Transaction tran)voidinvalidConnectionDetected(ResourceHandle h)callback method to handle the case of invalid connection detectedprotected booleanisConnectionValid(ResourceHandle h, ResourceAllocator alloc)Check whether the connection is validprotected booleanisResourceUnused(ResourceHandle h)protected booleanisSelfManaged()protected booleanmatchConnection(ResourceHandle resource, ResourceAllocator alloc)check whether the connection retrieved from the pool matches with the request.protected voidnotifyWaitingThreads()protected voidperformMaxConnectionUsageOperation(ResourceHandle handle)If the resource is used for maxConnectionUsage times, destroy and create onevoidpotentialConnectionLeakFound()listener method to handle in the event of connection leak detectedprotected ResourceHandleprefetch(ResourceSpec spec, ResourceAllocator alloc, jakarta.transaction.Transaction tran)Overridden in AssocWithThreadResourcePool to fetch the resource cached in the ThreadLocal In ConnectionPool this simply returns null.voidprintConnectionLeakTrace(StringBuilder stackTrace)to print the stack trace of the caller of getConnectionvoidreclaimConnection(ResourceHandle handle)reclaim the leaked connectionvoidreconfigurePool(ConnectorConnectionPool poolResource)Reconfigure the Pool's properties.protected voidreconfigureSteadyPoolSize(int oldSteadyPoolSize, int newSteadyPoolSize)voidremovePoolLifeCycleListener()remove pool life cycle listenervoidresizePool(boolean forced)voidresourceClosed(ResourceHandle h)this method is called to indicate that the resource is not used by a bean/application anymorevoidresourceEnlisted(jakarta.transaction.Transaction tran, ResourceHandle resource)this method is called when a resource is enlisted invoidresourceErrorOccurred(ResourceHandle h)voidsetMaxPoolSize(int size)Sets the max-pool-size value for this pool.voidsetPoolLifeCycleListener(PoolLifeCycleListener listener)set pool life cycle listenerprotected voidsetResourceStateToBusy(ResourceHandle resourceHandle)marks resource as busy.protected voidsetResourceStateToFree(ResourceHandle resourceHandle)marks resource as free.voidsetSelfManaged(boolean selfManaged)Sets/Resets the flag indicating if this pool is self managed.voidsetSteadyPoolSize(int size)Sets the steady-pool-size value for this pool.voidswitchOnMatching()Switch on matching of connections in the pool.StringtoString()voidtransactionCompleted(jakarta.transaction.Transaction tran, int status)this method is called when transaction tran is completed
-
-
-
Field Detail
-
localStrings
protected static final StringManager localStrings
-
_logger
protected static final Logger _logger
-
maxPoolSize
protected int maxPoolSize
-
steadyPoolSize
protected int steadyPoolSize
-
resizeQuantity
protected int resizeQuantity
-
maxWaitTime
protected int maxWaitTime
-
idletime
protected long idletime
-
failAllConnections
protected boolean failAllConnections
-
matchConnections
protected boolean matchConnections
-
validation
protected boolean validation
-
preferValidateOverRecreate
protected boolean preferValidateOverRecreate
-
resizerTask
protected Resizer resizerTask
-
poolInitialized
protected volatile boolean poolInitialized
-
timer
protected Timer timer
-
connectionCreationRetry_
protected boolean connectionCreationRetry_
-
connectionCreationRetryAttempts_
protected int connectionCreationRetryAttempts_
-
conCreationRetryInterval_
protected long conCreationRetryInterval_
-
validateAtmostPeriodInMilliSeconds_
protected long validateAtmostPeriodInMilliSeconds_
-
maxConnectionUsage_
protected int maxConnectionUsage_
-
resourceSelectionStrategyClass
protected String resourceSelectionStrategyClass
-
poolLifeCycleListener
protected PoolLifeCycleListener poolLifeCycleListener
-
gateway
protected ResourceGateway gateway
-
resourceGatewayClass
protected String resourceGatewayClass
-
leakDetector
protected ConnectionLeakDetector leakDetector
-
ds
protected DataStructure ds
-
dataStructureType
protected String dataStructureType
-
dataStructureParameters
protected String dataStructureParameters
-
waitQueue
protected PoolWaitQueue waitQueue
-
reconfigWaitQueue
protected PoolWaitQueue reconfigWaitQueue
-
poolWaitQueueClass
protected String poolWaitQueueClass
-
poolInfo
protected final PoolInfo poolInfo
-
allocator
protected ResourceAllocator allocator
-
-
Constructor Detail
-
ConnectionPool
public ConnectionPool(PoolInfo poolInfo, Hashtable env) throws PoolingException
- Throws:
PoolingException
-
-
Method Detail
-
initializePoolWaitQueue
protected void initializePoolWaitQueue() throws PoolingException- Throws:
PoolingException
-
initializePoolDataStructure
protected void initializePoolDataStructure() throws PoolingException- Throws:
PoolingException
-
initializeResourceSelectionStrategy
protected void initializeResourceSelectionStrategy()
-
getPoolConfigurationFromJndi
protected ConnectorConnectionPool getPoolConfigurationFromJndi(Hashtable env) throws PoolingException
- Throws:
PoolingException
-
initPool
protected void initPool(ResourceAllocator allocator) throws PoolingException
- Throws:
PoolingException
-
initializeResizer
protected Resizer initializeResizer()
-
addResource
public void addResource(ResourceAllocator alloc) throws PoolingException
add a resource with status busy and not enlisted- Parameters:
alloc- ResourceAllocator- Throws:
PoolingException- when unable to add a resource
-
setResourceStateToFree
protected void setResourceStateToFree(ResourceHandle resourceHandle)
marks resource as free. This method should be used instead of directly calling resoureHandle.getResourceState().setBusy(false) OR getResourceState(resourceHandle).setBusy(false) as this method handles stopping of connection leak tracing If connection leak tracing is enabled, takes care of stopping connection leak tracing- Parameters:
resourceHandle- Resource
-
setResourceStateToBusy
protected void setResourceStateToBusy(ResourceHandle resourceHandle)
marks resource as busy. This method should be used instead of directly calling resoureHandle.getResourceState().setBusy(true) OR getResourceState(resourceHandle).setBusy(true) as this method handles starting of connection leak tracing If connection leak tracing is enabled, takes care of starting connection leak tracing- Parameters:
resourceHandle- Resource
-
getResource
public ResourceHandle getResource(ResourceSpec spec, ResourceAllocator alloc, jakarta.transaction.Transaction txn) throws PoolingException, jakarta.resource.spi.RetryableUnavailableException
returns resource from the pool.- Specified by:
getResourcein interfaceResourcePool- Returns:
- a free pooled resource object matching the ResourceSpec
- Throws:
PoolingException- - if any error occurrs - or the pool has reached its max size and the max-connection-wait-time-in-millis has expired.jakarta.resource.spi.RetryableUnavailableException
-
prefetch
protected ResourceHandle prefetch(ResourceSpec spec, ResourceAllocator alloc, jakarta.transaction.Transaction tran)
Overridden in AssocWithThreadResourcePool to fetch the resource cached in the ThreadLocal In ConnectionPool this simply returns null.- Parameters:
spec- ResourceSpecalloc- ResourceAllocator to create a resourcetran- Transaction- Returns:
- ResourceHandle resource from ThreadLocal
-
internalGetResource
protected ResourceHandle internalGetResource(ResourceSpec spec, ResourceAllocator alloc, jakarta.transaction.Transaction tran) throws PoolingException
- Throws:
PoolingException
-
getUnenlistedResource
protected ResourceHandle getUnenlistedResource(ResourceSpec spec, ResourceAllocator alloc, jakarta.transaction.Transaction tran) throws PoolingException
To provide an unenlisted, valid, matched resource from pool.- Parameters:
spec- ResourceSpecalloc- ResourceAllocatortran- Transaction- Returns:
- ResourceHandle resource from pool
- Throws:
PoolingException- Exception while getting resource from pool
-
isConnectionValid
protected boolean isConnectionValid(ResourceHandle h, ResourceAllocator alloc)
Check whether the connection is valid- Parameters:
h- Resource to be validatedalloc- Allocator to validate the resource- Returns:
- boolean representing validation result
-
matchConnection
protected boolean matchConnection(ResourceHandle resource, ResourceAllocator alloc)
check whether the connection retrieved from the pool matches with the request.- Parameters:
resource- Resource to be matchedalloc- ResourceAllocator used to match the connection- Returns:
- boolean representing the match status of the connection
-
getResourceFromPool
protected ResourceHandle getResourceFromPool(ResourceAllocator alloc, ResourceSpec spec) throws PoolingException
return resource in free list. If none is found, try to scale up the pool/purge pool and
return a new resource. returns null if the pool new resources cannot be created.- Parameters:
alloc- ResourceAllocator- Returns:
- ResourceHandle resource from pool
- Throws:
PoolingException- if unable to create a new resource
-
createSingleResourceAndAdjustPool
protected ResourceHandle createSingleResourceAndAdjustPool(ResourceAllocator alloc, ResourceSpec spec) throws PoolingException
This method will be called from the getUnenlistedResource method if we detect a failAllConnection flag. Here we simply create a new resource and replace a free resource in the pool by this resource and then give it out. This replacement is required since the steadypoolsize might equal maxpoolsize and in that case if we were not to remove a resource from the pool, our resource would be above maxPoolSize- Parameters:
alloc- ResourceAllocator to create resourcespec- ResourceSpec- Returns:
- newly created resource
- Throws:
PoolingException- when unable to create a resource
-
createSingleResource
protected ResourceHandle createSingleResource(ResourceAllocator resourceAllocator) throws PoolingException
Method to be used to create resource, instead of calling ResourceAllocator.createConfigBean(). This method handles the connection creation retrial in case of failure- Parameters:
resourceAllocator- ResourceAllocator- Returns:
- ResourceHandle newly created resource
- Throws:
PoolingException- when unable create a resource
-
setPoolLifeCycleListener
public void setPoolLifeCycleListener(PoolLifeCycleListener listener)
Description copied from interface:ResourcePoolset pool life cycle listener- Specified by:
setPoolLifeCycleListenerin interfaceResourcePool
-
removePoolLifeCycleListener
public void removePoolLifeCycleListener()
Description copied from interface:ResourcePoolremove pool life cycle listener- Specified by:
removePoolLifeCycleListenerin interfaceResourcePool
-
deleteResource
public void deleteResource(ResourceHandle resourceHandle)
Description copied from interface:ResourceHandlerdestroys the given resource- Specified by:
deleteResourcein interfaceResourceHandler- Parameters:
resourceHandle- resource to be destroyed
-
resourceClosed
public void resourceClosed(ResourceHandle h) throws IllegalStateException
this method is called to indicate that the resource is not used by a bean/application anymore- Specified by:
resourceClosedin interfaceResourcePool- Throws:
IllegalStateException
-
performMaxConnectionUsageOperation
protected void performMaxConnectionUsageOperation(ResourceHandle handle)
If the resource is used for maxConnectionUsage times, destroy and create one- Parameters:
handle- Resource to be checked
-
freeUnenlistedResource
protected void freeUnenlistedResource(ResourceHandle h)
-
freeResource
protected void freeResource(ResourceHandle resourceHandle)
-
cleanupResource
protected boolean cleanupResource(ResourceHandle handle)
-
resourceErrorOccurred
public void resourceErrorOccurred(ResourceHandle h) throws IllegalStateException
- Specified by:
resourceErrorOccurredin interfaceResourcePool- Throws:
IllegalStateException
-
resourceEnlisted
public void resourceEnlisted(jakarta.transaction.Transaction tran, ResourceHandle resource) throws IllegalStateExceptionthis method is called when a resource is enlisted in- Specified by:
resourceEnlistedin interfaceResourcePool- Parameters:
tran- Transactionresource- ResourceHandle- Throws:
IllegalStateException
-
transactionCompleted
public void transactionCompleted(jakarta.transaction.Transaction tran, int status) throws IllegalStateExceptionthis method is called when transaction tran is completed- Specified by:
transactionCompletedin interfaceResourcePool- Parameters:
tran- Transactionstatus- status of transaction- Throws:
IllegalStateException
-
isResourceUnused
protected boolean isResourceUnused(ResourceHandle h)
-
createResource
public ResourceHandle createResource(ResourceAllocator alloc) throws PoolingException
Description copied from interface:ResourceHandlercreate a new resource using the given resource-allocator- Specified by:
createResourcein interfaceResourceHandler- Parameters:
alloc- allocator to create a resource- Returns:
- newly created resource
- Throws:
PoolingException- when unable to create a resource
-
createResourceAndAddToPool
public void createResourceAndAddToPool() throws PoolingExceptionDescription copied from interface:ResourceHandlercreate a new resource and add it to pool (using default resource-allocator)- Specified by:
createResourceAndAddToPoolin interfaceResourceHandler- Throws:
PoolingException- when unable to create a resource
-
getInvalidConnections
public Set getInvalidConnections(Set connections) throws jakarta.resource.ResourceException
Description copied from interface:ResourceHandlergets the invalid connections from the given connections set- Specified by:
getInvalidConnectionsin interfaceResourceHandler- Parameters:
connections- that need to be validated- Returns:
- invalid connections set
- Throws:
jakarta.resource.ResourceException- when unable to validate
-
invalidConnectionDetected
public void invalidConnectionDetected(ResourceHandle h)
Description copied from interface:ResourceHandlercallback method to handle the case of invalid connection detected- Specified by:
invalidConnectionDetectedin interfaceResourceHandler- Parameters:
h- connection that is invalid
-
resizePool
public void resizePool(boolean forced)
- Specified by:
resizePoolin interfaceResourcePool
-
notifyWaitingThreads
protected void notifyWaitingThreads()
-
emptyPool
public void emptyPool()
- Specified by:
emptyPoolin interfaceResourcePool
-
emptyFreeConnectionsInPool
public void emptyFreeConnectionsInPool()
- Specified by:
emptyFreeConnectionsInPoolin interfaceResourcePool
-
blockRequests
public void blockRequests(long waitTimeout)
Description copied from interface:ResourcePoolblock any new requests to the pool Used for transparent dynamic reconfiguration of the pool- Specified by:
blockRequestsin interfaceResourcePool- Parameters:
waitTimeout- time for which the new requests will wait
-
getPoolWaitQueue
public PoolWaitQueue getPoolWaitQueue()
Description copied from interface:ResourcePoolreturns pool-wait-queue- Specified by:
getPoolWaitQueuein interfaceResourcePool- Returns:
- wait-queue
-
getReconfigWaitQueue
public PoolWaitQueue getReconfigWaitQueue()
Description copied from interface:ResourcePoolreturns wait-queue used during transparent dynamic reconfiguration- Specified by:
getReconfigWaitQueuein interfaceResourcePool- Returns:
- PoolWaitQueue
-
getReconfigWaitTime
public long getReconfigWaitTime()
Description copied from interface:ResourcePoolreturns the reconfig-wait-time- Specified by:
getReconfigWaitTimein interfaceResourcePool- Returns:
- long
-
flushConnectionPool
public boolean flushConnectionPool() throws PoolingExceptionReinitialize connections established in the connection pool and bring the pool to steady pool size.- Specified by:
flushConnectionPoolin interfaceResourcePool- Returns:
- boolean indicating whether flush operation was successful or not
- Throws:
PoolingException
-
reconfigurePool
public void reconfigurePool(ConnectorConnectionPool poolResource) throws PoolingException
Reconfigure the Pool's properties. The reconfigConnectorConnectionPool method in the ConnectorRuntime will use this method (through PoolManager) if it needs to just change pool properties and not recreate the pool- Specified by:
reconfigurePoolin interfaceResourcePool- Parameters:
poolResource- - the ConnectorConnectionPool JavaBean that holds the new pool properties- Throws:
PoolingException- if the pool resizing fails
-
reconfigureSteadyPoolSize
protected void reconfigureSteadyPoolSize(int oldSteadyPoolSize, int newSteadyPoolSize) throws PoolingException- Throws:
PoolingException
-
switchOnMatching
public void switchOnMatching()
Switch on matching of connections in the pool.- Specified by:
switchOnMatchingin interfaceResourcePool
-
getPoolInfo
public PoolInfo getPoolInfo()
query the name of this pool. Required by monitoring- Specified by:
getPoolInfoin interfaceResourcePool- Returns:
- the name of this pool
-
cancelResizerTask
public void cancelResizerTask()
- Specified by:
cancelResizerTaskin interfaceResourcePool
-
dumpPoolStatus
public void dumpPoolStatus()
This method can be used for debugging purposes
-
getMaxPoolSize
public int getMaxPoolSize()
Description copied from interface:ResourcePoolGets the max-pool-size attribute of this pool. Envisaged to be used by the Self management framework to query the pool size attribute for tweaking it using the setMaxPoolSize method- Specified by:
getMaxPoolSizein interfacePoolProperties- Specified by:
getMaxPoolSizein interfaceResourcePool- Returns:
- max-pool-size value for this pool
- See Also:
ResourcePool.setMaxPoolSize(int)
-
getResizeQuantity
public int getResizeQuantity()
Description copied from interface:PoolPropertiesgets the resize quantity of the pool- Specified by:
getResizeQuantityin interfacePoolProperties- Returns:
- resize quantity
-
getIdleTimeout
public long getIdleTimeout()
Description copied from interface:PoolPropertiesgets the idle connection timeout value- Specified by:
getIdleTimeoutin interfacePoolProperties- Returns:
- idle timeout value
-
getWaitQueueLength
public int getWaitQueueLength()
Description copied from interface:PoolPropertiesgets the current wait queue length- Specified by:
getWaitQueueLengthin interfacePoolProperties- Returns:
- wait queue length
-
getSteadyPoolSize
public int getSteadyPoolSize()
Description copied from interface:ResourcePoolGets the steady-pool-size attribute of this pool. Envisaged to be used by the Self management framework to query the pool size attribute for tweaking it using the setSteadyPoolSize method- Specified by:
getSteadyPoolSizein interfacePoolProperties- Specified by:
getSteadyPoolSizein interfaceResourcePool- Returns:
- steady-pool-size value for this pool
- See Also:
ResourcePool.setSteadyPoolSize(int)
-
setMaxPoolSize
public void setMaxPoolSize(int size)
Description copied from interface:ResourcePoolSets the max-pool-size value for this pool. This attribute is expected to be set by the self-management framework for an optimum max-pool-size. The corresponding accessor gets this value.- Specified by:
setMaxPoolSizein interfaceResourcePool- Parameters:
size- - The new max-pool-size value- See Also:
ResourcePool.getMaxPoolSize()
-
setSteadyPoolSize
public void setSteadyPoolSize(int size)
Description copied from interface:ResourcePoolSets the steady-pool-size value for this pool. This attribute is expected to be set by the self-management framework for an optimum steady-pool-size. The corresponding accessor gets this value.- Specified by:
setSteadyPoolSizein interfaceResourcePool- Parameters:
size- - The new steady-pool-size value- See Also:
ResourcePool.getSteadyPoolSize()
-
setSelfManaged
public void setSelfManaged(boolean selfManaged)
Description copied from interface:ResourcePoolSets/Resets the flag indicating if this pool is self managed. This method would be typically called by the self management framework to indicate to the world (and this pool) that this pool is self managed. Its very important that the self mgmt framework properly sets this flag to control the dynamic reconfig behavior of this pool. If this flag is set to true, all dynamic reconfigs affecting the max/steady pool size of this pool will be ignored.- Specified by:
setSelfManagedin interfaceResourcePool- Parameters:
selfManaged- - true to switch on self management, false otherwise
-
isSelfManaged
protected boolean isSelfManaged()
-
potentialConnectionLeakFound
public void potentialConnectionLeakFound()
Description copied from interface:ConnectionLeakListenerlistener method to handle in the event of connection leak detected- Specified by:
potentialConnectionLeakFoundin interfaceConnectionLeakListener
-
printConnectionLeakTrace
public void printConnectionLeakTrace(StringBuilder stackTrace)
Description copied from interface:ConnectionLeakListenerto print the stack trace of the caller of getConnection- Specified by:
printConnectionLeakTracein interfaceConnectionLeakListener- Parameters:
stackTrace- Stack trace of the caller
-
reclaimConnection
public void reclaimConnection(ResourceHandle handle)
Description copied from interface:ConnectionLeakListenerreclaim the leaked connection- Specified by:
reclaimConnectionin interfaceConnectionLeakListener- Parameters:
handle- Resource to be reclaimed
-
getPoolStatus
public PoolStatus getPoolStatus()
Get Connection Pool status by computing the free/used values of the connections in the pool. Computations are based on whether the pool is initialized or not when this method is invoked.- Specified by:
getPoolStatusin interfaceResourcePool- Returns:
- PoolStatus object
-
-