Search in sources :

Example 1 with DefaultPooledObject

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject in project karaf by apache.

the class PooledConnectionFactory method initConnectionsPool.

public void initConnectionsPool() {
    if (this.connectionsPool == null) {
        this.connectionsPool = new GenericKeyedObjectPool<>(new KeyedPooledObjectFactory<ConnectionKey, ConnectionPool>() {

            @Override
            public void activateObject(ConnectionKey key, PooledObject<ConnectionPool> connection) throws Exception {
            }

            @Override
            public void destroyObject(ConnectionKey key, PooledObject<ConnectionPool> connection) throws Exception {
                try {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Destroying connection: {}", connection);
                    }
                    connection.getObject().close();
                } catch (Exception e) {
                    LOG.warn("Close connection failed for connection: " + connection + ". This exception will be ignored.", e);
                }
            }

            @Override
            public PooledObject<ConnectionPool> makeObject(ConnectionKey key) throws Exception {
                Connection delegate = createConnection(key);
                ConnectionPool connection = createConnectionPool(delegate);
                connection.setIdleTimeout(getIdleTimeout());
                connection.setExpiryTimeout(getExpiryTimeout());
                connection.setMaximumActiveSessionPerConnection(getMaximumActiveSessionPerConnection());
                connection.setBlockIfSessionPoolIsFull(isBlockIfSessionPoolIsFull());
                if (isBlockIfSessionPoolIsFull() && getBlockIfSessionPoolIsFullTimeout() > 0) {
                    connection.setBlockIfSessionPoolIsFullTimeout(getBlockIfSessionPoolIsFullTimeout());
                }
                connection.setUseAnonymousProducers(isUseAnonymousProducers());
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Created new connection: {}", connection);
                }
                return new DefaultPooledObject<>(connection);
            }

            @Override
            public void passivateObject(ConnectionKey key, PooledObject<ConnectionPool> connection) throws Exception {
            }

            @Override
            public boolean validateObject(ConnectionKey key, PooledObject<ConnectionPool> connection) {
                if (connection != null && connection.getObject() != null && connection.getObject().expiredCheck()) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Connection has expired: {} and will be destroyed", connection);
                    }
                    return false;
                }
                return true;
            }
        });
        // Set max idle (not max active) since our connections always idle in the pool.
        this.connectionsPool.setMaxIdlePerKey(1);
        // We always want our validate method to control when idle objects are evicted.
        this.connectionsPool.setTestOnBorrow(true);
        this.connectionsPool.setTestWhileIdle(true);
    }
}
Also used : DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) KeyedPooledObjectFactory(org.apache.commons.pool2.KeyedPooledObjectFactory) JMSException(javax.jms.JMSException)

Example 2 with DefaultPooledObject

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject 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 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) JedisExhaustedPoolException(redis.clients.jedis.exceptions.JedisExhaustedPoolException) Test(org.junit.Test)

Example 3 with DefaultPooledObject

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

the class PoolableConnectionFactory method makeObject.

@Override
public PooledObject<PoolableConnection> makeObject() throws Exception {
    Connection conn = _connFactory.createConnection();
    if (conn == null) {
        throw new IllegalStateException("Connection factory returned null from createConnection");
    }
    try {
        initializeConnection(conn);
    } catch (SQLException sqle) {
        // Make sure the connection is closed
        try {
            conn.close();
        } catch (SQLException ignore) {
        // ignore
        }
        // Rethrow original exception so it is visible to caller
        throw sqle;
    }
    long connIndex = connectionIndex.getAndIncrement();
    if (poolStatements) {
        conn = new PoolingConnection(conn);
        GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
        config.setMaxTotalPerKey(-1);
        config.setBlockWhenExhausted(false);
        config.setMaxWaitMillis(0);
        config.setMaxIdlePerKey(1);
        config.setMaxTotal(maxOpenPreparedStatements);
        if (dataSourceJmxName != null) {
            StringBuilder base = new StringBuilder(dataSourceJmxName.toString());
            base.append(Constants.JMX_CONNECTION_BASE_EXT);
            base.append(Long.toString(connIndex));
            config.setJmxNameBase(base.toString());
            config.setJmxNamePrefix(Constants.JMX_STATEMENT_POOL_PREFIX);
        } else {
            config.setJmxEnabled(false);
        }
        KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = new GenericKeyedObjectPool<>((PoolingConnection) conn, config);
        ((PoolingConnection) conn).setStatementPool(stmtPool);
        ((PoolingConnection) conn).setCacheState(_cacheState);
    }
    // Register this connection with JMX
    ObjectName connJmxName;
    if (dataSourceJmxName == null) {
        connJmxName = null;
    } else {
        connJmxName = new ObjectName(dataSourceJmxName.toString() + Constants.JMX_CONNECTION_BASE_EXT + connIndex);
    }
    PoolableConnection pc = new PoolableConnection(conn, _pool, connJmxName, _disconnectionSqlCodes, _fastFailValidation);
    return new DefaultPooledObject<>(pc);
}
Also used : GenericKeyedObjectPoolConfig(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPoolConfig) SQLException(java.sql.SQLException) GenericKeyedObjectPool(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPool) Connection(java.sql.Connection) ObjectName(javax.management.ObjectName) DefaultPooledObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject)

Example 4 with DefaultPooledObject

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject in project x-pipe by ctripcorp.

the class NettyKeyedPoolClientFactory method makeObject.

@Override
public PooledObject<NettyClient> makeObject(InetSocketAddress key) throws Exception {
    ChannelFuture f = b.connect(key);
    f.get(connectTimeoutMilli, TimeUnit.MILLISECONDS);
    Channel channel = f.channel();
    logger.debug("[makeObject]{}", channel);
    NettyClient nettyClient = new DefaultNettyClient(channel);
    channel.attr(NettyClientHandler.KEY_CLIENT).set(nettyClient);
    return new DefaultPooledObject<NettyClient>(nettyClient);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel)

Example 5 with DefaultPooledObject

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject in project new-cloud by xie-summer.

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

DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)8 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)6 PooledConnection (javax.sql.PooledConnection)5 PooledObject (org.apache.commons.pool2.PooledObject)5 URISyntaxException (java.net.URISyntaxException)4 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)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 ObjectName (javax.management.ObjectName)3 DefaultPooledObject (org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject)3 Jedis (redis.clients.jedis.Jedis)3 JedisPool (redis.clients.jedis.JedisPool)3 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)3 Channel (io.netty.channel.Channel)2 ChannelFuture (io.netty.channel.ChannelFuture)2