Search in sources :

Example 31 with ResourceHandle

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");
            }
        }
    }
}
Also used : InvocationException(org.glassfish.api.invocation.InvocationException) ResourceHandle(com.sun.enterprise.resource.ResourceHandle) ResourceSpec(com.sun.enterprise.resource.ResourceSpec) JavaEETransactionManager(com.sun.enterprise.transaction.api.JavaEETransactionManager) ResourceException(javax.resource.ResourceException)

Example 32 with ResourceHandle

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();
}
Also used : PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) JavaEETransaction(com.sun.enterprise.transaction.api.JavaEETransaction) Transaction(javax.transaction.Transaction) ResourceHandle(com.sun.enterprise.resource.ResourceHandle) ManagedConnection(javax.resource.spi.ManagedConnection) ResourceException(javax.resource.ResourceException) PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) ResourceException(javax.resource.ResourceException) InvocationException(org.glassfish.api.invocation.InvocationException) RetryableUnavailableException(javax.resource.spi.RetryableUnavailableException)

Example 33 with ResourceHandle

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;
}
Also used : Iterator(java.util.Iterator) ResourceHandle(com.sun.enterprise.resource.ResourceHandle)

Example 34 with ResourceHandle

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;
}
Also used : PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) ResourceHandle(com.sun.enterprise.resource.ResourceHandle) PoolingException(com.sun.appserv.connectors.internal.api.PoolingException)

Example 35 with ResourceHandle

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;
}
Also used : ResourceHandle(com.sun.enterprise.resource.ResourceHandle) ResourceState(com.sun.enterprise.resource.ResourceState) HashSet(java.util.HashSet)

Aggregations

ResourceHandle (com.sun.enterprise.resource.ResourceHandle)36 PoolingException (com.sun.appserv.connectors.internal.api.PoolingException)11 ResourceException (javax.resource.ResourceException)9 ResourceState (com.sun.enterprise.resource.ResourceState)6 JavaEETransaction (com.sun.enterprise.transaction.api.JavaEETransaction)5 HashSet (java.util.HashSet)5 AssocWithThreadResourceHandle (com.sun.enterprise.resource.AssocWithThreadResourceHandle)4 ManagedConnection (javax.resource.spi.ManagedConnection)3 RetryableUnavailableException (javax.resource.spi.RetryableUnavailableException)3 ResourceSpec (com.sun.enterprise.resource.ResourceSpec)2 JavaEETransactionManager (com.sun.enterprise.transaction.api.JavaEETransactionManager)2 Set (java.util.Set)2 InvocationException (org.glassfish.api.invocation.InvocationException)2 ConnectorRuntimeException (com.sun.appserv.connectors.internal.api.ConnectorRuntimeException)1 XAResourceWrapper (com.sun.enterprise.resource.XAResourceWrapper)1 ConnectionEventListener (com.sun.enterprise.resource.listener.ConnectionEventListener)1 PoolManager (com.sun.enterprise.resource.pool.PoolManager)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Iterator (java.util.Iterator)1 List (java.util.List)1