Search in sources :

Example 1 with GenericKeyedObjectPool

use of org.apache.commons.pool2.impl.GenericKeyedObjectPool in project tomcat by apache.

the class SharedPoolDataSource method registerPool.

private void registerPool(final String username, final String password) throws NamingException, SQLException {
    final ConnectionPoolDataSource cpds = testCPDS(username, password);
    // Create an object pool to contain our PooledConnections
    factory = new KeyedCPDSConnectionFactory(cpds, getValidationQuery(), getValidationQueryTimeout(), isRollbackAfterValidation());
    factory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis());
    final GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setBlockWhenExhausted(getDefaultBlockWhenExhausted());
    config.setEvictionPolicyClassName(getDefaultEvictionPolicyClassName());
    config.setLifo(getDefaultLifo());
    config.setMaxIdlePerKey(getDefaultMaxIdle());
    config.setMaxTotal(getMaxTotal());
    config.setMaxTotalPerKey(getDefaultMaxTotal());
    config.setMaxWaitMillis(getDefaultMaxWaitMillis());
    config.setMinEvictableIdleTimeMillis(getDefaultMinEvictableIdleTimeMillis());
    config.setMinIdlePerKey(getDefaultMinIdle());
    config.setNumTestsPerEvictionRun(getDefaultNumTestsPerEvictionRun());
    config.setSoftMinEvictableIdleTimeMillis(getDefaultSoftMinEvictableIdleTimeMillis());
    config.setTestOnCreate(getDefaultTestOnCreate());
    config.setTestOnBorrow(getDefaultTestOnBorrow());
    config.setTestOnReturn(getDefaultTestOnReturn());
    config.setTestWhileIdle(getDefaultTestWhileIdle());
    config.setTimeBetweenEvictionRunsMillis(getDefaultTimeBetweenEvictionRunsMillis());
    final KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> tmpPool = new GenericKeyedObjectPool<>(factory, config);
    factory.setPool(tmpPool);
    pool = tmpPool;
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) GenericKeyedObjectPoolConfig(org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig) GenericKeyedObjectPool(org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool)

Example 2 with GenericKeyedObjectPool

use of org.apache.commons.pool2.impl.GenericKeyedObjectPool in project tomcat by apache.

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 (final SQLException sqle) {
        // Make sure the connection is closed
        try {
            conn.close();
        } catch (final SQLException ignore) {
        // ignore
        }
        // Rethrow original exception so it is visible to caller
        throw sqle;
    }
    final long connIndex = connectionIndex.getAndIncrement();
    if (poolStatements) {
        conn = new PoolingConnection(conn);
        final GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
        config.setMaxTotalPerKey(-1);
        config.setBlockWhenExhausted(false);
        config.setMaxWaitMillis(0);
        config.setMaxIdlePerKey(1);
        config.setMaxTotal(maxOpenPreparedStatements);
        if (dataSourceJmxName != null) {
            final 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);
        }
        final 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);
    }
    final PoolableConnection pc = new PoolableConnection(conn, _pool, connJmxName, _disconnectionSqlCodes, _fastFailValidation);
    pc.setCacheState(_cacheState);
    return new DefaultPooledObject<>(pc);
}
Also used : GenericKeyedObjectPoolConfig(org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig) SQLException(java.sql.SQLException) GenericKeyedObjectPool(org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool) Connection(java.sql.Connection) ObjectName(javax.management.ObjectName) DefaultPooledObject(org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)

Example 3 with GenericKeyedObjectPool

use of org.apache.commons.pool2.impl.GenericKeyedObjectPool in project jmeter by apache.

the class AbstractGraphiteMetricsSender method createSocketOutputStreamPool.

/**
     * Create a new keyed pool of {@link SocketOutputStream}s using a
     * {@link SocketOutputStreamPoolFactory}. The keys for the pool are
     * {@link SocketConnectionInfos} instances.
     *
     * @return GenericKeyedObjectPool the newly generated pool
     */
protected GenericKeyedObjectPool<SocketConnectionInfos, SocketOutputStream> createSocketOutputStreamPool() {
    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setTestOnBorrow(true);
    config.setTestWhileIdle(true);
    config.setMaxTotalPerKey(-1);
    config.setMaxTotal(-1);
    config.setMaxIdlePerKey(-1);
    config.setMinEvictableIdleTimeMillis(TimeUnit.MINUTES.toMillis(3));
    config.setTimeBetweenEvictionRunsMillis(TimeUnit.MINUTES.toMillis(3));
    return new GenericKeyedObjectPool<>(new SocketOutputStreamPoolFactory(SOCKET_CONNECT_TIMEOUT_MS, SOCKET_TIMEOUT), config);
}
Also used : GenericKeyedObjectPoolConfig(org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig) GenericKeyedObjectPool(org.apache.commons.pool2.impl.GenericKeyedObjectPool)

Example 4 with GenericKeyedObjectPool

use of org.apache.commons.pool2.impl.GenericKeyedObjectPool 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 5 with GenericKeyedObjectPool

use of org.apache.commons.pool2.impl.GenericKeyedObjectPool in project openhab1-addons by openhab.

the class ModbusBinding method reconstructConnectionPool.

private static void reconstructConnectionPool() {
    connectionFactory = new ModbusSlaveConnectionFactoryImpl();
    GenericKeyedObjectPool<ModbusSlaveEndpoint, ModbusSlaveConnection> genericKeyedObjectPool = new GenericKeyedObjectPool<ModbusSlaveEndpoint, ModbusSlaveConnection>(connectionFactory, poolConfig);
    genericKeyedObjectPool.setSwallowedExceptionListener(new SwallowedExceptionListener() {

        @Override
        public void onSwallowException(Exception e) {
            logger.error("Connection pool swallowed unexpected exception: {}", e.getMessage());
        }
    });
    connectionPool = genericKeyedObjectPool;
}
Also used : GenericKeyedObjectPool(org.apache.commons.pool2.impl.GenericKeyedObjectPool) ModbusSlaveEndpoint(org.openhab.binding.modbus.internal.pooling.ModbusSlaveEndpoint) SwallowedExceptionListener(org.apache.commons.pool2.SwallowedExceptionListener) ModbusSlaveConnection(net.wimpi.modbus.net.ModbusSlaveConnection) ModbusSlaveConnectionFactoryImpl(org.openhab.binding.modbus.internal.pooling.ModbusSlaveConnectionFactoryImpl) ConfigurationException(org.osgi.service.cm.ConfigurationException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

GenericKeyedObjectPool (org.apache.commons.pool2.impl.GenericKeyedObjectPool)2 GenericKeyedObjectPool (org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool)2 GenericKeyedObjectPoolConfig (org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 NoSuchElementException (java.util.NoSuchElementException)1 Connection (javax.jms.Connection)1 JMSException (javax.jms.JMSException)1 QueueConnection (javax.jms.QueueConnection)1 TopicConnection (javax.jms.TopicConnection)1 ObjectName (javax.management.ObjectName)1 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)1 ModbusSlaveConnection (net.wimpi.modbus.net.ModbusSlaveConnection)1 KeyedPooledObjectFactory (org.apache.commons.pool2.KeyedPooledObjectFactory)1 PooledObject (org.apache.commons.pool2.PooledObject)1 SwallowedExceptionListener (org.apache.commons.pool2.SwallowedExceptionListener)1 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)1 GenericKeyedObjectPoolConfig (org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig)1 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)1 ModbusSlaveConnectionFactoryImpl (org.openhab.binding.modbus.internal.pooling.ModbusSlaveConnectionFactoryImpl)1