Search in sources :

Example 16 with GenericObjectPool

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

the class JedisHealthIndicatorFactory method build.

public static HealthIndicator build(Pool<Jedis> jedisPool) {
    try {
        final Pool<Jedis> src = jedisPool;
        final Field poolAccess = Pool.class.getDeclaredField("internalPool");
        poolAccess.setAccessible(true);
        GenericObjectPool<Jedis> internal = (GenericObjectPool<Jedis>) poolAccess.get(jedisPool);
        return () -> {
            Jedis jedis = null;
            Health.Builder health;
            try {
                jedis = src.getResource();
                if ("PONG".equals(jedis.ping())) {
                    health = Health.up();
                } else {
                    health = Health.down();
                }
            } catch (Exception ex) {
                health = Health.down(ex);
            } finally {
                if (jedis != null)
                    jedis.close();
            }
            health.withDetail("maxIdle", internal.getMaxIdle());
            health.withDetail("minIdle", internal.getMinIdle());
            health.withDetail("numActive", internal.getNumActive());
            health.withDetail("numIdle", internal.getNumIdle());
            health.withDetail("numWaiters", internal.getNumWaiters());
            return health.build();
        };
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new BeanCreationException("Error creating Redis health indicator", e);
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Field(java.lang.reflect.Field) BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) BeanCreationException(org.springframework.beans.factory.BeanCreationException)

Example 17 with GenericObjectPool

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

the class PoolableBroadcasterFactoryTest method testImplementation.

@Test
public void testImplementation() {
    assertNotNull(factory.poolableProvider());
    assertNotNull(factory.poolableProvider().implementation());
    assertEquals(factory.poolableProvider().implementation().getClass(), GenericObjectPool.class);
    GenericObjectPool nativePool = (GenericObjectPool) factory.poolableProvider().implementation();
    assertTrue(nativePool.getLifo());
    GenericObjectPoolConfig c = new GenericObjectPoolConfig();
    c.setMaxTotal(1);
    nativePool.setConfig(c);
    assertEquals(1, nativePool.getMaxTotal());
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Test(org.testng.annotations.Test)

Example 18 with GenericObjectPool

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

the class XpipeNettyClientPool method doInitialize.

@Override
protected void doInitialize() throws Exception {
    this.factory = new NettyClientFactory(target);
    this.factory.start();
    GenericObjectPool<NettyClient> genericObjectPool = new GenericObjectPool<>(factory, config);
    genericObjectPool.setTestOnBorrow(true);
    genericObjectPool.setTestOnCreate(true);
    this.objectPool = genericObjectPool;
}
Also used : NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) NettyClientFactory(com.ctrip.xpipe.netty.commands.NettyClientFactory) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Example 19 with GenericObjectPool

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

the class ThriftClientFactory method buildPooled.

/**
 * build pooled(auto close connections) and thread-safe (unblocking) client.
 * <br/>
 * WARNING: the holder needs to be closed after using.
 */
@SuppressWarnings("unchecked")
public ClientHolder<TI> buildPooled() {
    GenericObjectPoolConfig poolConf = new GenericObjectPoolConfig();
    poolConf.setMinIdle(0);
    poolConf.setMaxTotal(Integer.MAX_VALUE);
    poolConf.setMaxWaitMillis(conf.clientSocketTimeout);
    poolConf.setTimeBetweenEvictionRunsMillis(POOL_IDLE_OBJ_TIMEOUT);
    poolConf.setTestWhileIdle(true);
    GenericObjectPool<ThriftClient> pool = new GenericObjectPool(new PoolObjMaker(), poolConf);
    TI client = (TI) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[] { conf.iface }, (obj, method, args) -> {
        ThriftClient tc = pool.borrowObject();
        try {
            return tc.invokeThriftClient(method, args);
        } finally {
            pool.returnObject(tc);
        }
    });
    return new ClientHolder<>(client, pool::close);
}
Also used : BasePooledObjectFactory(org.apache.commons.pool2.BasePooledObjectFactory) Logger(org.slf4j.Logger) Proxy(java.lang.reflect.Proxy) PooledObject(org.apache.commons.pool2.PooledObject) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) org.apache.thrift.transport(org.apache.thrift.transport) LoggerFactory(org.slf4j.LoggerFactory) TException(org.apache.thrift.TException) IOException(java.io.IOException) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) SocketException(java.net.SocketException) TProtocol(org.apache.thrift.protocol.TProtocol) Closeable(java.io.Closeable) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) Method(java.lang.reflect.Method) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Example 20 with GenericObjectPool

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

the class DBCPConnectionFactory method getConnection.

public Connection getConnection(GenericHelperInfo helperInfo, JdbcElement abstractJdbc) throws SQLException, GenericEntityException {
    String cacheKey = helperInfo.getHelperFullName();
    DebugManagedDataSource mds = dsCache.get(cacheKey);
    if (mds != null) {
        return TransactionUtil.getCursorConnection(helperInfo, mds.getConnection());
    }
    if (!(abstractJdbc instanceof InlineJdbc)) {
        throw new GenericEntityConfException("DBCP requires an <inline-jdbc> child element in the <datasource> element");
    }
    InlineJdbc jdbcElement = (InlineJdbc) abstractJdbc;
    // connection properties
    TransactionManager txMgr = TransactionFactoryLoader.getInstance().getTransactionManager();
    String driverName = jdbcElement.getJdbcDriver();
    String jdbcUri = helperInfo.getOverrideJdbcUri(jdbcElement.getJdbcUri());
    String jdbcUsername = helperInfo.getOverrideUsername(jdbcElement.getJdbcUsername());
    String jdbcPassword = helperInfo.getOverridePassword(EntityConfig.getJdbcPassword(jdbcElement));
    // pool settings
    int maxSize = jdbcElement.getPoolMaxsize();
    int minSize = jdbcElement.getPoolMinsize();
    int maxIdle = jdbcElement.getIdleMaxsize();
    // maxIdle must be greater than pool-minsize
    maxIdle = maxIdle > minSize ? maxIdle : minSize;
    // load the driver
    Driver jdbcDriver;
    synchronized (DBCPConnectionFactory.class) {
        // Sync needed for MS SQL JDBC driver. See OFBIZ-5216.
        try {
            jdbcDriver = (Driver) Class.forName(driverName, true, Thread.currentThread().getContextClassLoader()).newInstance();
        } catch (Exception e) {
            Debug.logError(e, module);
            throw new GenericEntityException(e.getMessage(), e);
        }
    }
    // connection factory properties
    Properties cfProps = new Properties();
    cfProps.put("user", jdbcUsername);
    cfProps.put("password", jdbcPassword);
    // create the connection factory
    org.apache.commons.dbcp2.ConnectionFactory cf = new DriverConnectionFactory(jdbcDriver, jdbcUri, cfProps);
    // wrap it with a LocalXAConnectionFactory
    XAConnectionFactory xacf = new LocalXAConnectionFactory(txMgr, cf);
    // create the pool object factory
    PoolableConnectionFactory factory = new PoolableManagedConnectionFactory(xacf, null);
    factory.setValidationQuery(jdbcElement.getPoolJdbcTestStmt());
    factory.setDefaultReadOnly(false);
    factory.setRollbackOnReturn(false);
    factory.setEnableAutoCommitOnReturn(false);
    String transIso = jdbcElement.getIsolationLevel();
    if (!transIso.isEmpty()) {
        if ("Serializable".equals(transIso)) {
            factory.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        } else if ("RepeatableRead".equals(transIso)) {
            factory.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        } else if ("ReadUncommitted".equals(transIso)) {
            factory.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
        } else if ("ReadCommitted".equals(transIso)) {
            factory.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        } else if ("None".equals(transIso)) {
            factory.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
        }
    }
    // configure the pool settings
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxTotal(maxSize);
    // settings for idle connections
    poolConfig.setMaxIdle(maxIdle);
    poolConfig.setMinIdle(minSize);
    poolConfig.setTimeBetweenEvictionRunsMillis(jdbcElement.getTimeBetweenEvictionRunsMillis());
    // disabled in favour of setSoftMinEvictableIdleTimeMillis(...)
    poolConfig.setMinEvictableIdleTimeMillis(-1);
    poolConfig.setSoftMinEvictableIdleTimeMillis(jdbcElement.getSoftMinEvictableIdleTimeMillis());
    // test all the idle connections
    poolConfig.setNumTestsPerEvictionRun(maxSize);
    // settings for when the pool is exhausted
    // the thread requesting the connection waits if no connection is available
    poolConfig.setBlockWhenExhausted(true);
    // throw an exception if, after getPoolSleeptime() ms, no connection is available for the requesting thread
    poolConfig.setMaxWaitMillis(jdbcElement.getPoolSleeptime());
    // settings for the execution of the validation query
    poolConfig.setTestOnCreate(jdbcElement.getTestOnCreate());
    poolConfig.setTestOnBorrow(jdbcElement.getTestOnBorrow());
    poolConfig.setTestOnReturn(jdbcElement.getTestOnReturn());
    poolConfig.setTestWhileIdle(jdbcElement.getTestWhileIdle());
    GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(factory, poolConfig);
    factory.setPool(pool);
    mds = new DebugManagedDataSource(pool, xacf.getTransactionRegistry());
    mds.setAccessToUnderlyingConnectionAllowed(true);
    // cache the pool
    dsCache.putIfAbsent(cacheKey, mds);
    mds = dsCache.get(cacheKey);
    return TransactionUtil.getCursorConnection(helperInfo, mds.getConnection());
}
Also used : GenericEntityConfException(org.apache.ofbiz.entity.GenericEntityConfException) Driver(java.sql.Driver) Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableManagedConnectionFactory(org.apache.commons.dbcp2.managed.PoolableManagedConnectionFactory) GenericEntityConfException(org.apache.ofbiz.entity.GenericEntityConfException) SQLException(java.sql.SQLException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) TransactionManager(javax.transaction.TransactionManager) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) InlineJdbc(org.apache.ofbiz.entity.config.model.InlineJdbc) DriverConnectionFactory(org.apache.commons.dbcp2.DriverConnectionFactory) XAConnectionFactory(org.apache.commons.dbcp2.managed.XAConnectionFactory) LocalXAConnectionFactory(org.apache.commons.dbcp2.managed.LocalXAConnectionFactory) LocalXAConnectionFactory(org.apache.commons.dbcp2.managed.LocalXAConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

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