Example usage for org.apache.commons.pool2 PooledObjectState ALLOCATED

List of usage examples for org.apache.commons.pool2 PooledObjectState ALLOCATED

Introduction

In this page you can find the example usage for org.apache.commons.pool2 PooledObjectState ALLOCATED.

Prototype

PooledObjectState ALLOCATED

To view the source code for org.apache.commons.pool2 PooledObjectState ALLOCATED.

Click Source Link

Document

In use.

Usage

From source file:JDBCPool.dbcp.demo.sourcecode.GenericObjectPool.java

/**
 * {@inheritDoc}//from  w ww . j a  v  a2 s .c o m
 * <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 SwallowedExceptionListener}.
 */
@Override
public void returnObject(T obj) {
    PooledObject<T> p = allObjects.get(new IdentityWrapper<T>(obj));

    if (p == null) {
        if (!isAbandonedConfig()) {
            throw new IllegalStateException("Returned object not currently part of this pool");
        } else {
            return; // Object was abandoned and removed
        }
    }

    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");
        } else {
            p.markReturning(); // Keep from being marked abandoned
        }
    }

    long activeTime = p.getActiveTimeMillis();

    if (getTestOnReturn()) {
        if (!factory.validateObject(p)) {
            try {
                destroy(p);
            } catch (Exception e) {
                swallowException(e);
            }
            try {
                ensureIdle(1, false);
            } catch (Exception e) {
                swallowException(e);
            }
            updateStatsReturn(activeTime);
            return;
        }
    }

    try {
        factory.passivateObject(p);
    } catch (Exception e1) {
        swallowException(e1);
        try {
            destroy(p);
        } catch (Exception e) {
            swallowException(e);
        }
        try {
            ensureIdle(1, false);
        } catch (Exception e) {
            swallowException(e);
        }
        updateStatsReturn(activeTime);
        return;
    }

    if (!p.deallocate()) {
        throw new IllegalStateException("Object has already been returned to this pool or is invalid");
    }

    int maxIdleSave = getMaxIdle();
    if (isClosed() || maxIdleSave > -1 && maxIdleSave <= idleObjects.size()) {
        try {
            destroy(p);
        } catch (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);
}

From source file:JDBCPool.dbcp.demo.sourcecode.GenericObjectPool.java

/**
 * Recover abandoned objects which have been checked out but
 * not used since longer than the removeAbandonedTimeout.
 *
 * @param ac The configuration to use to identify abandoned objects
 *//*  w  w  w  .  j  a  v a 2 s .  c  o m*/
private void removeAbandoned(AbandonedConfig ac) {
    // Generate a list of abandoned objects to remove
    final long now = System.currentTimeMillis();
    final long timeout = now - (ac.getRemoveAbandonedTimeout() * 1000L);
    ArrayList<PooledObject<T>> remove = new ArrayList<PooledObject<T>>();
    Iterator<PooledObject<T>> it = allObjects.values().iterator();
    while (it.hasNext()) {
        PooledObject<T> pooledObject = it.next();
        synchronized (pooledObject) {
            if (pooledObject.getState() == PooledObjectState.ALLOCATED
                    && pooledObject.getLastUsedTime() <= timeout) {
                pooledObject.markAbandoned();
                remove.add(pooledObject);
            }
        }
    }

    // Now remove the abandoned objects
    Iterator<PooledObject<T>> itr = remove.iterator();
    while (itr.hasNext()) {
        PooledObject<T> pooledObject = itr.next();
        if (ac.getLogAbandoned()) {
            pooledObject.printStackTrace(ac.getLogWriter());
        }
        try {
            invalidateObject(pooledObject.getObject());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}