Search in sources :

Example 6 with GenericKeyedObjectPoolConfig

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

the class PoolableManagedConnectionFactory method makeObject.

/**
 * Uses the configured XAConnectionFactory to create a {@link PoolableManagedConnection}. Throws
 * <code>IllegalStateException</code> if the connection factory returns null. Also initializes the connection using
 * configured initialization SQL (if provided) and sets up a prepared statement pool associated with the
 * PoolableManagedConnection if statement pooling is enabled.
 */
@Override
public synchronized PooledObject<PoolableConnection> makeObject() throws Exception {
    Connection conn = getConnectionFactory().createConnection();
    if (conn == null) {
        throw new IllegalStateException("Connection factory returned null from createConnection");
    }
    initializeConnection(conn);
    if (getPoolStatements()) {
        conn = new PoolingConnection(conn);
        final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
        config.setMaxTotalPerKey(-1);
        config.setBlockWhenExhausted(false);
        config.setMaxWait(Duration.ZERO);
        config.setMaxIdlePerKey(1);
        config.setMaxTotal(getMaxOpenPreparedStatements());
        final ObjectName dataSourceJmxName = getDataSourceJmxName();
        final long connIndex = getConnectionIndex().getAndIncrement();
        if (dataSourceJmxName != null) {
            final StringBuilder base = new StringBuilder(dataSourceJmxName.toString());
            base.append(Constants.JMX_CONNECTION_BASE_EXT);
            base.append(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(getCacheState());
    }
    final PoolableManagedConnection pmc = new PoolableManagedConnection(transactionRegistry, conn, getPool(), getDisconnectionSqlCodes(), isFastFailValidation());
    pmc.setCacheState(getCacheState());
    return new DefaultPooledObject<>(pmc);
}
Also used : GenericKeyedObjectPoolConfig(org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig) DelegatingPreparedStatement(org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement) GenericKeyedObjectPool(org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool) Connection(java.sql.Connection) PoolableConnection(org.apache.tomcat.dbcp.dbcp2.PoolableConnection) PoolingConnection(org.apache.tomcat.dbcp.dbcp2.PoolingConnection) PStmtKey(org.apache.tomcat.dbcp.dbcp2.PStmtKey) ObjectName(javax.management.ObjectName) PoolingConnection(org.apache.tomcat.dbcp.dbcp2.PoolingConnection) DefaultPooledObject(org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)

Example 7 with GenericKeyedObjectPoolConfig

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

the class PoolableConnectionFactory method makeObject.

@Override
public PooledObject<PoolableConnection> makeObject() throws Exception {
    Connection conn = connectionFactory.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
        Utils.closeQuietly((AutoCloseable) conn);
        // Rethrow original exception so it is visible to caller
        throw sqle;
    }
    final long connIndex = connectionIndex.getAndIncrement();
    if (poolStatements) {
        conn = new PoolingConnection(conn);
        final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
        config.setMaxTotalPerKey(-1);
        config.setBlockWhenExhausted(false);
        config.setMaxWait(Duration.ZERO);
        config.setMaxIdlePerKey(1);
        config.setMaxTotal(maxOpenPreparedStatements);
        if (dataSourceJmxObjectName != null) {
            final StringBuilder base = new StringBuilder(dataSourceJmxObjectName.toString());
            base.append(Constants.JMX_CONNECTION_BASE_EXT);
            base.append(connIndex);
            config.setJmxNameBase(base.toString());
            config.setJmxNamePrefix(Constants.JMX_STATEMENT_POOL_PREFIX);
        } else {
            config.setJmxEnabled(false);
        }
        final PoolingConnection poolingConn = (PoolingConnection) conn;
        final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = new GenericKeyedObjectPool<>(poolingConn, config);
        poolingConn.setStatementPool(stmtPool);
        poolingConn.setClearStatementPoolOnReturn(clearStatementPoolOnReturn);
        poolingConn.setCacheState(cacheState);
    }
    // Register this connection with JMX
    final ObjectName connJmxName;
    if (dataSourceJmxObjectName == null) {
        connJmxName = null;
    } else {
        connJmxName = new ObjectName(dataSourceJmxObjectName.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 8 with GenericKeyedObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPoolConfig in project cuba by cuba-platform.

the class AbstractScripting method getPool.

private synchronized GenericKeyedObjectPool<String, Script> getPool() {
    if (pool == null) {
        GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
        poolConfig.setMaxTotalPerKey(-1);
        poolConfig.setMaxIdlePerKey(globalConfig.getGroovyEvaluationPoolMaxIdle());
        pool = new GenericKeyedObjectPool<>(new BaseKeyedPooledObjectFactory<String, Script>() {

            @Override
            public Script create(String key) throws Exception {
                return createScript(key);
            }

            @Override
            public PooledObject<Script> wrap(Script value) {
                return new DefaultPooledObject<>(value);
            }
        }, poolConfig);
    }
    return pool;
}
Also used : GenericKeyedObjectPoolConfig(org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig) Script(groovy.lang.Script) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) BaseKeyedPooledObjectFactory(org.apache.commons.pool2.BaseKeyedPooledObjectFactory)

Example 9 with GenericKeyedObjectPoolConfig

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

the class SharedPoolDataSource method registerPool.

private void registerPool(String username, String password) throws NamingException, SQLException {
    ConnectionPoolDataSource cpds = testCPDS(username, password);
    // Create an object pool to contain our PooledConnections
    factory = new KeyedCPDSConnectionFactory(cpds, getValidationQuery(), getValidationQueryTimeout(), isRollbackAfterValidation());
    factory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis());
    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());
    KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> tmpPool = new GenericKeyedObjectPool<>(factory, config);
    factory.setPool(tmpPool);
    pool = tmpPool;
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) GenericKeyedObjectPoolConfig(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPoolConfig) GenericKeyedObjectPool(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPool)

Example 10 with GenericKeyedObjectPoolConfig

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

the class XpipeNettyClientKeyedObjectPool method createDefaultConfig.

private static GenericKeyedObjectPoolConfig createDefaultConfig(int maxPerKey) {
    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setMaxTotalPerKey(maxPerKey);
    config.setBlockWhenExhausted(false);
    return config;
}
Also used : GenericKeyedObjectPoolConfig(org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig)

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