Search in sources :

Example 1 with GenericObjectPool

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

the class BasicDataSource method createConnectionPool.

/**
 * Creates a connection pool for this datasource.  This method only exists
 * so subclasses can replace the implementation class.
 *
 * This implementation configures all pool properties other than
 * timeBetweenEvictionRunsMillis.  Setting that property is deferred to
 * {@link #startPoolMaintenance()}, since setting timeBetweenEvictionRunsMillis
 * to a positive value causes {@link GenericObjectPool}'s eviction timer
 * to be started.
 */
protected void createConnectionPool(PoolableConnectionFactory factory) {
    // Create an object pool to contain our active connections
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    updateJmxName(config);
    // Disable JMX on the underlying pool if the DS is not registered.
    config.setJmxEnabled(registeredJmxName != null);
    GenericObjectPool<PoolableConnection> gop;
    if (abandonedConfig != null && (abandonedConfig.getRemoveAbandonedOnBorrow() || abandonedConfig.getRemoveAbandonedOnMaintenance())) {
        gop = new GenericObjectPool<>(factory, config, abandonedConfig);
    } else {
        gop = new GenericObjectPool<>(factory, config);
    }
    gop.setMaxTotal(maxTotal);
    gop.setMaxIdle(maxIdle);
    gop.setMinIdle(minIdle);
    gop.setMaxWaitMillis(maxWaitMillis);
    gop.setTestOnCreate(testOnCreate);
    gop.setTestOnBorrow(testOnBorrow);
    gop.setTestOnReturn(testOnReturn);
    gop.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    gop.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    gop.setTestWhileIdle(testWhileIdle);
    gop.setLifo(lifo);
    gop.setSwallowedExceptionListener(new SwallowedExceptionLogger(log, logExpiredConnections));
    gop.setEvictionPolicyClassName(evictionPolicyClassName);
    factory.setPool(gop);
    connectionPool = gop;
}
Also used : GenericObjectPoolConfig(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig)

Example 2 with GenericObjectPool

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPool in project cas by apereo.

the class MemcachedMonitorConfiguration method memcachedHealthIndicator.

@Bean
public HealthIndicator memcachedHealthIndicator() {
    final MonitorProperties.Memcached memcached = casProperties.getMonitor().getMemcached();
    final MemcachedPooledClientConnectionFactory factory = new MemcachedPooledClientConnectionFactory(memcached, memcachedMonitorTranscoder());
    final ObjectPool<MemcachedClientIF> pool = new GenericObjectPool<>(factory);
    return new MemcachedHealthIndicator(pool, casProperties);
}
Also used : MemcachedPooledClientConnectionFactory(org.apereo.cas.memcached.MemcachedPooledClientConnectionFactory) MemcachedClientIF(net.spy.memcached.MemcachedClientIF) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) MemcachedHealthIndicator(org.apereo.cas.monitor.MemcachedHealthIndicator) MonitorProperties(org.apereo.cas.configuration.model.core.monitor.MonitorProperties) Bean(org.springframework.context.annotation.Bean)

Example 3 with GenericObjectPool

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

the class BasicDataSource method setRemoveAbandonedOnMaintenance.

/**
 * @param removeAbandonedOnMaintenance true means abandoned connections may be removed on pool maintenance.
 * @see #getRemoveAbandonedOnMaintenance()
 */
public void setRemoveAbandonedOnMaintenance(final boolean removeAbandonedOnMaintenance) {
    if (abandonedConfig == null) {
        abandonedConfig = new AbandonedConfig();
    }
    abandonedConfig.setRemoveAbandonedOnMaintenance(removeAbandonedOnMaintenance);
    final GenericObjectPool<?> gop = this.connectionPool;
    if (gop != null) {
        gop.setAbandonedConfig(abandonedConfig);
    }
}
Also used : AbandonedConfig(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.AbandonedConfig)

Example 4 with GenericObjectPool

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

the class BasicDataSource method setLogAbandoned.

/**
 * @param logAbandoned new logAbandoned property value
 */
public void setLogAbandoned(final boolean logAbandoned) {
    if (abandonedConfig == null) {
        abandonedConfig = new AbandonedConfig();
    }
    abandonedConfig.setLogAbandoned(logAbandoned);
    final GenericObjectPool<?> gop = this.connectionPool;
    if (gop != null) {
        gop.setAbandonedConfig(abandonedConfig);
    }
}
Also used : AbandonedConfig(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.AbandonedConfig)

Example 5 with GenericObjectPool

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPool in project atlasdb by palantir.

the class CassandraClientPoolingContainer method createClientPool.

/**
 * Pool size:
 *    Always keep {@link CassandraKeyValueServiceConfig#poolSize()} connections around, per host. Allow bursting
 *    up to {@link CassandraKeyValueServiceConfig#maxConnectionBurstSize()} connections per host under load.
 *
 * Borrowing from pool:
 *    On borrow, check if the connection is actually open. If it is not,
 *       immediately discard this connection from the pool, and try to take another.
 *    Borrow attempts against a fully in-use pool immediately throw a NoSuchElementException.
 *       {@code CassandraClientPool} when it sees this will:
 *          Follow an exponential backoff as a method of back pressure.
 *          Try 3 times against this host, and then give up and try against different hosts 3 additional times.
 *
 * In an asynchronous thread (using default values):
 *    Every 20-30 seconds, examine approximately a tenth of the connections in pool.
 *    Discard any connections in this tenth of the pool whose TCP connections are closed.
 *    Discard any connections in this tenth of the pool that have been idle for more than 10 minutes,
 *       while still keeping a minimum number of idle connections around for fast borrows.
 *
 * @param poolNumber number of the pool for metric registration.
 */
private GenericObjectPool<CassandraClient> createClientPool(int poolNumber) {
    CassandraClientFactory cassandraClientFactory = new CassandraClientFactory(qosClient, host, config);
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMinIdle(config.poolSize());
    poolConfig.setMaxIdle(config.maxConnectionBurstSize());
    poolConfig.setMaxTotal(config.maxConnectionBurstSize());
    // immediately throw when we try and borrow from a full pool; dealt with at higher level
    poolConfig.setBlockWhenExhausted(false);
    poolConfig.setMaxWaitMillis(config.socketTimeoutMillis());
    // this test is free/just checks a boolean and does not block; borrow is still fast
    poolConfig.setTestOnBorrow(true);
    poolConfig.setMinEvictableIdleTimeMillis(TimeUnit.MILLISECONDS.convert(config.idleConnectionTimeoutSeconds(), TimeUnit.SECONDS));
    // the randomness here is to prevent all of the pools for all of the hosts
    // evicting all at at once, which isn't great for C*.
    int timeBetweenEvictionsSeconds = config.timeBetweenConnectionEvictionRunsSeconds();
    int delta = ThreadLocalRandom.current().nextInt(Math.min(timeBetweenEvictionsSeconds / 2, 10));
    poolConfig.setTimeBetweenEvictionRunsMillis(TimeUnit.MILLISECONDS.convert(timeBetweenEvictionsSeconds + delta, TimeUnit.SECONDS));
    poolConfig.setNumTestsPerEvictionRun(-(int) (1.0 / config.proportionConnectionsToCheckPerEvictionRun()));
    poolConfig.setTestWhileIdle(true);
    poolConfig.setJmxNamePrefix(CassandraLogHelper.host(host));
    GenericObjectPool<CassandraClient> pool = new GenericObjectPool<>(cassandraClientFactory, poolConfig);
    registerMetrics(pool, poolNumber);
    return pool;
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Aggregations

GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)24 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)9 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)9 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)7 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)6 AbandonedConfig (org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.AbandonedConfig)6 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)5 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)4 SQLException (java.sql.SQLException)3 Properties (java.util.Properties)3 File (java.io.File)2 IOException (java.io.IOException)2 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)2 lombok.val (lombok.val)2 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)2 MemcachedPooledClientConnectionFactory (org.apereo.cas.memcached.MemcachedPooledClientConnectionFactory)2 GenericObjectPool (org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPool)2 Bean (org.springframework.context.annotation.Bean)2 NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)1 NettyClientFactory (com.ctrip.xpipe.netty.commands.NettyClientFactory)1