Search in sources :

Example 6 with ResourceHandle

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

the class ConnectionPool method purgeResources.

/**
 * Try to purge resources by size <=  quantity <br>
 *
 * @param quantity maximum no. of resources to remove. <br>
 * @return resourceCount No. of resources actually removed. <br>
 */
private int purgeResources(int quantity) {
    // Must be called from the thread holding the lock to this pool.
    int totalResourcesRemoved = 0;
    int freeResourcesCount = ds.getFreeListSize();
    int resourcesCount = (freeResourcesCount >= quantity) ? quantity : freeResourcesCount;
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, "Purging resources of size : " + resourcesCount);
    }
    for (int i = resourcesCount - 1; i >= 0; i--) {
        ResourceHandle resource = ds.getResource();
        if (resource != null) {
            ds.removeResource(resource);
            totalResourcesRemoved += 1;
        }
    }
    return totalResourcesRemoved;
}
Also used : ResourceHandle(com.sun.enterprise.resource.ResourceHandle)

Example 7 with ResourceHandle

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

the class ConnectionPool method getResource.

/**
 * returns resource from the pool.
 *
 * @return 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.
 */
public ResourceHandle getResource(ResourceSpec spec, ResourceAllocator alloc, Transaction txn) throws PoolingException, RetryableUnavailableException {
    // Note: this method should not be synchronized or the
    // startTime would be incorrect for threads waiting to enter
    /*
        * Here are all the comments for the method put together for
        * easy reference.
        *  1.
           // - Try to get a free resource. Note: internalGetResource()
           // will create a new resource if none is free and the max has
           // not been reached.
           // - If can't get one, get on the wait queue.
           // - Repeat this until maxWaitTime expires.
           // - If maxWaitTime == 0, repeat indefinitely.

           2.
           //the doFailAllConnectionsProcessing method would already
           //have been invoked by now.
           //We simply go ahead and create a new resource here
           //from the allocator that we have and adjust the resources
           //list accordingly so as to not exceed the maxPoolSize ever
           //(i.e if steadyPoolSize == maxPoolSize )
           ///Also since we are creating the resource out of the allocator
           //that we came into this method with, we need not worry about
           //matching
        */
    ResourceHandle result = null;
    long startTime = System.currentTimeMillis();
    long elapsedWaitTime;
    long remainingWaitTime = 0;
    while (true) {
        if (gateway.allowed()) {
            // See comment #1 above
            JavaEETransaction jtx = ((JavaEETransaction) txn);
            Set resourcesSet = null;
            if (jtx != null) {
                resourcesSet = jtx.getResources(poolInfo);
            }
            // already obtained in the current transaction.
            if (!blocked || (resourcesSet != null && resourcesSet.size() > 0)) {
                try {
                    result = internalGetResource(spec, alloc, txn);
                } finally {
                    gateway.acquiredResource();
                }
            }
        }
        if (result != null) {
            // got one, return it
            if (poolLifeCycleListener != null) {
                poolLifeCycleListener.connectionAcquired(result.getId());
                elapsedWaitTime = System.currentTimeMillis() - startTime;
                poolLifeCycleListener.connectionRequestServed(elapsedWaitTime);
                if (_logger.isLoggable(Level.FINE)) {
                    _logger.log(Level.FINE, "Resource Pool: elapsed time " + "(ms) to get connection for [" + spec + "] : " + elapsedWaitTime);
                }
            }
            // return it
            break;
        } else {
            // did not get a resource.
            if (maxWaitTime > 0) {
                elapsedWaitTime = System.currentTimeMillis() - startTime;
                if (elapsedWaitTime < maxWaitTime) {
                    // time has not expired, determine remaining wait time.
                    remainingWaitTime = maxWaitTime - elapsedWaitTime;
                } else {
                    if (!blocked) {
                        // wait time has expired
                        if (poolLifeCycleListener != null) {
                            poolLifeCycleListener.connectionTimedOut();
                        }
                        String msg = localStrings.getStringWithDefault("poolmgr.no.available.resource", "No available resource. Wait-time expired.");
                        throw new PoolingException(msg);
                    }
                }
            }
            if (!blocked) {
                // add to wait-queue
                Object waitMonitor = new Object();
                if (poolLifeCycleListener != null) {
                    poolLifeCycleListener.connectionRequestQueued();
                }
                synchronized (waitMonitor) {
                    waitQueue.addToQueue(waitMonitor);
                    try {
                        logFine("Resource Pool: getting on wait queue");
                        waitMonitor.wait(remainingWaitTime);
                    } catch (InterruptedException ex) {
                        // Could be system shutdown.
                        break;
                    }
                    // so the overhead for removing inexistant objects is low.
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.log(Level.FINE, "removing wait monitor from queue: " + waitMonitor);
                    }
                    if (waitQueue.removeFromQueue(waitMonitor)) {
                        if (poolLifeCycleListener != null) {
                            poolLifeCycleListener.connectionRequestDequeued();
                        }
                    }
                }
            } else {
                // add to reconfig-wait-queue
                Object reconfigWaitMonitor = new Object();
                synchronized (reconfigWaitMonitor) {
                    reconfigWaitQueue.addToQueue(reconfigWaitMonitor);
                    try {
                        if (reconfigWaitTime > 0) {
                            if (_logger.isLoggable(Level.FINEST)) {
                                _logger.finest("[DRC] getting into reconfig wait queue for time [" + reconfigWaitTime + "]");
                            }
                            reconfigWaitMonitor.wait(reconfigWaitTime);
                        }
                    } catch (InterruptedException ex) {
                        // Could be system shutdown.
                        break;
                    }
                    // so the overhead for removing inexistent objects is low.
                    if (_logger.isLoggable(Level.FINEST)) {
                        _logger.log(Level.FINEST, "[DRC] removing wait monitor from reconfig-wait-queue: " + reconfigWaitMonitor);
                    }
                    reconfigWaitQueue.removeFromQueue(reconfigWaitMonitor);
                    if (_logger.isLoggable(Level.FINEST)) {
                        _logger.log(Level.FINEST, "[DRC] throwing Retryable-Unavailable-Exception");
                    }
                    RetryableUnavailableException rue = new RetryableUnavailableException("Pool Reconfigured, " + "Connection Factory can retry the lookup");
                    rue.setErrorCode(BadConnectionEventListener.POOL_RECONFIGURED_ERROR_CODE);
                    throw rue;
                }
            }
        }
    }
    alloc.fillInResourceObjects(result);
    return result;
}
Also used : PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) JavaEETransaction(com.sun.enterprise.transaction.api.JavaEETransaction) RetryableUnavailableException(javax.resource.spi.RetryableUnavailableException) ResourceHandle(com.sun.enterprise.resource.ResourceHandle)

Example 8 with ResourceHandle

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

the class ConnectionPool method getMatchedResourceFromPool.

// TODO can't this be replaced by getResourceFromPool ?
private ResourceHandle getMatchedResourceFromPool(ResourceAllocator alloc) {
    ResourceHandle handle;
    ResourceHandle result = null;
    ArrayList<ResourceHandle> activeResources = new ArrayList<ResourceHandle>();
    try {
        while ((handle = ds.getResource()) != null) {
            if (matchConnection(handle, alloc)) {
                result = handle;
                setResourceStateToBusy(result);
                break;
            } else {
                activeResources.add(handle);
            }
        }
    } finally {
        // return unmatched resources
        for (ResourceHandle activeResource : activeResources) {
            ds.returnResource(activeResource);
        }
        activeResources.clear();
    }
    return result;
}
Also used : ResourceHandle(com.sun.enterprise.resource.ResourceHandle)

Example 9 with ResourceHandle

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

the class ConnectionPool method createSingleResourceAndAdjustPool.

/**
 * 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
 *
 * @param alloc ResourceAllocator to create resource
 * @param spec  ResourceSpec
 * @return newly created resource
 * @throws PoolingException when unable to create a resource
 */
protected ResourceHandle createSingleResourceAndAdjustPool(ResourceAllocator alloc, ResourceSpec spec) throws PoolingException {
    ResourceHandle handle = ds.getResource();
    if (handle != null) {
        ds.removeResource(handle);
    }
    ResourceHandle result = getNewResource(alloc);
    return result;
}
Also used : ResourceHandle(com.sun.enterprise.resource.ResourceHandle)

Example 10 with ResourceHandle

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

the class ConnectionPool method killExtraResources.

/**
 * Kill the extra resources.<br>
 * The maxPoolSize being reduced causes this method to
 * be called
 */
private void killExtraResources(int numToKill) {
    cancelResizerTask();
    ResourceHandle h;
    for (int i = 0; i < numToKill && ((h = ds.getResource()) != null); i++) {
        ds.removeResource(h);
    }
    scheduleResizerTask();
}
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