Example usage for org.apache.commons.pool2.impl GenericObjectPool setMaxIdle

List of usage examples for org.apache.commons.pool2.impl GenericObjectPool setMaxIdle

Introduction

In this page you can find the example usage for org.apache.commons.pool2.impl GenericObjectPool setMaxIdle.

Prototype

public void setMaxIdle(int maxIdle) 

Source Link

Document

Returns the cap on the number of "idle" instances in the pool.

Usage

From source file:com.mirth.connect.donkey.server.data.jdbc.DBCPConnectionPool.java

public DBCPConnectionPool(String url, String username, String password, int maxConnections) {
    this.maxConnections = maxConnections;

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, username, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);//from   w  w  w  . ja va 2  s.c  o m
    poolableConnectionFactory.setDefaultAutoCommit(false);

    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    connectionPool.setMaxTotal(maxConnections);
    connectionPool.setMaxIdle(maxConnections);

    poolableConnectionFactory.setPool(connectionPool);

    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<PoolableConnection>(
            connectionPool);
    dataSource.setAccessToUnderlyingConnectionAllowed(true);

    this.dataSource = dataSource;
}

From source file:io.seldon.dbcp.DbcpFactory.java

private void createDbcp(DbcpConfig conf) {
    if (!dataSources.containsKey(conf.name)) {
        try {/*from  w w w . j  av a  2  s.c  om*/

            Class.forName(conf.driverClassName);

            DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory(conf.jdbc, conf.user,
                    conf.password);

            PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, null);
            pcf.setValidationQuery(conf.validationQuery);
            //, pool, null, conf.validationQuery, false, true,abandondedConfig);

            logger.info("Creating pool " + conf.toString());
            // create a generic pool
            GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pcf);
            pool.setMaxTotal(conf.maxTotal);
            pool.setMaxIdle(conf.maxIdle);
            pool.setMinIdle(conf.minIdle);
            pool.setMaxWaitMillis(conf.maxWait);
            pool.setTimeBetweenEvictionRunsMillis(conf.timeBetweenEvictionRunsMillis);
            pool.setMinEvictableIdleTimeMillis(conf.minEvictableIdleTimeMillis);
            pool.setTestWhileIdle(conf.testWhileIdle);
            pool.setTestOnBorrow(conf.testOnBorrow);

            AbandonedConfig abandonedConfig = new AbandonedConfig();
            abandonedConfig.setRemoveAbandonedOnMaintenance(conf.removeAbanadoned);
            abandonedConfig.setRemoveAbandonedTimeout(conf.removeAbandonedTimeout);
            abandonedConfig.setLogAbandoned(conf.logAbandonded);

            pool.setAbandonedConfig(abandonedConfig);

            pcf.setPool(pool);
            DataSource ds = new PoolingDataSource(pool);
            dataSources.put(conf.name, ds);

        } catch (ClassNotFoundException e) {
            logger.error(
                    "Failed to create datasource for " + conf.name + " with class " + conf.driverClassName);
        }

    } else {
        logger.error("Pool " + conf.name + " already exists. Can't change existing datasource at present.");
    }
}

From source file:eu.peppol.persistence.jdbc.OxalisDataSourceFactoryDbcpImpl.java

/**
 * Creates a DataSource with connection pooling as provided by Apache DBCP
 *
 * @return a DataSource/*  w w w  . j a v  a 2 s .c o  m*/
 */
DataSource configureAndCreateDataSource(RepositoryConfiguration configuration) {

    log.debug("Configuring DataSource wrapped in a Database Connection Pool, using custom loader");

    String jdbcDriverClassPath = configuration.getJdbcDriverClassPath();

    log.debug("Loading JDBC Driver with custom class path: " + jdbcDriverClassPath);
    // Creates a new class loader, which will be used for loading our JDBC driver
    URLClassLoader urlClassLoader = getOxalisClassLoaderForJdbc(jdbcDriverClassPath);

    String className = configuration.getJdbcDriverClassName();
    String connectURI = configuration.getJdbcConnectionUri().toString();
    String userName = configuration.getJdbcUsername();
    String password = configuration.getJdbcPassword();

    log.debug("className=" + className);
    log.debug("connectURI=" + connectURI);
    log.debug("userName=" + userName);
    log.debug("password=" + password);

    // Loads the JDBC Driver in a separate class loader
    Driver driver = getJdbcDriver(jdbcDriverClassPath, urlClassLoader, className);

    Properties properties = new Properties();
    properties.put("user", userName);
    properties.put("password", password);

    // DBCP factory which will produce JDBC Driver instances
    ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, connectURI, properties);

    // DBCP Factory holding the pooled connection, which are created by the driver connection factory and held in the supplied pool
    ObjectName dataSourceJmxName = null;
    try {
        dataSourceJmxName = new ObjectName("no.difi.oxalis", "connectionPool", "OxalisDB");
    } catch (MalformedObjectNameException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory,
            dataSourceJmxName);

    String validationQuery = configuration.getValidationQuery();
    if (validationQuery != null) {
        poolableConnectionFactory.setValidationQuery(validationQuery);
    }
    // DBCP object pool holding our driver connections
    GenericObjectPool<PoolableConnection> genericObjectPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(genericObjectPool);
    genericObjectPool.setMaxTotal(100);
    genericObjectPool.setMaxIdle(30);
    genericObjectPool.setMaxWaitMillis(10000);

    genericObjectPool.setTestOnBorrow(true); // Test the connection returned from the pool

    genericObjectPool.setTestWhileIdle(true); // Test idle instances visited by the pool maintenance thread and destroy any that fail validation
    genericObjectPool.setTimeBetweenEvictionRunsMillis(60 * 60 * 1000); // Test every hour

    // Creates the actual DataSource instance
    PoolingDataSource poolingDataSource = new PoolingDataSource(genericObjectPool);

    return poolingDataSource;

}

From source file:net.identio.server.service.authentication.ldap.LdapAuthenticationProvider.java

private void initPool(List<LdapAuthMethod> ldapAuthMethods) {

    for (LdapAuthMethod ldapAuthMethod : ldapAuthMethods) {

        LOG.debug("* Auth Method: {}", ldapAuthMethod.getName());

        LdapConnectionFactory factory = new LdapConnectionFactory(ldapAuthMethod);

        GenericObjectPool<InitialLdapContext> pool = new GenericObjectPool<>(factory);

        LdapAuthenticationProviderConfiguration.LdapPoolConfig poolConfig = ldapAuthMethod.getPoolConfig();

        pool.setMinIdle(poolConfig.getMinIdleConnections());
        pool.setMaxIdle(poolConfig.getMaxIdleConnections());
        pool.setBlockWhenExhausted(true);
        pool.setTestWhileIdle(poolConfig.isTestWhileIdle());
        pool.setTestOnBorrow(poolConfig.isTestOnBorrow());
        pool.setTimeBetweenEvictionRunsMillis(1000 * poolConfig.getTimeBetweenEvictionRuns());
        pool.setNumTestsPerEvictionRun(poolConfig.getNumTestsPerEvictionRun());
        pool.setMinEvictableIdleTimeMillis(1000 * poolConfig.getMinEvictableIdleTime());

        pools.put(ldapAuthMethod.getName(), pool);

    }//from w  w w  .ja  va2 s.  c  o m
}

From source file:net.sf.jasperreports.phantomjs.ProcessDirector.java

private GenericObjectPool<PhantomJSProcess> createProcessPool(JRPropertiesUtil properties) {
    ProcessFactory processFactory = new ProcessFactory(this, properties);
    GenericObjectPool<PhantomJSProcess> pool = new GenericObjectPool<>(processFactory);
    pool.setLifo(true);//from w  ww  . j  av  a 2s. c om

    int maxProcessCount = properties.getIntegerProperty(PhantomJS.PROPERTY_PHANTOMJS_MAX_PROCESS_COUNT,
            PhantomJS.DEFAULT_PHANTOMJS_MAX_PROCESS_COUNT);
    pool.setMaxTotal(maxProcessCount);
    pool.setMaxIdle(maxProcessCount);

    int borrowTimeout = properties.getIntegerProperty(PhantomJS.PROPERTY_PHANTOMJS_POOL_BORROW_TIMEOUT,
            PhantomJS.DEFAULT_PHANTOMJS_POOL_BORROW_TIMEOUT);
    pool.setMaxWaitMillis(borrowTimeout);

    int idleTimeout = properties.getIntegerProperty(PhantomJS.PROPERTY_PHANTOMJS_IDLE_TIMEOUT,
            PhantomJS.DEFAULT_PHANTOMJS_IDLE_TIMEOUT);
    pool.setMinEvictableIdleTimeMillis(idleTimeout);

    pool.setTimeBetweenEvictionRunsMillis(idlePingInterval);

    pool.setTestWhileIdle(true);
    pool.setNumTestsPerEvictionRun(Integer.MAX_VALUE);

    pool.setSwallowedExceptionListener(new SwallowedExceptionListener() {
        @Override
        public void onSwallowException(Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Pool exception", e);
            }
        }
    });

    return pool;
}

From source file:com.adaptris.core.services.splitter.ServiceWorkerPool.java

public GenericObjectPool<Worker> createCommonsObjectPool() throws CoreException {
    GenericObjectPool<Worker> pool = new GenericObjectPool<>(new WorkerFactory());
    // Make the pool the same size as the thread pool
    pool.setMaxTotal(maxThreads);/*w ww.  j a  va2s  . co  m*/
    pool.setMinIdle(maxThreads);
    pool.setMaxIdle(maxThreads);
    pool.setMaxWaitMillis(-1L);
    pool.setBlockWhenExhausted(true);
    pool.setSoftMinEvictableIdleTimeMillis(EVICT_RUN);
    pool.setTimeBetweenEvictionRunsMillis(EVICT_RUN + ThreadLocalRandom.current().nextLong(EVICT_RUN));
    return pool;
}

From source file:com.adaptris.core.services.splitter.ServiceWorkerPool.java

/**
 * //from w  w  w  .ja  va  2  s  .  c o  m
 * @deprecated since 3.8.3 switch to commons-pool2 and {@link createCommonsObjectPool()} instead.
 */
@Deprecated
@Removal(version = "3.9.0", message = "use commons-pool2 + createCommonsObjectPool()")
public org.apache.commons.pool.impl.GenericObjectPool<Worker> createObjectPool() throws CoreException {
    logDeprecationWarning();
    org.apache.commons.pool.impl.GenericObjectPool<Worker> pool = new org.apache.commons.pool.impl.GenericObjectPool(
            new LegacyCommonsPoolWorkerFactory());
    pool.setMaxActive(maxThreads);
    pool.setMinIdle(maxThreads);
    pool.setMaxIdle(maxThreads);
    pool.setMaxWait(-1L);
    pool.setWhenExhaustedAction(org.apache.commons.pool.impl.GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    pool.setMinEvictableIdleTimeMillis(EVICT_RUN);
    pool.setTimeBetweenEvictionRunsMillis(EVICT_RUN + ThreadLocalRandom.current().nextLong(EVICT_RUN));
    return pool;
}

From source file:com.adaptris.core.PoolingWorkflow.java

private GenericObjectPool<Worker> createObjectPool() {
    GenericObjectPool<Worker> pool = new GenericObjectPool<>(new WorkerFactory());
    long lifetime = threadLifetimeMs();
    pool.setMaxTotal(poolSize());/*w  w  w .ja v a 2s.  c  o m*/
    pool.setMinIdle(minIdle());
    pool.setMaxIdle(maxIdle());
    pool.setMaxWaitMillis(-1L);
    pool.setBlockWhenExhausted(true);
    pool.setSoftMinEvictableIdleTimeMillis(lifetime);
    pool.setTimeBetweenEvictionRunsMillis(lifetime + ThreadLocalRandom.current().nextLong(lifetime));
    return pool;
}

From source file:com.zaxxer.hikari.benchmark.BenchBase.java

private void setupDBCP2() {
    org.apache.commons.pool2.impl.GenericObjectPool<org.apache.commons.dbcp2.PoolableConnection> connectionPool;
    DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcURL, "sa", "");

    // Wrap the connections and statements with pooled variants
    org.apache.commons.dbcp2.PoolableConnectionFactory poolableCF = null;
    poolableCF = new org.apache.commons.dbcp2.PoolableConnectionFactory(connectionFactory, null);

    poolableCF.setValidationQuery("VALUES 1");
    poolableCF.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    poolableCF.setDefaultAutoCommit(false);
    poolableCF.setRollbackOnReturn(true);

    // Create the actual pool of connections, and apply any properties
    connectionPool = new org.apache.commons.pool2.impl.GenericObjectPool(poolableCF);
    connectionPool.setTestOnBorrow(true);
    connectionPool.setMaxIdle(maxPoolSize);

    connectionPool.setMinIdle(MIN_POOL_SIZE);
    connectionPool.setMaxTotal(maxPoolSize);
    connectionPool.setMaxWaitMillis(8000);
    connectionPool.setMinEvictableIdleTimeMillis((int) TimeUnit.MINUTES.toMillis(30));
    poolableCF.setPool(connectionPool);//  w  w w . ja  v  a2  s.  co m
    DS = new org.apache.commons.dbcp2.PoolingDataSource(connectionPool);
}

From source file:no.sr.ringo.persistence.jdbc.RingoDataSourceFactoryDbcpImpl.java

private PoolingDataSource getPoolingDataSource(JdbcConfiguration configuration, String connectURI,
        String userName, String password, Driver driver) {
    Properties properties = new Properties();
    properties.put("user", userName);
    properties.put("password", password);

    // DBCP factory which will produce JDBC Driver instances
    ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, connectURI, properties);

    // DBCP Factory holding the pooled connection, which are created by the driver connection factory and held in the supplied pool
    ObjectName dataSourceJmxName;
    try {//from  ww w  .ja v a 2s.  c  o  m
        dataSourceJmxName = new ObjectName("no.difi.oxalis", "connectionPool", "OxalisDB");
    } catch (MalformedObjectNameException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory,
            dataSourceJmxName);

    if (configuration.getValidationQuery().isPresent()) {
        poolableConnectionFactory.setValidationQuery(configuration.getValidationQuery().get());
    }
    // DBCP object pool holding our driver connections
    GenericObjectPool<PoolableConnection> genericObjectPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(genericObjectPool);
    genericObjectPool.setMaxTotal(100);
    genericObjectPool.setMaxIdle(30);
    genericObjectPool.setMaxWaitMillis(10000);

    genericObjectPool.setTestOnBorrow(true); // Test the connection returned from the pool

    genericObjectPool.setTestWhileIdle(true); // Test idle instances visited by the pool maintenance thread and destroy any that fail validation
    genericObjectPool.setTimeBetweenEvictionRunsMillis(60 * 60 * 1000); // Test every hour

    // Creates the actual DataSource instance
    return new PoolingDataSource(genericObjectPool);
}