use of com.sun.enterprise.resource.ResourceHandle in project Payara by payara.
the class PoolManagerImpl method handleLazilyAssociatedConnectionPools.
/**
* If the connections associated with the component are lazily-associatable, dissociate them.
* @param comp Component that acquired connections
* @param invToUse component invocation
*/
private void handleLazilyAssociatedConnectionPools(Object comp, ComponentInvocation invToUse) {
JavaEETransactionManager tm = getConnectorRuntime().getTransactionManager();
List list = tm.getExistingResourceList(comp, invToUse);
if (list == null) {
// have any resources and hence the existingResourcesList is null
return;
}
if (list.isEmpty())
return;
ResourceHandle[] handles = new ResourceHandle[list.size()];
handles = (ResourceHandle[]) list.toArray(handles);
for (ResourceHandle h : handles) {
if (h == null) {
_logger.log(Level.WARNING, "lazy_association.lazy_association_resource_handle");
continue;
}
ResourceSpec spec = h.getResourceSpec();
if (spec == null) {
_logger.log(Level.WARNING, "lazy_association.lazy_association_resource_spec");
continue;
}
if (spec.isLazyAssociatable()) {
// of type DissociatableManagedConnection
if (h.getResource() != null) {
javax.resource.spi.DissociatableManagedConnection mc = (javax.resource.spi.DissociatableManagedConnection) h.getResource();
if (h.isEnlisted()) {
getResourceManager(spec).delistResource(h, XAResource.TMSUCCESS);
}
try {
mc.dissociateConnections();
} catch (ResourceException re) {
InvocationException ie = new InvocationException(re.getMessage());
ie.initCause(re);
throw ie;
} finally {
if (h.getResourceState().isBusy()) {
putbackDirectToPool(h, spec.getPoolInfo());
}
}
} else {
_logger.log(Level.WARNING, "lazy_association.lazy_association_resource");
}
}
}
}
use of com.sun.enterprise.resource.ResourceHandle in project Payara by payara.
the class PoolManagerImpl method getResource.
// invoked by DataSource objects to obtain a connection
public Object getResource(ResourceSpec spec, ResourceAllocator alloc, ClientSecurityInfo info) throws PoolingException, RetryableUnavailableException {
Transaction tran = null;
boolean transactional = alloc.isTransactional();
if (transactional) {
tran = getResourceManager(spec).getTransaction();
}
ResourceHandle handle = getResourceFromPool(spec, alloc, info, tran);
if (!handle.supportsLazyAssociation()) {
spec.setLazyAssociatable(false);
}
if (spec.isLazyAssociatable() && spec.getConnectionToAssociate() != null) {
// we need to associate a new connection with it
try {
Object connection = spec.getConnectionToAssociate();
ManagedConnection dmc = (ManagedConnection) handle.getResource();
dmc.associateConnection(connection);
} catch (ResourceException e) {
putbackDirectToPool(handle, spec.getPoolInfo());
PoolingException pe = new PoolingException(e.getMessage());
pe.initCause(e);
throw pe;
}
}
// we cannot either
if (!handle.supportsLazyEnlistment()) {
spec.setLazyEnlistable(false);
}
handle.setResourceSpec(spec);
try {
if (handle.getResourceState().isUnenlisted()) {
// The spec being used here is the spec with the updated
// lazy enlistment info
// Here's the real place where we care about the correct
// resource manager (which in turn depends upon the ResourceSpec)
// and that's because if lazy enlistment needs to be done
// we need to get the LazyEnlistableResourceManager
getResourceManager(spec).enlistResource(handle);
}
} catch (Exception e) {
// In the rare cases where enlistResource throws exception, we
// should throw the resource away
putbackBadResourceToPool(handle);
_logger.log(Level.WARNING, "poolmgr.err_enlisting_res_in_getconn", spec.getPoolInfo());
logFine("rm.enlistResource threw Exception. Evicting resource from pool");
// and rethrow the exception
throw new PoolingException(e);
}
return handle.getUserConnection();
}
use of com.sun.enterprise.resource.ResourceHandle in project Payara by payara.
the class RWLockDataStructure method getFreeListSize.
/**
* {@inheritDoc}
*/
public int getFreeListSize() {
// inefficient implementation.
int free = 0;
readLock.lock();
try {
Iterator it = resources.iterator();
while (it.hasNext()) {
ResourceHandle rh = (ResourceHandle) it.next();
if (!rh.isBusy()) {
free++;
}
}
} finally {
readLock.unlock();
}
return free;
}
use of com.sun.enterprise.resource.ResourceHandle in project Payara by payara.
the class RWLockDataStructure method addResource.
/**
* {@inheritDoc}
*/
public int addResource(ResourceAllocator allocator, int count) throws PoolingException {
int numResAdded = 0;
writeLock.lock();
// for now, coarser lock. finer lock needs "resources.size() < maxSize()" once more.
try {
for (int i = 0; i < count && resources.size() < maxSize; i++) {
ResourceHandle handle = handler.createResource(allocator);
resources.add(handle);
numResAdded++;
}
} catch (Exception e) {
PoolingException pe = new PoolingException(e.getMessage());
pe.initCause(e);
throw pe;
} finally {
writeLock.unlock();
}
return numResAdded;
}
use of com.sun.enterprise.resource.ResourceHandle in project Payara by payara.
the class Resizer method removeIdleAndInvalidResources.
/**
* Get the free connections list from the pool, remove idle-timed-out resources
* and then invalid resources.
*
* @return int number of resources removed
*/
protected int removeIdleAndInvalidResources() {
int poolSizeBeforeRemoval = ds.getResourcesSize();
int noOfResourcesRemoved;
// Find all Connections that are free/not-in-use
ResourceState state;
int size = ds.getFreeListSize();
// let's cache the current time since precision is not required here.
long currentTime = System.currentTimeMillis();
int validConnectionsCounter = 0;
int idleConnKeptInSteadyCounter = 0;
// iterate through all thre active resources to find idle-time lapsed ones.
ResourceHandle h;
Set<ResourceHandle> activeResources = new HashSet<ResourceHandle>();
Set<String> resourcesToValidate = new HashSet<String>();
try {
while ((h = ds.getResource()) != null) {
state = h.getResourceState();
if (currentTime - state.getTimestamp() < pool.getIdleTimeout()) {
// Should be added for validation.
validConnectionsCounter++;
resourcesToValidate.add(h.toString());
activeResources.add(h);
} else {
boolean isResourceEligibleForRemoval = isResourceEligibleForRemoval(h, validConnectionsCounter);
if (!isResourceEligibleForRemoval) {
// preferValidateOverrecreate true and connection is valid within SPS
validConnectionsCounter++;
idleConnKeptInSteadyCounter++;
activeResources.add(h);
debug("PreferValidateOverRecreate: Keeping idle resource " + h + " in the steady part of the free pool " + "as the RA reports it to be valid (" + validConnectionsCounter + " <= " + pool.getSteadyPoolSize() + ")");
} else {
// Add to remove
ds.removeResource(h);
}
}
}
} finally {
for (ResourceHandle activeResource : activeResources) {
ds.returnResource(activeResource);
}
}
// remove invalid resources from the free (active) resources list.
// Since the whole pool is not locked, it may happen that some of these resources may be
// given to applications.
removeInvalidResources(resourcesToValidate);
// These statistic computations will work fine as long as resizer locks the pool throughout its operations.
if (preferValidateOverRecreate) {
debug("Idle resources validated and kept in the steady pool for pool [ " + poolInfo + " ] - " + idleConnKeptInSteadyCounter);
debug("Number of Idle resources freed for pool [ " + poolInfo + " ] - " + (size - activeResources.size() - idleConnKeptInSteadyCounter));
debug("Number of Invalid resources removed for pool [ " + poolInfo + " ] - " + (activeResources.size() - ds.getFreeListSize() + idleConnKeptInSteadyCounter));
} else {
debug("Number of Idle resources freed for pool [ " + poolInfo + " ] - " + (size - activeResources.size()));
debug("Number of Invalid resources removed for pool [ " + poolInfo + " ] - " + (activeResources.size() - ds.getFreeListSize()));
}
noOfResourcesRemoved = poolSizeBeforeRemoval - ds.getResourcesSize();
return noOfResourcesRemoved;
}
Aggregations