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 void
addResource(ResourceAllocator alloc)
add a resource with status busy and not enlistedvoid
blockRequests(long waitTimeout)
block any new requests to the pool Used for transparent dynamic reconfiguration of the poolvoid
cancelResizerTask()
protected boolean
cleanupResource(ResourceHandle handle)
ResourceHandle
createResource(ResourceAllocator alloc)
create a new resource using the given resource-allocatorvoid
createResourceAndAddToPool()
create a new resource and add it to pool (using default resource-allocator)protected ResourceHandle
createSingleResource(ResourceAllocator resourceAllocator)
Method to be used to create resource, instead of calling ResourceAllocator.createConfigBean().protected ResourceHandle
createSingleResourceAndAdjustPool(ResourceAllocator alloc, ResourceSpec spec)
This method will be called from the getUnenlistedResource method if we detect a failAllConnection flag.void
deleteResource(ResourceHandle resourceHandle)
destroys the given resourcevoid
dumpPoolStatus()
This method can be used for debugging purposesvoid
emptyFreeConnectionsInPool()
void
emptyPool()
boolean
flushConnectionPool()
Reinitialize connections established in the connection pool and bring the pool to steady pool size.protected void
freeResource(ResourceHandle resourceHandle)
protected void
freeUnenlistedResource(ResourceHandle h)
long
getIdleTimeout()
gets the idle connection timeout valueSet
getInvalidConnections(Set connections)
gets the invalid connections from the given connections setint
getMaxPoolSize()
Gets the max-pool-size attribute of this pool.protected ConnectorConnectionPool
getPoolConfigurationFromJndi(Hashtable env)
PoolInfo
getPoolInfo()
query the name of this pool.PoolStatus
getPoolStatus()
Get Connection Pool status by computing the free/used values of the connections in the pool.PoolWaitQueue
getPoolWaitQueue()
returns pool-wait-queuePoolWaitQueue
getReconfigWaitQueue()
returns wait-queue used during transparent dynamic reconfigurationlong
getReconfigWaitTime()
returns the reconfig-wait-timeint
getResizeQuantity()
gets the resize quantity of the poolResourceHandle
getResource(ResourceSpec spec, ResourceAllocator alloc, jakarta.transaction.Transaction txn)
returns resource from the pool.protected ResourceHandle
getResourceFromPool(ResourceAllocator alloc, ResourceSpec spec)
return resource in free list.int
getSteadyPoolSize()
Gets the steady-pool-size attribute of this pool.protected ResourceHandle
getUnenlistedResource(ResourceSpec spec, ResourceAllocator alloc, jakarta.transaction.Transaction tran)
To provide an unenlisted, valid, matched resource from pool.int
getWaitQueueLength()
gets the current wait queue lengthprotected void
initializePoolDataStructure()
protected void
initializePoolWaitQueue()
protected Resizer
initializeResizer()
protected void
initializeResourceSelectionStrategy()
protected void
initPool(ResourceAllocator allocator)
protected ResourceHandle
internalGetResource(ResourceSpec spec, ResourceAllocator alloc, jakarta.transaction.Transaction tran)
void
invalidConnectionDetected(ResourceHandle h)
callback method to handle the case of invalid connection detectedprotected boolean
isConnectionValid(ResourceHandle h, ResourceAllocator alloc)
Check whether the connection is validprotected boolean
isResourceUnused(ResourceHandle h)
protected boolean
isSelfManaged()
protected boolean
matchConnection(ResourceHandle resource, ResourceAllocator alloc)
check whether the connection retrieved from the pool matches with the request.protected void
notifyWaitingThreads()
protected void
performMaxConnectionUsageOperation(ResourceHandle handle)
If the resource is used for maxConnectionUsage times, destroy and create onevoid
potentialConnectionLeakFound()
listener method to handle in the event of connection leak detectedprotected 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.void
printConnectionLeakTrace(StringBuilder stackTrace)
to print the stack trace of the caller of getConnectionvoid
reclaimConnection(ResourceHandle handle)
reclaim the leaked connectionvoid
reconfigurePool(ConnectorConnectionPool poolResource)
Reconfigure the Pool's properties.protected void
reconfigureSteadyPoolSize(int oldSteadyPoolSize, int newSteadyPoolSize)
void
removePoolLifeCycleListener()
remove pool life cycle listenervoid
resizePool(boolean forced)
void
resourceClosed(ResourceHandle h)
this method is called to indicate that the resource is not used by a bean/application anymorevoid
resourceEnlisted(jakarta.transaction.Transaction tran, ResourceHandle resource)
this method is called when a resource is enlisted invoid
resourceErrorOccurred(ResourceHandle h)
void
setMaxPoolSize(int size)
Sets the max-pool-size value for this pool.void
setPoolLifeCycleListener(PoolLifeCycleListener listener)
set pool life cycle listenerprotected void
setResourceStateToBusy(ResourceHandle resourceHandle)
marks resource as busy.protected void
setResourceStateToFree(ResourceHandle resourceHandle)
marks resource as free.void
setSelfManaged(boolean selfManaged)
Sets/Resets the flag indicating if this pool is self managed.void
setSteadyPoolSize(int size)
Sets the steady-pool-size value for this pool.void
switchOnMatching()
Switch on matching of connections in the pool.String
toString()
void
transactionCompleted(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:
getResource
in 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:ResourcePool
set pool life cycle listener- Specified by:
setPoolLifeCycleListener
in interfaceResourcePool
-
removePoolLifeCycleListener
public void removePoolLifeCycleListener()
Description copied from interface:ResourcePool
remove pool life cycle listener- Specified by:
removePoolLifeCycleListener
in interfaceResourcePool
-
deleteResource
public void deleteResource(ResourceHandle resourceHandle)
Description copied from interface:ResourceHandler
destroys the given resource- Specified by:
deleteResource
in 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:
resourceClosed
in 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:
resourceErrorOccurred
in interfaceResourcePool
- Throws:
IllegalStateException
-
resourceEnlisted
public void resourceEnlisted(jakarta.transaction.Transaction tran, ResourceHandle resource) throws IllegalStateException
this method is called when a resource is enlisted in- Specified by:
resourceEnlisted
in interfaceResourcePool
- Parameters:
tran
- Transactionresource
- ResourceHandle- Throws:
IllegalStateException
-
transactionCompleted
public void transactionCompleted(jakarta.transaction.Transaction tran, int status) throws IllegalStateException
this method is called when transaction tran is completed- Specified by:
transactionCompleted
in 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:ResourceHandler
create a new resource using the given resource-allocator- Specified by:
createResource
in 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 PoolingException
Description copied from interface:ResourceHandler
create a new resource and add it to pool (using default resource-allocator)- Specified by:
createResourceAndAddToPool
in interfaceResourceHandler
- Throws:
PoolingException
- when unable to create a resource
-
getInvalidConnections
public Set getInvalidConnections(Set connections) throws jakarta.resource.ResourceException
Description copied from interface:ResourceHandler
gets the invalid connections from the given connections set- Specified by:
getInvalidConnections
in 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:ResourceHandler
callback method to handle the case of invalid connection detected- Specified by:
invalidConnectionDetected
in interfaceResourceHandler
- Parameters:
h
- connection that is invalid
-
resizePool
public void resizePool(boolean forced)
- Specified by:
resizePool
in interfaceResourcePool
-
notifyWaitingThreads
protected void notifyWaitingThreads()
-
emptyPool
public void emptyPool()
- Specified by:
emptyPool
in interfaceResourcePool
-
emptyFreeConnectionsInPool
public void emptyFreeConnectionsInPool()
- Specified by:
emptyFreeConnectionsInPool
in interfaceResourcePool
-
blockRequests
public void blockRequests(long waitTimeout)
Description copied from interface:ResourcePool
block any new requests to the pool Used for transparent dynamic reconfiguration of the pool- Specified by:
blockRequests
in interfaceResourcePool
- Parameters:
waitTimeout
- time for which the new requests will wait
-
getPoolWaitQueue
public PoolWaitQueue getPoolWaitQueue()
Description copied from interface:ResourcePool
returns pool-wait-queue- Specified by:
getPoolWaitQueue
in interfaceResourcePool
- Returns:
- wait-queue
-
getReconfigWaitQueue
public PoolWaitQueue getReconfigWaitQueue()
Description copied from interface:ResourcePool
returns wait-queue used during transparent dynamic reconfiguration- Specified by:
getReconfigWaitQueue
in interfaceResourcePool
- Returns:
- PoolWaitQueue
-
getReconfigWaitTime
public long getReconfigWaitTime()
Description copied from interface:ResourcePool
returns the reconfig-wait-time- Specified by:
getReconfigWaitTime
in interfaceResourcePool
- Returns:
- long
-
flushConnectionPool
public boolean flushConnectionPool() throws PoolingException
Reinitialize connections established in the connection pool and bring the pool to steady pool size.- Specified by:
flushConnectionPool
in 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:
reconfigurePool
in 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:
switchOnMatching
in interfaceResourcePool
-
getPoolInfo
public PoolInfo getPoolInfo()
query the name of this pool. Required by monitoring- Specified by:
getPoolInfo
in interfaceResourcePool
- Returns:
- the name of this pool
-
cancelResizerTask
public void cancelResizerTask()
- Specified by:
cancelResizerTask
in interfaceResourcePool
-
dumpPoolStatus
public void dumpPoolStatus()
This method can be used for debugging purposes
-
getMaxPoolSize
public int getMaxPoolSize()
Description copied from interface:ResourcePool
Gets 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:
getMaxPoolSize
in interfacePoolProperties
- Specified by:
getMaxPoolSize
in interfaceResourcePool
- Returns:
- max-pool-size value for this pool
- See Also:
ResourcePool.setMaxPoolSize(int)
-
getResizeQuantity
public int getResizeQuantity()
Description copied from interface:PoolProperties
gets the resize quantity of the pool- Specified by:
getResizeQuantity
in interfacePoolProperties
- Returns:
- resize quantity
-
getIdleTimeout
public long getIdleTimeout()
Description copied from interface:PoolProperties
gets the idle connection timeout value- Specified by:
getIdleTimeout
in interfacePoolProperties
- Returns:
- idle timeout value
-
getWaitQueueLength
public int getWaitQueueLength()
Description copied from interface:PoolProperties
gets the current wait queue length- Specified by:
getWaitQueueLength
in interfacePoolProperties
- Returns:
- wait queue length
-
getSteadyPoolSize
public int getSteadyPoolSize()
Description copied from interface:ResourcePool
Gets 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:
getSteadyPoolSize
in interfacePoolProperties
- Specified by:
getSteadyPoolSize
in interfaceResourcePool
- Returns:
- steady-pool-size value for this pool
- See Also:
ResourcePool.setSteadyPoolSize(int)
-
setMaxPoolSize
public void setMaxPoolSize(int size)
Description copied from interface:ResourcePool
Sets 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:
setMaxPoolSize
in interfaceResourcePool
- Parameters:
size
- - The new max-pool-size value- See Also:
ResourcePool.getMaxPoolSize()
-
setSteadyPoolSize
public void setSteadyPoolSize(int size)
Description copied from interface:ResourcePool
Sets 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:
setSteadyPoolSize
in interfaceResourcePool
- Parameters:
size
- - The new steady-pool-size value- See Also:
ResourcePool.getSteadyPoolSize()
-
setSelfManaged
public void setSelfManaged(boolean selfManaged)
Description copied from interface:ResourcePool
Sets/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:
setSelfManaged
in interfaceResourcePool
- Parameters:
selfManaged
- - true to switch on self management, false otherwise
-
isSelfManaged
protected boolean isSelfManaged()
-
potentialConnectionLeakFound
public void potentialConnectionLeakFound()
Description copied from interface:ConnectionLeakListener
listener method to handle in the event of connection leak detected- Specified by:
potentialConnectionLeakFound
in interfaceConnectionLeakListener
-
printConnectionLeakTrace
public void printConnectionLeakTrace(StringBuilder stackTrace)
Description copied from interface:ConnectionLeakListener
to print the stack trace of the caller of getConnection- Specified by:
printConnectionLeakTrace
in interfaceConnectionLeakListener
- Parameters:
stackTrace
- Stack trace of the caller
-
reclaimConnection
public void reclaimConnection(ResourceHandle handle)
Description copied from interface:ConnectionLeakListener
reclaim the leaked connection- Specified by:
reclaimConnection
in 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:
getPoolStatus
in interfaceResourcePool
- Returns:
- PoolStatus object
-
-