Example usage for org.apache.commons.pool2.impl AbandonedConfig getRemoveAbandonedTimeout

List of usage examples for org.apache.commons.pool2.impl AbandonedConfig getRemoveAbandonedTimeout

Introduction

In this page you can find the example usage for org.apache.commons.pool2.impl AbandonedConfig getRemoveAbandonedTimeout.

Prototype

public int getRemoveAbandonedTimeout() 

Source Link

Document

Timeout in seconds before an abandoned object can be removed.

The time of most recent use of an object is the maximum (latest) of TrackedUse#getLastUsed() (if this class of the object implements TrackedUse) and the time when the object was borrowed from the pool.

The default value is 300 seconds.

Usage

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

/**
 * Obtain the timeout before which an object will be considered to be
 * abandoned by this pool./* w  w  w.j av  a  2 s .  c  o  m*/
 *
 * @return The abandoned object timeout in seconds if abandoned object
 *         removal is configured for this pool; Integer.MAX_VALUE otherwise.
 *
 * @see AbandonedConfig#getRemoveAbandonedTimeout()
 */
@Override
public int getRemoveAbandonedTimeout() {
    AbandonedConfig ac = this.abandonedConfig;
    return ac != null ? ac.getRemoveAbandonedTimeout() : Integer.MAX_VALUE;
}

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 a2 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();
        }
    }
}

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

/**
 * Sets the abandoned object removal configuration.
 *
 * @param abandonedConfig the new configuration to use. This is used by value.
 *
 * @see AbandonedConfig//ww w .  j a va 2s .c  o  m
 */
public void setAbandonedConfig(AbandonedConfig abandonedConfig) throws IllegalArgumentException {
    if (abandonedConfig == null) {
        this.abandonedConfig = null;
    } else {
        this.abandonedConfig = new AbandonedConfig();
        this.abandonedConfig.setLogAbandoned(abandonedConfig.getLogAbandoned());
        this.abandonedConfig.setLogWriter(abandonedConfig.getLogWriter());
        this.abandonedConfig.setRemoveAbandonedOnBorrow(abandonedConfig.getRemoveAbandonedOnBorrow());
        this.abandonedConfig.setRemoveAbandonedOnMaintenance(abandonedConfig.getRemoveAbandonedOnMaintenance());
        this.abandonedConfig.setRemoveAbandonedTimeout(abandonedConfig.getRemoveAbandonedTimeout());
        this.abandonedConfig.setUseUsageTracking(abandonedConfig.getUseUsageTracking());
    }
}