Search in sources :

Example 26 with ResourceHandle

use of com.sun.enterprise.resource.ResourceHandle in project Payara by payara.

the class AssocWithThreadPoolResizer method scaleDownPool.

/**
 * Scale down pool by a <code>size &lt;= 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);
                }
            }
        }
    }
}
Also used : ResourceHandle(com.sun.enterprise.resource.ResourceHandle) AssocWithThreadResourceHandle(com.sun.enterprise.resource.AssocWithThreadResourceHandle) HashSet(java.util.HashSet) AssocWithThreadResourceHandle(com.sun.enterprise.resource.AssocWithThreadResourceHandle)

Example 27 with ResourceHandle

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

Example 28 with ResourceHandle

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

Example 29 with 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;
}
Also used : ResourceHandle(com.sun.enterprise.resource.ResourceHandle)

Example 30 with ResourceHandle

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

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