Search in sources :

Example 1 with PooledObjectState

use of org.apache.tomcat.dbcp.pool2.PooledObjectState in project tomcat by apache.

the class GenericObjectPool method returnObject.

/**
     * {@inheritDoc}
     * <p>
     * If {@link #getMaxIdle() maxIdle} is set to a positive value and the
     * number of idle instances has reached this value, the returning instance
     * is destroyed.
     * <p>
     * If {@link #getTestOnReturn() testOnReturn} == true, the returning
     * instance is validated before being returned to the idle instance pool. In
     * this case, if validation fails, the instance is destroyed.
     * <p>
     * Exceptions encountered destroying objects for any reason are swallowed
     * but notified via a
     * {@link org.apache.tomcat.dbcp.pool2.SwallowedExceptionListener}.
     */
@Override
public void returnObject(final T obj) {
    final PooledObject<T> p = allObjects.get(new IdentityWrapper<>(obj));
    if (p == null) {
        if (!isAbandonedConfig()) {
            throw new IllegalStateException("Returned object not currently part of this pool");
        }
        // Object was abandoned and removed
        return;
    }
    synchronized (p) {
        final PooledObjectState state = p.getState();
        if (state != PooledObjectState.ALLOCATED) {
            throw new IllegalStateException("Object has already been returned to this pool or is invalid");
        }
        // Keep from being marked abandoned
        p.markReturning();
    }
    final long activeTime = p.getActiveTimeMillis();
    if (getTestOnReturn()) {
        if (!factory.validateObject(p)) {
            try {
                destroy(p);
            } catch (final Exception e) {
                swallowException(e);
            }
            try {
                ensureIdle(1, false);
            } catch (final Exception e) {
                swallowException(e);
            }
            updateStatsReturn(activeTime);
            return;
        }
    }
    try {
        factory.passivateObject(p);
    } catch (final Exception e1) {
        swallowException(e1);
        try {
            destroy(p);
        } catch (final Exception e) {
            swallowException(e);
        }
        try {
            ensureIdle(1, false);
        } catch (final Exception e) {
            swallowException(e);
        }
        updateStatsReturn(activeTime);
        return;
    }
    if (!p.deallocate()) {
        throw new IllegalStateException("Object has already been returned to this pool or is invalid");
    }
    final int maxIdleSave = getMaxIdle();
    if (isClosed() || maxIdleSave > -1 && maxIdleSave <= idleObjects.size()) {
        try {
            destroy(p);
        } catch (final Exception e) {
            swallowException(e);
        }
    } else {
        if (getLifo()) {
            idleObjects.addFirst(p);
        } else {
            idleObjects.addLast(p);
        }
        if (isClosed()) {
            // Pool closed while object was being added to idle objects.
            // Make sure the returned object is destroyed rather than left
            // in the idle object pool (which would effectively be a leak)
            clear();
        }
    }
    updateStatsReturn(activeTime);
}
Also used : PooledObjectState(org.apache.tomcat.dbcp.pool2.PooledObjectState) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with PooledObjectState

use of org.apache.tomcat.dbcp.pool2.PooledObjectState in project tomcat by apache.

the class GenericKeyedObjectPool method returnObject.

/**
     * Returns an object to a keyed sub-pool.
     * <p>
     * If {@link #getMaxIdlePerKey() maxIdle} is set to a positive value and the
     * number of idle instances under the given key has reached this value, the
     * returning instance is destroyed.
     * <p>
     * If {@link #getTestOnReturn() testOnReturn} == true, the returning
     * instance is validated before being returned to the idle instance sub-pool
     * under the given key. In this case, if validation fails, the instance is
     * destroyed.
     * <p>
     * Exceptions encountered destroying objects for any reason are swallowed
     * but notified via a
     * {@link org.apache.tomcat.dbcp.pool2.SwallowedExceptionListener}.
     *
     * @param key pool key
     * @param obj instance to return to the keyed pool
     *
     * @throws IllegalStateException if an object is returned to the pool that
     *                               was not borrowed from it or if an object is
     *                               returned to the pool multiple times
     */
@Override
public void returnObject(final K key, final T obj) {
    final ObjectDeque<T> objectDeque = poolMap.get(key);
    final PooledObject<T> p = objectDeque.getAllObjects().get(new IdentityWrapper<>(obj));
    if (p == null) {
        throw new IllegalStateException("Returned object not currently part of this pool");
    }
    synchronized (p) {
        final PooledObjectState state = p.getState();
        if (state != PooledObjectState.ALLOCATED) {
            throw new IllegalStateException("Object has already been returned to this pool or is invalid");
        }
        // Keep from being marked abandoned (once GKOP does this)
        p.markReturning();
    }
    final long activeTime = p.getActiveTimeMillis();
    try {
        if (getTestOnReturn()) {
            if (!factory.validateObject(key, p)) {
                try {
                    destroy(key, p, true);
                } catch (final Exception e) {
                    swallowException(e);
                }
                if (objectDeque.idleObjects.hasTakeWaiters()) {
                    try {
                        addObject(key);
                    } catch (final Exception e) {
                        swallowException(e);
                    }
                }
                return;
            }
        }
        try {
            factory.passivateObject(key, p);
        } catch (final Exception e1) {
            swallowException(e1);
            try {
                destroy(key, p, true);
            } catch (final Exception e) {
                swallowException(e);
            }
            if (objectDeque.idleObjects.hasTakeWaiters()) {
                try {
                    addObject(key);
                } catch (final Exception e) {
                    swallowException(e);
                }
            }
            return;
        }
        if (!p.deallocate()) {
            throw new IllegalStateException("Object has already been returned to this pool");
        }
        final int maxIdle = getMaxIdlePerKey();
        final LinkedBlockingDeque<PooledObject<T>> idleObjects = objectDeque.getIdleObjects();
        if (isClosed() || maxIdle > -1 && maxIdle <= idleObjects.size()) {
            try {
                destroy(key, p, true);
            } catch (final Exception e) {
                swallowException(e);
            }
        } else {
            if (getLifo()) {
                idleObjects.addFirst(p);
            } else {
                idleObjects.addLast(p);
            }
            if (isClosed()) {
                // Pool closed while object was being added to idle objects.
                // Make sure the returned object is destroyed rather than left
                // in the idle object pool (which would effectively be a leak)
                clear(key);
            }
        }
    } finally {
        if (hasBorrowWaiters()) {
            reuseCapacity();
        }
        updateStatsReturn(activeTime);
    }
}
Also used : PooledObject(org.apache.tomcat.dbcp.pool2.PooledObject) PooledObjectState(org.apache.tomcat.dbcp.pool2.PooledObjectState) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

NoSuchElementException (java.util.NoSuchElementException)2 PooledObjectState (org.apache.tomcat.dbcp.pool2.PooledObjectState)2 PooledObject (org.apache.tomcat.dbcp.pool2.PooledObject)1