Search in sources :

Example 16 with PooledObject

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject 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>
 * <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>
 * <p>
 * Exceptions encountered destroying objects for any reason are swallowed
 * but notified via a {@link SwallowedExceptionListener}.
 * </p>
 *
 * @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);
    if (objectDeque == null) {
        throw new IllegalStateException("No keyed pool found under the given 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");
    }
    markReturningState(p);
    final Duration activeTime = p.getActiveDuration();
    try {
        if (getTestOnReturn() && !factory.validateObject(key, p)) {
            try {
                destroy(key, p, true, DestroyMode.NORMAL);
            } catch (final Exception e) {
                swallowException(e);
            }
            whenWaitersAddObject(key, objectDeque.idleObjects);
            return;
        }
        try {
            factory.passivateObject(key, p);
        } catch (final Exception e1) {
            swallowException(e1);
            try {
                destroy(key, p, true, DestroyMode.NORMAL);
            } catch (final Exception e) {
                swallowException(e);
            }
            whenWaitersAddObject(key, objectDeque.idleObjects);
            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, DestroyMode.NORMAL);
            } 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) Duration(java.time.Duration) NoSuchElementException(java.util.NoSuchElementException)

Example 17 with PooledObject

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject in project tomcat by apache.

the class BaseGenericObjectPool method createRemoveList.

/**
 * Creates a list of pooled objects to remove based on their state.
 * @param abandonedConfig The abandoned configuration.
 * @param allObjects PooledObject instances to consider.
 * @return a list of pooled objects to remove based on their state.
 */
ArrayList<PooledObject<T>> createRemoveList(final AbandonedConfig abandonedConfig, final Map<IdentityWrapper<T>, PooledObject<T>> allObjects) {
    final Instant timeout = Instant.now().minus(abandonedConfig.getRemoveAbandonedTimeoutDuration());
    final ArrayList<PooledObject<T>> remove = new ArrayList<>();
    allObjects.values().forEach(pooledObject -> {
        synchronized (pooledObject) {
            if (pooledObject.getState() == PooledObjectState.ALLOCATED && pooledObject.getLastUsedInstant().compareTo(timeout) <= 0) {
                pooledObject.markAbandoned();
                remove.add(pooledObject);
            }
        }
    });
    return remove;
}
Also used : PooledObject(org.apache.tomcat.dbcp.pool2.PooledObject) Instant(java.time.Instant) ArrayList(java.util.ArrayList)

Example 18 with PooledObject

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject in project cachecloud by sohutv.

the class JedisPoolTest method returnResourceDestroysResourceOnException.

@Test
public void returnResourceDestroysResourceOnException() {
    class CrashingJedis extends Jedis {

        @Override
        public void resetState() {
            throw new RuntimeException();
        }
    }
    final AtomicInteger destroyed = new AtomicInteger(0);
    class CrashingJedisPooledObjectFactory implements PooledObjectFactory<Jedis> {

        @Override
        public PooledObject<Jedis> makeObject() throws Exception {
            return new DefaultPooledObject<Jedis>(new CrashingJedis());
        }

        @Override
        public void destroyObject(PooledObject<Jedis> p) throws Exception {
            destroyed.incrementAndGet();
        }

        @Override
        public boolean validateObject(PooledObject<Jedis> p) {
            return true;
        }

        @Override
        public void activateObject(PooledObject<Jedis> p) throws Exception {
        }

        @Override
        public void passivateObject(PooledObject<Jedis> p) throws Exception {
        }
    }
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
    pool.initPool(config, new CrashingJedisPooledObjectFactory());
    Jedis crashingJedis = pool.getResource();
    try {
        crashingJedis.close();
    } catch (Exception ignored) {
    }
    assertEquals(destroyed.get(), 1);
}
Also used : Jedis(redis.clients.jedis.Jedis) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) PooledObjectFactory(org.apache.commons.pool2.PooledObjectFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) URISyntaxException(java.net.URISyntaxException) JedisException(redis.clients.jedis.exceptions.JedisException) InvalidURIException(redis.clients.jedis.exceptions.InvalidURIException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) Test(org.junit.Test)

Aggregations

NoSuchElementException (java.util.NoSuchElementException)5 PooledObject (org.apache.commons.pool2.PooledObject)5 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)5 DefaultPooledObject (org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject)5 URISyntaxException (java.net.URISyntaxException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 PooledObjectFactory (org.apache.commons.pool2.PooledObjectFactory)4 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)4 Test (org.junit.Test)4 InvalidURIException (redis.clients.jedis.exceptions.InvalidURIException)4 JedisException (redis.clients.jedis.exceptions.JedisException)4 ArrayList (java.util.ArrayList)3 PooledConnection (javax.sql.PooledConnection)3 PooledObject (org.apache.tomcat.dbcp.pool2.PooledObject)3 PooledObjectState (org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObjectState)3 Jedis (redis.clients.jedis.Jedis)3 JedisPool (redis.clients.jedis.JedisPool)3 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)3 CallableStatement (java.sql.CallableStatement)2 Connection (java.sql.Connection)2