Search in sources :

Example 1 with GenericKeyedObjectPoolConfig

use of org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig in project tomcat by apache.

the class DriverAdapterCPDS method getPooledConnection.

/**
     * Attempt to establish a database connection.
     * @param username name to be used for the connection
     * @param pass password to be used fur the connection
     */
@Override
public PooledConnection getPooledConnection(final String username, final String pass) throws SQLException {
    getConnectionCalled = true;
    PooledConnectionImpl pci = null;
    // exception upon first invocation.
    try {
        if (connectionProperties != null) {
            connectionProperties.put("user", username);
            connectionProperties.put("password", pass);
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), connectionProperties));
        } else {
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), username, pass));
        }
        pci.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    } catch (final ClassCircularityError e) {
        if (connectionProperties != null) {
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), connectionProperties));
        } else {
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), username, pass));
        }
        pci.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    }
    KeyedObjectPool<PStmtKeyCPDS, PoolablePreparedStatement<PStmtKeyCPDS>> stmtPool = null;
    if (isPoolPreparedStatements()) {
        final GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
        config.setMaxTotalPerKey(Integer.MAX_VALUE);
        config.setBlockWhenExhausted(false);
        config.setMaxWaitMillis(0);
        config.setMaxIdlePerKey(getMaxIdle());
        if (getMaxPreparedStatements() <= 0) {
            // since there is no limit, create a prepared statement pool with an eviction thread
            //  evictor settings are the same as the connection pool settings.
            config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
            config.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun());
            config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
        } else {
            // since there is limit, create a prepared statement pool without an eviction thread
            //  pool has LRU functionality so when the limit is reached, 15% of the pool is cleared.
            // see org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool.clearOldest method
            config.setMaxTotal(getMaxPreparedStatements());
            config.setTimeBetweenEvictionRunsMillis(-1);
            config.setNumTestsPerEvictionRun(0);
            config.setMinEvictableIdleTimeMillis(0);
        }
        stmtPool = new GenericKeyedObjectPool<>(pci, config);
        pci.setStatementPool(stmtPool);
    }
    return pci;
}
Also used : GenericKeyedObjectPoolConfig(org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig) PoolablePreparedStatement(org.apache.tomcat.dbcp.dbcp2.PoolablePreparedStatement)

Example 2 with GenericKeyedObjectPoolConfig

use of org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig 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 3 with GenericKeyedObjectPoolConfig

use of org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig in project datanucleus-rdbms by datanucleus.

the class DriverAdapterCPDS method getPooledConnection.

/**
 * Attempt to establish a database connection.
 * @param username name to be used for the connection
 * @param pass password to be used fur the connection
 */
@Override
public PooledConnection getPooledConnection(String username, String pass) throws SQLException {
    getConnectionCalled = true;
    PooledConnectionImpl pci = null;
    // exception upon first invocation.
    try {
        if (connectionProperties != null) {
            connectionProperties.put("user", username);
            connectionProperties.put("password", pass);
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), connectionProperties));
        } else {
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), username, pass));
        }
        pci.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    } catch (ClassCircularityError e) {
        if (connectionProperties != null) {
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), connectionProperties));
        } else {
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), username, pass));
        }
        pci.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    }
    KeyedObjectPool<PStmtKeyCPDS, PoolablePreparedStatement<PStmtKeyCPDS>> stmtPool = null;
    if (isPoolPreparedStatements()) {
        GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
        config.setMaxTotalPerKey(Integer.MAX_VALUE);
        config.setBlockWhenExhausted(false);
        config.setMaxWaitMillis(0);
        config.setMaxIdlePerKey(getMaxIdle());
        if (getMaxPreparedStatements() <= 0) {
            // since there is no limit, create a prepared statement pool with an eviction thread
            // evictor settings are the same as the connection pool settings.
            config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
            config.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun());
            config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
        } else {
            // since there is limit, create a prepared statement pool without an eviction thread
            // pool has LRU functionality so when the limit is reached, 15% of the pool is cleared.
            // see org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPool.clearOldest method
            config.setMaxTotal(getMaxPreparedStatements());
            config.setTimeBetweenEvictionRunsMillis(-1);
            config.setNumTestsPerEvictionRun(0);
            config.setMinEvictableIdleTimeMillis(0);
        }
        stmtPool = new GenericKeyedObjectPool<>(pci, config);
        pci.setStatementPool(stmtPool);
    }
    return pci;
}
Also used : GenericKeyedObjectPoolConfig(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPoolConfig) PoolablePreparedStatement(org.datanucleus.store.rdbms.datasource.dbcp2.PoolablePreparedStatement)

Example 4 with GenericKeyedObjectPoolConfig

use of org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig 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 5 with GenericKeyedObjectPoolConfig

use of org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig 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(), getValidationQueryTimeoutDuration(), isRollbackAfterValidation());
    factory.setMaxConn(getMaxConnDuration());
    final GenericKeyedObjectPoolConfig<PooledConnectionAndInfo> config = new GenericKeyedObjectPoolConfig<>();
    config.setBlockWhenExhausted(getDefaultBlockWhenExhausted());
    config.setEvictionPolicyClassName(getDefaultEvictionPolicyClassName());
    config.setLifo(getDefaultLifo());
    config.setMaxIdlePerKey(getDefaultMaxIdle());
    config.setMaxTotal(getMaxTotal());
    config.setMaxTotalPerKey(getDefaultMaxTotal());
    config.setMaxWait(getDefaultMaxWait());
    config.setMinEvictableIdleTime(getDefaultMinEvictableIdleDuration());
    config.setMinIdlePerKey(getDefaultMinIdle());
    config.setNumTestsPerEvictionRun(getDefaultNumTestsPerEvictionRun());
    config.setSoftMinEvictableIdleTime(getDefaultSoftMinEvictableIdleDuration());
    config.setTestOnCreate(getDefaultTestOnCreate());
    config.setTestOnBorrow(getDefaultTestOnBorrow());
    config.setTestOnReturn(getDefaultTestOnReturn());
    config.setTestWhileIdle(getDefaultTestWhileIdle());
    config.setTimeBetweenEvictionRuns(getDefaultDurationBetweenEvictionRuns());
    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)

Aggregations

GenericKeyedObjectPoolConfig (org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig)4 Connection (java.sql.Connection)3 ObjectName (javax.management.ObjectName)3 GenericKeyedObjectPoolConfig (org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig)3 GenericKeyedObjectPool (org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool)3 GenericKeyedObjectPoolConfig (org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPoolConfig)3 SQLException (java.sql.SQLException)2 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)2 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)2 GenericKeyedObjectPool (org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPool)2 Script (groovy.lang.Script)1 BaseKeyedPooledObjectFactory (org.apache.commons.pool2.BaseKeyedPooledObjectFactory)1 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)1 GenericKeyedObjectPool (org.apache.commons.pool2.impl.GenericKeyedObjectPool)1 DelegatingPreparedStatement (org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement)1 PStmtKey (org.apache.tomcat.dbcp.dbcp2.PStmtKey)1 PoolableConnection (org.apache.tomcat.dbcp.dbcp2.PoolableConnection)1 PoolablePreparedStatement (org.apache.tomcat.dbcp.dbcp2.PoolablePreparedStatement)1 PoolingConnection (org.apache.tomcat.dbcp.dbcp2.PoolingConnection)1 PoolablePreparedStatement (org.datanucleus.store.rdbms.datasource.dbcp2.PoolablePreparedStatement)1