Search in sources :

Example 21 with GenericObjectPool

use of org.apache.commons.pool2.impl.GenericObjectPool in project hive by apache.

the class DbCPDataSourceProvider method create.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DataSource create(Configuration hdpConfig) throws SQLException {
    LOG.debug("Creating dbcp connection pool for the MetaStore");
    String driverUrl = DataSourceProvider.getMetastoreJdbcDriverUrl(hdpConfig);
    String user = DataSourceProvider.getMetastoreJdbcUser(hdpConfig);
    String passwd = DataSourceProvider.getMetastoreJdbcPasswd(hdpConfig);
    BasicDataSource dbcpDs = new BasicDataSource();
    dbcpDs.setUrl(driverUrl);
    dbcpDs.setUsername(user);
    dbcpDs.setPassword(passwd);
    dbcpDs.setDefaultReadOnly(false);
    dbcpDs.setDefaultAutoCommit(true);
    DatabaseProduct dbProduct = DatabaseProduct.determineDatabaseProduct(driverUrl, hdpConfig);
    Map<String, String> props = dbProduct.getDataSourceProperties();
    for (Map.Entry<String, String> kv : props.entrySet()) {
        dbcpDs.setConnectionProperties(kv.getKey() + "=" + kv.getValue());
    }
    int maxPoolSize = hdpConfig.getInt(MetastoreConf.ConfVars.CONNECTION_POOLING_MAX_CONNECTIONS.getVarname(), ((Long) MetastoreConf.ConfVars.CONNECTION_POOLING_MAX_CONNECTIONS.getDefaultVal()).intValue());
    long connectionTimeout = hdpConfig.getLong(CONNECTION_TIMEOUT_PROPERTY, 30000L);
    int connectionMaxIlde = hdpConfig.getInt(CONNECTION_MAX_IDLE_PROPERTY, 8);
    int connectionMinIlde = hdpConfig.getInt(CONNECTION_MIN_IDLE_PROPERTY, 0);
    boolean testOnBorrow = hdpConfig.getBoolean(CONNECTION_TEST_BORROW_PROPERTY, BaseObjectPoolConfig.DEFAULT_TEST_ON_BORROW);
    long evictionTimeMillis = hdpConfig.getLong(CONNECTION_MIN_EVICT_MILLIS_PROPERTY, BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
    boolean testWhileIdle = hdpConfig.getBoolean(CONNECTION_TEST_IDLEPROPERTY, BaseObjectPoolConfig.DEFAULT_TEST_WHILE_IDLE);
    long timeBetweenEvictionRuns = hdpConfig.getLong(CONNECTION_TIME_BETWEEN_EVICTION_RUNS_MILLIS, BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
    int numTestsPerEvictionRun = hdpConfig.getInt(CONNECTION_NUM_TESTS_PER_EVICTION_RUN, BaseObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN);
    boolean testOnReturn = hdpConfig.getBoolean(CONNECTION_TEST_ON_RETURN, BaseObjectPoolConfig.DEFAULT_TEST_ON_RETURN);
    long softMinEvictableIdleTimeMillis = hdpConfig.getLong(CONNECTION_SOFT_MIN_EVICTABLE_IDLE_TIME, BaseObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
    boolean lifo = hdpConfig.getBoolean(CONNECTION_LIFO, BaseObjectPoolConfig.DEFAULT_LIFO);
    ConnectionFactory connFactory = new DataSourceConnectionFactory(dbcpDs);
    PoolableConnectionFactory poolableConnFactory = new PoolableConnectionFactory(connFactory, null);
    GenericObjectPool objectPool = new GenericObjectPool(poolableConnFactory);
    objectPool.setMaxTotal(maxPoolSize);
    objectPool.setMaxWaitMillis(connectionTimeout);
    objectPool.setMaxIdle(connectionMaxIlde);
    objectPool.setMinIdle(connectionMinIlde);
    objectPool.setTestOnBorrow(testOnBorrow);
    objectPool.setTestWhileIdle(testWhileIdle);
    objectPool.setMinEvictableIdleTimeMillis(evictionTimeMillis);
    objectPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRuns);
    objectPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    objectPool.setTestOnReturn(testOnReturn);
    objectPool.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
    objectPool.setLifo(lifo);
    String stmt = dbProduct.getPrepareTxnStmt();
    if (stmt != null) {
        poolableConnFactory.setValidationQuery(stmt);
    }
    return new PoolingDataSource(objectPool);
}
Also used : PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) DatabaseProduct(org.apache.hadoop.hive.metastore.DatabaseProduct) ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DataSourceConnectionFactory(org.apache.commons.dbcp2.DataSourceConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) DataSourceConnectionFactory(org.apache.commons.dbcp2.DataSourceConnectionFactory) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) Map(java.util.Map) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 22 with GenericObjectPool

use of org.apache.commons.pool2.impl.GenericObjectPool in project Anserini by castorini.

the class SolrEndToEndTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    final File solrHome = createTempDir().toFile();
    final File configSetBaseDir = new File(solrHome.toPath() + File.separator + "configsets");
    FileUtils.copyDirectory(getFile("solr/anserini"), new File(configSetBaseDir + File.separator + "anserini"));
    SolrResourceLoader loader = new SolrResourceLoader(solrHome.toPath());
    NodeConfig config = new NodeConfig.NodeConfigBuilder("embeddedSolrServerNode", loader.getInstancePath()).setConfigSetBaseDirectory(configSetBaseDir.getAbsolutePath()).build();
    client = new EmbeddedSolrServer(config, getCollectionName());
    LOG.info("Created Embedded Solr Server");
    CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
    createRequest.setCoreName(getCollectionName());
    createRequest.setConfigSet("anserini");
    createRequest.process(client);
    client.commit();
    LOG.info("Created Solr Core: " + getCollectionName());
    GenericObjectPoolConfig<SolrClient> poolConfig = new GenericObjectPoolConfig<>();
    // only 1 EmbeddedSolrServer instance will be created by getSolrClient
    poolConfig.setMaxTotal(1);
    poolConfig.setMinIdle(1);
    stubSolrPool = new GenericObjectPool<>(new StubSolrClientFactory(client), poolConfig);
}
Also used : CoreAdminRequest(org.apache.solr.client.solrj.request.CoreAdminRequest) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) SolrClient(org.apache.solr.client.solrj.SolrClient) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) File(java.io.File) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) NodeConfig(org.apache.solr.core.NodeConfig) Before(org.junit.Before)

Example 23 with GenericObjectPool

use of org.apache.commons.pool2.impl.GenericObjectPool in project datanucleus-rdbms by datanucleus.

the class PerUserPoolDataSource method registerPool.

private synchronized void registerPool(final String userName, final String password) throws NamingException, SQLException {
    final ConnectionPoolDataSource cpds = testCPDS(userName, password);
    // Set up the factory we will use (passing the pool associates
    // the factory with the pool, so we do not have to do so
    // explicitly)
    final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, getValidationQuery(), getValidationQueryTimeout(), isRollbackAfterValidation(), userName, password);
    factory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis());
    // Create an object pool to contain our PooledConnections
    final GenericObjectPool<PooledConnectionAndInfo> pool = new GenericObjectPool<>(factory);
    factory.setPool(pool);
    pool.setBlockWhenExhausted(getPerUserBlockWhenExhausted(userName));
    pool.setEvictionPolicyClassName(getPerUserEvictionPolicyClassName(userName));
    pool.setLifo(getPerUserLifo(userName));
    pool.setMaxIdle(getPerUserMaxIdle(userName));
    pool.setMaxTotal(getPerUserMaxTotal(userName));
    pool.setMaxWaitMillis(getPerUserMaxWaitMillis(userName));
    pool.setMinEvictableIdleTimeMillis(getPerUserMinEvictableIdleTimeMillis(userName));
    pool.setMinIdle(getPerUserMinIdle(userName));
    pool.setNumTestsPerEvictionRun(getPerUserNumTestsPerEvictionRun(userName));
    pool.setSoftMinEvictableIdleTimeMillis(getPerUserSoftMinEvictableIdleTimeMillis(userName));
    pool.setTestOnCreate(getPerUserTestOnCreate(userName));
    pool.setTestOnBorrow(getPerUserTestOnBorrow(userName));
    pool.setTestOnReturn(getPerUserTestOnReturn(userName));
    pool.setTestWhileIdle(getPerUserTestWhileIdle(userName));
    pool.setTimeBetweenEvictionRunsMillis(getPerUserTimeBetweenEvictionRunsMillis(userName));
    pool.setSwallowedExceptionListener(new SwallowedExceptionLogger(log));
    final Object old = managers.put(getPoolKey(userName), factory);
    if (old != null) {
        throw new IllegalStateException("Pool already contains an entry for this user/password: " + userName);
    }
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) SwallowedExceptionLogger(org.datanucleus.store.rdbms.datasource.dbcp2.SwallowedExceptionLogger) GenericObjectPool(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPool)

Example 24 with GenericObjectPool

use of org.apache.commons.pool2.impl.GenericObjectPool in project datanucleus-rdbms by datanucleus.

the class DBCP2BuiltinConnectionPoolFactory method createConnectionPool.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.datasource.ConnectionPoolFactory#createConnectionPool(org.datanucleus.store.StoreManager)
     */
public ConnectionPool createConnectionPool(StoreManager storeMgr) {
    // Load the database driver
    String dbDriver = storeMgr.getConnectionDriverName();
    if (!StringUtils.isWhitespace(dbDriver)) {
        loadDriver(dbDriver, storeMgr.getNucleusContext().getClassLoaderResolver(null));
    }
    String dbURL = storeMgr.getConnectionURL();
    PoolingDataSource ds = null;
    GenericObjectPool<PoolableConnection> connectionPool;
    try {
        // Create a factory to be used by the pool to create the connections
        Properties dbProps = getPropertiesForDriver(storeMgr);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dbURL, dbProps);
        // Wrap the connections and statements with pooled variants
        PoolableConnectionFactory poolableCF = null;
        poolableCF = new PoolableConnectionFactory(connectionFactory, null);
        String testSQL = null;
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TEST_SQL)) {
            testSQL = storeMgr.getStringProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TEST_SQL);
            poolableCF.setValidationQuery(testSQL);
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_VALIDATION_TIMEOUT)) {
            int validationTimeout = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_VALIDATION_TIMEOUT);
            if (validationTimeout >= 0) {
                poolableCF.setValidationQueryTimeout(validationTimeout);
            }
        }
        // Create the actual pool of connections, and apply any properties
        connectionPool = new GenericObjectPool(poolableCF);
        poolableCF.setPool(connectionPool);
        if (testSQL != null) {
            connectionPool.setTestOnBorrow(true);
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_IDLE)) {
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_IDLE);
            if (value > 0) {
                connectionPool.setMaxIdle(value);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_IDLE)) {
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_IDLE);
            if (value > 0) {
                connectionPool.setMinIdle(value);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_ACTIVE)) {
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_ACTIVE);
            if (value > 0) {
                connectionPool.setMaxTotal(value);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_WAIT)) {
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_WAIT);
            if (value > 0) {
                connectionPool.setMaxWaitMillis(value);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TIME_BETWEEN_EVICTOR_RUNS_MILLIS)) {
            // how often should the evictor run (if ever, default is -1 = off)
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TIME_BETWEEN_EVICTOR_RUNS_MILLIS);
            if (value > 0) {
                connectionPool.setTimeBetweenEvictionRunsMillis(value);
                // in each eviction run, evict at least a quarter of "maxIdle" connections
                int maxIdle = connectionPool.getMaxIdle();
                int numTestsPerEvictionRun = (int) Math.ceil((double) maxIdle / 4);
                connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS)) {
            // how long may a connection sit idle in the pool before it may be evicted
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS);
            if (value > 0) {
                connectionPool.setMinEvictableIdleTimeMillis(value);
            }
        }
        // Create the datasource
        ds = new org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource(connectionPool);
    } catch (Exception e) {
        throw new DatastorePoolException("DBCP2", dbDriver, dbURL, e);
    }
    return new DBCPConnectionPool(ds, connectionPool);
}
Also used : PoolingDataSource(org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource) DriverManagerConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory) Properties(java.util.Properties) GenericObjectPool(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPool) DriverManagerConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory) PoolableConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnectionFactory) ConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.ConnectionFactory) PoolableConnection(org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnection) PoolingDataSource(org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource) PoolableConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnectionFactory)

Example 25 with GenericObjectPool

use of org.apache.commons.pool2.impl.GenericObjectPool in project logging-log4j2 by apache.

the class PoolingDriverConnectionSource method setupDriver.

private void setupDriver(final String connectionString, final PoolableConnectionFactoryConfig poolableConnectionFactoryConfig) throws SQLException {
    // 
    // First, we'll create a ConnectionFactory that the
    // pool will use to create Connections.
    // We'll use the DriverManagerConnectionFactory,
    // using the connect string passed in the command line
    // arguments.
    // 
    final Property[] properties = getProperties();
    final char[] userName = getUserName();
    final char[] password = getPassword();
    final ConnectionFactory connectionFactory;
    if (properties != null && properties.length > 0) {
        if (userName != null || password != null) {
            throw new SQLException("Either set the userName and password, or set the Properties, but not both.");
        }
        connectionFactory = new DriverManagerConnectionFactory(connectionString, toProperties(properties));
    } else {
        connectionFactory = new DriverManagerConnectionFactory(connectionString, toString(userName), toString(password));
    }
    // 
    // Next, we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    // 
    final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    if (poolableConnectionFactoryConfig != null) {
        poolableConnectionFactoryConfig.init(poolableConnectionFactory);
    }
    // 
    // Now we'll need a ObjectPool that serves as the
    // actual pool of connections.
    // 
    // We'll use a GenericObjectPool instance, although
    // any ObjectPool implementation will suffice.
    // 
    @SuppressWarnings("resource") final ObjectPool<PoolableConnection> // This GenericObjectPool will be closed on shutdown
    connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);
    loadDriver(poolingDriverClassName);
    final PoolingDriver driver = getPoolingDriver();
    if (driver != null) {
        getLogger().debug("Registering DBCP pool '{}' with pooling driver {}: {}", poolName, driver, connectionPool);
        driver.registerPool(poolName, connectionPool);
    }
// 
// Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
// to access our pool of Connections.
// 
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) SQLException(java.sql.SQLException) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) PoolingDriver(org.apache.commons.dbcp2.PoolingDriver) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Property(org.apache.logging.log4j.core.config.Property) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Aggregations

GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)24 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)12 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)10 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)8 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)6 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)6 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)4 File (java.io.File)3 SQLException (java.sql.SQLException)3 Properties (java.util.Properties)3 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 Before (org.junit.Before)2 Bean (org.springframework.context.annotation.Bean)2 NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)1 NettyClientFactory (com.ctrip.xpipe.netty.commands.NettyClientFactory)1