Search in sources :

Example 11 with PooledObject

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

the class CPDSConnectionFactory method makeObject.

@Override
public synchronized PooledObject<PooledConnectionAndInfo> makeObject() {
    PooledConnectionAndInfo pci;
    try {
        PooledConnection pc = null;
        if (userName == null) {
            pc = cpds.getPooledConnection();
        } else {
            pc = cpds.getPooledConnection(userName, Utils.toString(userPassword));
        }
        if (pc == null) {
            throw new IllegalStateException("Connection pool data source returned null from getPooledConnection");
        }
        // should we add this object as a listener or the pool.
        // consider the validateObject method in decision
        pc.addConnectionEventListener(this);
        pci = new PooledConnectionAndInfo(pc, userName, userPassword);
        pcMap.put(pc, pci);
    } catch (final SQLException e) {
        throw new RuntimeException(e.getMessage());
    }
    return new DefaultPooledObject<>(pci);
}
Also used : DefaultPooledObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject) PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException)

Example 12 with PooledObject

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

the class KeyedCPDSConnectionFactory method makeObject.

/**
 * Creates a new {@link PooledConnectionAndInfo} from the given {@link UserPassKey}.
 *
 * @param upkey
 *            {@link UserPassKey} containing user credentials
 * @throws SQLException
 *             if the connection could not be created.
 * @see org.datanucleus.store.rdbms.datasource.dbcp2.pool2.KeyedPooledObjectFactory#makeObject(java.lang.Object)
 */
@Override
public synchronized PooledObject<PooledConnectionAndInfo> makeObject(final UserPassKey upkey) throws Exception {
    PooledConnectionAndInfo pci = null;
    PooledConnection pc = null;
    final String userName = upkey.getUsername();
    final String password = upkey.getPassword();
    if (userName == null) {
        pc = cpds.getPooledConnection();
    } else {
        pc = cpds.getPooledConnection(userName, password);
    }
    if (pc == null) {
        throw new IllegalStateException("Connection pool data source returned null from getPooledConnection");
    }
    // should we add this object as a listener or the pool.
    // consider the validateObject method in decision
    pc.addConnectionEventListener(this);
    pci = new PooledConnectionAndInfo(pc, userName, upkey.getPasswordCharArray());
    pcMap.put(pc, pci);
    return new DefaultPooledObject<>(pci);
}
Also used : DefaultPooledObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject) PooledConnection(javax.sql.PooledConnection)

Example 13 with PooledObject

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

the class BaseGenericObjectPool method markReturningState.

/**
 * Marks the object as returning to the pool.
 * @param pooledObject instance to return to the keyed pool
 */
protected void markReturningState(final PooledObject<T> pooledObject) {
    synchronized (pooledObject) {
        final PooledObjectState state = pooledObject.getState();
        if (state != PooledObjectState.ALLOCATED) {
            throw new IllegalStateException("Object has already been returned to this pool or is invalid");
        }
        // Keep from being marked abandoned
        pooledObject.markReturning();
    }
}
Also used : PooledObjectState(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObjectState)

Example 14 with PooledObject

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject in project jedis by xetorthio.

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<Jedis> config = new GenericObjectPoolConfig<>();
    config.setMaxTotal(1);
    JedisPool pool = new JedisPool(config, new CrashingJedisPooledObjectFactory());
    Jedis crashingJedis = pool.getResource();
    try {
        crashingJedis.close();
    } catch (Exception ignored) {
    }
    assertEquals(1, destroyed.get());
}
Also used : 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) 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)

Example 15 with PooledObject

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

the class GenericKeyedObjectPool method evict.

/**
 * {@inheritDoc}
 * <p>
 * Successive activations of this method examine objects in keyed sub-pools
 * in sequence, cycling through the keys and examining objects in
 * oldest-to-youngest order within the keyed sub-pools.
 * </p>
 */
@Override
public void evict() throws Exception {
    assertOpen();
    if (getNumIdle() > 0) {
        PooledObject<T> underTest = null;
        final EvictionPolicy<T> evictionPolicy = getEvictionPolicy();
        synchronized (evictionLock) {
            final EvictionConfig evictionConfig = new EvictionConfig(getMinEvictableIdleDuration(), getSoftMinEvictableIdleDuration(), getMinIdlePerKey());
            final boolean testWhileIdle = getTestWhileIdle();
            for (int i = 0, m = getNumTests(); i < m; i++) {
                if (evictionIterator == null || !evictionIterator.hasNext()) {
                    if (evictionKeyIterator == null || !evictionKeyIterator.hasNext()) {
                        final List<K> keyCopy = new ArrayList<>();
                        final Lock readLock = keyLock.readLock();
                        readLock.lock();
                        try {
                            keyCopy.addAll(poolKeyList);
                        } finally {
                            readLock.unlock();
                        }
                        evictionKeyIterator = keyCopy.iterator();
                    }
                    while (evictionKeyIterator.hasNext()) {
                        evictionKey = evictionKeyIterator.next();
                        final ObjectDeque<T> objectDeque = poolMap.get(evictionKey);
                        if (objectDeque == null) {
                            continue;
                        }
                        final Deque<PooledObject<T>> idleObjects = objectDeque.getIdleObjects();
                        evictionIterator = new EvictionIterator(idleObjects);
                        if (evictionIterator.hasNext()) {
                            break;
                        }
                        evictionIterator = null;
                    }
                }
                if (evictionIterator == null) {
                    // Pools exhausted
                    return;
                }
                final Deque<PooledObject<T>> idleObjects;
                try {
                    underTest = evictionIterator.next();
                    idleObjects = evictionIterator.getIdleObjects();
                } catch (final NoSuchElementException nsee) {
                    // Object was borrowed in another thread
                    // Don't count this as an eviction test so reduce i;
                    i--;
                    evictionIterator = null;
                    continue;
                }
                if (!underTest.startEvictionTest()) {
                    // Object was borrowed in another thread
                    // Don't count this as an eviction test so reduce i;
                    i--;
                    continue;
                }
                // User provided eviction policy could throw all sorts of
                // crazy exceptions. Protect against such an exception
                // killing the eviction thread.
                boolean evict;
                try {
                    evict = evictionPolicy.evict(evictionConfig, underTest, poolMap.get(evictionKey).getIdleObjects().size());
                } catch (final Throwable t) {
                    // Slightly convoluted as SwallowedExceptionListener
                    // uses Exception rather than Throwable
                    PoolUtils.checkRethrow(t);
                    swallowException(new Exception(t));
                    // Don't evict on error conditions
                    evict = false;
                }
                if (evict) {
                    destroy(evictionKey, underTest, true, DestroyMode.NORMAL);
                    destroyedByEvictorCount.incrementAndGet();
                } else {
                    if (testWhileIdle) {
                        boolean active = false;
                        try {
                            factory.activateObject(evictionKey, underTest);
                            active = true;
                        } catch (final Exception e) {
                            destroy(evictionKey, underTest, true, DestroyMode.NORMAL);
                            destroyedByEvictorCount.incrementAndGet();
                        }
                        if (active) {
                            boolean validate = false;
                            Throwable validationThrowable = null;
                            try {
                                validate = factory.validateObject(evictionKey, underTest);
                            } catch (final Throwable t) {
                                PoolUtils.checkRethrow(t);
                                validationThrowable = t;
                            }
                            if (!validate) {
                                destroy(evictionKey, underTest, true, DestroyMode.NORMAL);
                                destroyedByEvictorCount.incrementAndGet();
                                if (validationThrowable != null) {
                                    if (validationThrowable instanceof RuntimeException) {
                                        throw (RuntimeException) validationThrowable;
                                    }
                                    throw (Error) validationThrowable;
                                }
                            } else {
                                try {
                                    factory.passivateObject(evictionKey, underTest);
                                } catch (final Exception e) {
                                    destroy(evictionKey, underTest, true, DestroyMode.NORMAL);
                                    destroyedByEvictorCount.incrementAndGet();
                                }
                            }
                        }
                    }
                    if (!underTest.endEvictionTest(idleObjects)) {
                    // TODO - May need to add code here once additional
                    // states are used
                    }
                }
            }
        }
    }
    final AbandonedConfig ac = this.abandonedConfig;
    if (ac != null && ac.getRemoveAbandonedOnMaintenance()) {
        removeAbandoned(ac);
    }
}
Also used : ArrayList(java.util.ArrayList) NoSuchElementException(java.util.NoSuchElementException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock) PooledObject(org.apache.tomcat.dbcp.pool2.PooledObject) NoSuchElementException(java.util.NoSuchElementException)

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