use of com.sun.enterprise.resource.ResourceHandle in project Payara by payara.
the class AssocWithThreadPoolResizer method scaleDownPool.
/**
* Scale down pool by a <code>size <= pool-resize-quantity</code>
*
* @param forced scale-down only when forced
* @param scaleDownQuantity no. of resources to remove
*/
@Override
protected void scaleDownPool(int scaleDownQuantity, boolean forced) {
if (pool.getResizeQuantity() > 0 && forced) {
scaleDownQuantity = (scaleDownQuantity <= (ds.getResourcesSize() - pool.getSteadyPoolSize())) ? scaleDownQuantity : 0;
debug("Scaling down pool by quantity : " + scaleDownQuantity);
Set<ResourceHandle> resourcesToRemove = new HashSet<ResourceHandle>();
try {
for (ResourceHandle h : ds.getAllResources()) {
if (scaleDownQuantity > 0) {
synchronized (h.lock) {
if (!h.isBusy()) {
resourcesToRemove.add(h);
((AssocWithThreadResourceHandle) h).setDirty();
scaleDownQuantity--;
}
}
}
}
} finally {
for (ResourceHandle resourceToRemove : resourcesToRemove) {
if (ds.getAllResources().contains(resourceToRemove)) {
ds.removeResource(resourceToRemove);
}
}
}
}
}
use of com.sun.enterprise.resource.ResourceHandle in project Payara by payara.
the class ConnectionPool method createResource.
public ResourceHandle createResource(ResourceAllocator alloc) throws PoolingException {
// NOTE : Pool should not call this method directly, it should be called only by pool-datastructure
ResourceHandle result = createSingleResource(alloc);
ResourceState state = new ResourceState();
state.setBusy(false);
state.setEnlisted(false);
result.setResourceState(state);
if (poolLifeCycleListener != null) {
poolLifeCycleListener.connectionCreated();
}
return result;
}
use of com.sun.enterprise.resource.ResourceHandle in project Payara by payara.
the class ConnectionPool method createSingleResource.
/**
* Method to be used to create resource, instead of calling ResourceAllocator.createConfigBean().
* This method handles the connection creation retrial in case of failure
*
* @param resourceAllocator ResourceAllocator
* @return ResourceHandle newly created resource
* @throws PoolingException when unable create a resource
*/
protected ResourceHandle createSingleResource(ResourceAllocator resourceAllocator) throws PoolingException {
ResourceHandle resourceHandle;
int count = 0;
long startTime = 0;
while (true) {
try {
count++;
startTime = System.currentTimeMillis();
resourceHandle = resourceAllocator.createResource();
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Time taken to create a single " + "resource : " + resourceHandle.getResourceSpec().getResourceId() + " and adding to the pool (ms) : " + (System.currentTimeMillis() - startTime));
}
if (validation || validateAtmostEveryIdleSecs)
resourceHandle.setLastValidated(System.currentTimeMillis());
break;
} catch (Exception ex) {
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Connection creation failed for " + count + " time. It will be retried, " + "if connection creation retrial is enabled.", ex);
}
if (!connectionCreationRetry_ || count > connectionCreationRetryAttempts_)
throw new PoolingException(ex);
try {
Thread.sleep(conCreationRetryInterval_);
} catch (InterruptedException ie) {
// ignore this exception
}
}
}
return resourceHandle;
}
use of com.sun.enterprise.resource.ResourceHandle in project Payara by payara.
the class ConnectionPool method getResourceFromPool.
/**
* return resource in free list. If none is found, try to scale up the pool/purge pool and <br>
* return a new resource. returns null if the pool new resources cannot be created. <br>
*
* @param alloc ResourceAllocator
* @return ResourceHandle resource from pool
* @throws PoolingException if unable to create a new resource
*/
protected ResourceHandle getResourceFromPool(ResourceAllocator alloc, ResourceSpec spec) throws PoolingException {
// the order of serving a resource request
// 1. free and enlisted in the same transaction
// 2. free and unenlisted
// Do NOT give out a connection that is
// free and enlisted in a different transaction
ResourceHandle result = null;
ResourceHandle h;
ArrayList<ResourceHandle> freeResources = new ArrayList<ResourceHandle>();
try {
while ((h = ds.getResource()) != null) {
if (h.hasConnectionErrorOccurred()) {
ds.removeResource(h);
continue;
}
if (matchConnection(h, alloc)) {
boolean isValid = isConnectionValid(h, alloc);
if (h.hasConnectionErrorOccurred() || !isValid) {
if (failAllConnections) {
result = createSingleResourceAndAdjustPool(alloc, spec);
// no need to match since the resource is created with the allocator of caller.
break;
} else {
ds.removeResource(h);
// resource is invalid, continue iteration.
continue;
}
}
if (h.isShareable() == alloc.shareableWithinComponent()) {
// got a matched, valid resource
result = h;
break;
} else {
freeResources.add(h);
}
} else {
freeResources.add(h);
}
}
} finally {
// return all unmatched, free resources
for (ResourceHandle freeResource : freeResources) {
ds.returnResource(freeResource);
}
freeResources.clear();
}
if (result != null) {
// set correct state
setResourceStateToBusy(result);
} else {
result = resizePoolAndGetNewResource(alloc);
}
return result;
}
use of com.sun.enterprise.resource.ResourceHandle in project Payara by payara.
the class PoolManagerImpl method registerResource.
public void registerResource(com.sun.appserv.connectors.internal.api.ResourceHandle handle) throws PoolingException {
ResourceHandle h = (ResourceHandle) handle;
ResourceManager rm = getResourceManager(h.getResourceSpec());
rm.registerResource(h);
}
Aggregations