Example usage for org.apache.commons.pool.impl GenericObjectPool setMinIdle

List of usage examples for org.apache.commons.pool.impl GenericObjectPool setMinIdle

Introduction

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

Prototype

public synchronized void setMinIdle(int minIdle) 

Source Link

Document

Sets the minimum number of objects allowed in the pool before the evictor thread (if active) spawns new objects.

Usage

From source file:com.tethrnet.manage.util.DSPool.java

/**
 * register the data source for H2 DB//from   ww  w  .ja  va  2  s .c om
 *
 * @return pooling database object
 */

private static PoolingDataSource registerDataSource() {

    // create a database connection
    String user = "tethrnetbox";
    String password = "filepwd 45WJLnwhpA47EepT162hrVnDn3vYRvJhpZi0sVdvN9Sdsf";
    String connectionURI = "jdbc:h2:" + DB_PATH + "/tethrnetbox;CIPHER=AES";

    String validationQuery = "select 1";

    try {
        Class.forName("org.h2.Driver");
    } catch (ClassNotFoundException ex) {
        log.error(ex.toString(), ex);
    }

    GenericObjectPool connectionPool = new GenericObjectPool(null);

    connectionPool.setMaxActive(25);
    connectionPool.setTestOnBorrow(true);
    connectionPool.setMinIdle(2);
    connectionPool.setMaxWait(15000);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionURI, user, password);

    new PoolableConnectionFactory(connectionFactory, connectionPool, null, validationQuery, false, true);

    return new PoolingDataSource(connectionPool);

}

From source file:com.keybox.manage.util.DSPool.java

/**
 * register the data source for H2 DB/*from  ww w .  j  a v  a 2  s  . c  om*/
 *
 * @return pooling database object
 */

private static PoolingDataSource registerDataSource() {

    // create a database connection
    String user = "keybox";
    String password = "filepwd 45WJLnwhpA47EepT162hrVnDn3vYRvJhpZi0sVdvN9Sdsf";
    String connectionURI = "jdbc:h2:" + getDBPath() + "/keybox;CIPHER=AES";

    String validationQuery = "select 1";

    try {
        Class.forName("org.h2.Driver");
    } catch (ClassNotFoundException ex) {
        log.error(ex.toString(), ex);
    }

    GenericObjectPool connectionPool = new GenericObjectPool(null);

    connectionPool.setMaxActive(MAX_ACTIVE);
    connectionPool.setTestOnBorrow(TEST_ON_BORROW);
    connectionPool.setMinIdle(MIN_IDLE);
    connectionPool.setMaxWait(MAX_WAIT);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionURI, user, password);

    new PoolableConnectionFactory(connectionFactory, connectionPool, null, validationQuery, false, true);

    return new PoolingDataSource(connectionPool);

}

From source file:com.ec2box.manage.util.DSPool.java

/**
 * register the data source for H2 DB/*ww w .j  a va 2  s.c o m*/
 *
 * @return pooling database object
 */

private static PoolingDataSource registerDataSource() {

    // create a database connection
    String user = "ec2box";
    String password = "filepwd 0WJLnwhpA47EepT1A4drVnDn3vYRvJhpZi0sVdvN9SmlbKw";
    String connectionURI = "jdbc:h2:" + getDBPath() + "/ec2box;CIPHER=AES;";

    if (StringUtils.isNotEmpty(DB_OPTIONS)) {
        connectionURI = connectionURI + DB_OPTIONS;
    }

    String validationQuery = "select 1";

    try {
        Class.forName("org.h2.Driver");
    } catch (ClassNotFoundException ex) {
        log.error(ex.toString(), ex);
    }

    GenericObjectPool connectionPool = new GenericObjectPool(null);

    connectionPool.setMaxActive(MAX_ACTIVE);
    connectionPool.setTestOnBorrow(TEST_ON_BORROW);
    connectionPool.setMinIdle(MIN_IDLE);
    connectionPool.setMaxWait(MAX_WAIT);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionURI, user, password);

    new PoolableConnectionFactory(connectionFactory, connectionPool, null, validationQuery, false, true);

    return new PoolingDataSource(connectionPool);

}

From source file:lineage2.commons.dbcp.BasicDataSource.java

/**
 * Constructor for BasicDataSource./*from   w ww. j  av  a2  s . c om*/
 * @param driver String
 * @param connectURI String
 * @param uname String
 * @param passwd String
 * @param maxActive int
 * @param maxIdle int
 * @param idleTimeOut int
 * @param idleTestPeriod int
 * @param poolPreparedStatements boolean
 */
public BasicDataSource(String driver, String connectURI, String uname, String passwd, int maxActive,
        int maxIdle, int idleTimeOut, int idleTestPeriod, boolean poolPreparedStatements) {
    GenericObjectPool<?> connectionPool = new GenericObjectPool<>(null);
    connectionPool.setMaxActive(maxActive);
    connectionPool.setMaxIdle(maxIdle);
    connectionPool.setMinIdle(1);
    connectionPool.setMaxWait(-1L);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    connectionPool.setTestOnBorrow(false);
    connectionPool.setTestWhileIdle(true);
    connectionPool.setTimeBetweenEvictionRunsMillis(idleTestPeriod * 1000L);
    connectionPool.setNumTestsPerEvictionRun(maxActive);
    connectionPool.setMinEvictableIdleTimeMillis(idleTimeOut * 1000L);
    GenericKeyedObjectPoolFactory<?, ?> statementPoolFactory = null;
    if (poolPreparedStatements) {
        statementPoolFactory = new GenericKeyedObjectPoolFactory<>(null, -1,
                GenericObjectPool.WHEN_EXHAUSTED_FAIL, 0L, 1, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL);
    }
    Properties connectionProperties = new Properties();
    connectionProperties.put("user", uname);
    connectionProperties.put("password", passwd);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, connectionProperties);
    @SuppressWarnings("unused")
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, statementPoolFactory, "SELECT 1", false, true);
    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
    _connectionPool = connectionPool;
    _source = dataSource;
}

From source file:com.nesscomputing.syslog4j.impl.pool.generic.GenericSyslogPoolFactory.java

protected void configureGenericObjectPool(GenericObjectPool<AbstractSyslogWriter> genericObjectPool)
        throws SyslogRuntimeException {
    SyslogPoolConfigIF poolConfig = null;

    try {//from  w  w w . j  ava  2  s .c o  m
        poolConfig = (SyslogPoolConfigIF) this.syslog.getConfig();

    } catch (ClassCastException cce) {
        throw new SyslogRuntimeException("config must implement interface SyslogPoolConfigIF");
    }

    genericObjectPool.setMaxActive(poolConfig.getMaxActive());
    genericObjectPool.setMaxIdle(poolConfig.getMaxIdle());
    genericObjectPool.setMaxWait(poolConfig.getMaxWait());
    genericObjectPool.setMinEvictableIdleTimeMillis(poolConfig.getMinEvictableIdleTimeMillis());
    genericObjectPool.setMinIdle(poolConfig.getMinIdle());
    genericObjectPool.setNumTestsPerEvictionRun(poolConfig.getNumTestsPerEvictionRun());
    genericObjectPool.setSoftMinEvictableIdleTimeMillis(poolConfig.getSoftMinEvictableIdleTimeMillis());
    genericObjectPool.setTestOnBorrow(poolConfig.isTestOnBorrow());
    genericObjectPool.setTestOnReturn(poolConfig.isTestOnReturn());
    genericObjectPool.setTestWhileIdle(poolConfig.isTestWhileIdle());
    genericObjectPool.setTimeBetweenEvictionRunsMillis(poolConfig.getTimeBetweenEvictionRunsMillis());
    genericObjectPool.setWhenExhaustedAction(poolConfig.getWhenExhaustedAction());
}

From source file:com.zotoh.core.db.JDBCPoolManager.java

private synchronized JDBCPool create(String pool, JDBCInfo param, Properties props) throws SQLException {
    if (existsPool(pool)) {
        throw new SQLException("Jdbc Pool already exists: " + pool);
    }/*ww w .  ja v a 2  s .  c  om*/

    PoolableConnectionFactory pcf;
    DriverConnectionFactory dcf;
    GenericObjectPool gop;
    DBVendor dbv;
    ObjectPool p;
    Driver d;

    tlog().debug("JDBCPoolMgr: Driver : {}", param.getDriver());
    tlog().debug("JDBCPoolMgr: URL : {}", param.getUrl());

    //        Ute.loadDriver(param.getDriver());
    d = DriverManager.getDriver(param.getUrl());
    dbv = DBUte.getDBVendor(param);

    dcf = new DriverConnectionFactory(d, param.getUrl(), props);
    gop = new GenericObjectPool();
    gop.setMaxActive(asInt(props.getProperty("max-conns"), 10));
    gop.setTestOnBorrow(true);
    gop.setMaxIdle(gop.getMaxActive());
    gop.setMinIdle(asInt(props.getProperty("min-conns"), 2));
    gop.setMaxWait(asLong(props.getProperty("max-wait4-conn-millis"), 1500L));
    gop.setMinEvictableIdleTimeMillis(asLong(props.getProperty("evict-conn-ifidle-millis"), 300000L));
    gop.setTimeBetweenEvictionRunsMillis(asLong(props.getProperty("check-evict-every-millis"), 60000L));

    pcf = new PoolableConnectionFactory(dcf, gop, null, null, true, false);
    pcf.setDefaultReadOnly(false);
    p = pcf.getPool();

    JDBCPool j = new JDBCPool(dbv, param, p);
    _ps.put(pool, j);

    tlog().debug("JDBCPoolMgr: Added db pool: {}, info= {}", pool, param);
    return j;
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java

/**
 * Tests addObject contention between ensureMinIdle triggered by the Evictor
 * with minIdle > 0 and borrowObject.
 *//*from ww w  . j  av  a  2s .  co  m*/
public void testEvictAddObjects() throws Exception {
    SimpleFactory factory = new SimpleFactory();
    factory.setMakeLatency(300);
    factory.setMaxActive(2);
    GenericObjectPool pool = new GenericObjectPool(factory);
    pool.setMaxActive(2);
    pool.setMinIdle(1);
    pool.borrowObject(); // numActive = 1, numIdle = 0
    // Create a test thread that will run once and try a borrow after
    // 150ms fixed delay
    TestThread borrower = new TestThread(pool, 1, 150, false);
    Thread borrowerThread = new Thread(borrower);
    // Set evictor to run in 100 ms - will create idle instance
    pool.setTimeBetweenEvictionRunsMillis(100);
    borrowerThread.start(); // Off to the races
    borrowerThread.join();
    assertTrue(!borrower.failed());
    pool.close();
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java

public void checkEvict(boolean lifo) throws Exception {
    // yea this is hairy but it tests all the code paths in GOP.evict()
    final SimpleFactory factory = new SimpleFactory();
    final GenericObjectPool pool = new GenericObjectPool(factory);
    pool.setSoftMinEvictableIdleTimeMillis(10);
    pool.setMinIdle(2);
    pool.setTestWhileIdle(true);/*from   w w w.  java  2 s .  co m*/
    pool.setLifo(lifo);
    PoolUtils.prefill(pool, 5);
    pool.evict();
    factory.setEvenValid(false);
    factory.setOddValid(false);
    factory.setThrowExceptionOnActivate(true);
    pool.evict();
    PoolUtils.prefill(pool, 5);
    factory.setThrowExceptionOnActivate(false);
    factory.setThrowExceptionOnPassivate(true);
    pool.evict();
    factory.setThrowExceptionOnPassivate(false);
    factory.setEvenValid(true);
    factory.setOddValid(true);
    Thread.sleep(125);
    pool.evict();
    assertEquals(2, pool.getNumIdle());
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java

public void testEvictionSoftMinIdle() throws Exception {
    GenericObjectPool pool = null;

    class TimeTest extends BasePoolableObjectFactory {
        private final long createTime;

        public TimeTest() {
            createTime = System.currentTimeMillis();
        }//  w w  w  .j ava  2 s.co  m

        public Object makeObject() throws Exception {
            return new TimeTest();
        }

        public long getCreateTime() {
            return createTime;
        }
    }

    pool = new GenericObjectPool(new TimeTest());

    pool.setMaxIdle(5);
    pool.setMaxActive(5);
    pool.setNumTestsPerEvictionRun(5);
    pool.setMinEvictableIdleTimeMillis(3000L);
    pool.setSoftMinEvictableIdleTimeMillis(1000L);
    pool.setMinIdle(2);

    Object[] active = new Object[5];
    Long[] creationTime = new Long[5];
    for (int i = 0; i < 5; i++) {
        active[i] = pool.borrowObject();
        creationTime[i] = new Long(((TimeTest) active[i]).getCreateTime());
    }

    for (int i = 0; i < 5; i++) {
        pool.returnObject(active[i]);
    }

    // Soft evict all but minIdle(2)
    Thread.sleep(1500L);
    pool.evict();
    assertEquals("Idle count different than expected.", 2, pool.getNumIdle());

    // Hard evict the rest.
    Thread.sleep(2000L);
    pool.evict();
    assertEquals("Idle count different than expected.", 0, pool.getNumIdle());
}

From source file:org.aitools.util.db.DBConnectionManager.java

/**
 * Create a new database connection manager using the given driver (classname)
 * and database URI (DBMS-specific)./*from w  w w. j  a va  2 s  . c om*/
 * 
 * @param logger
 * @param driver
 * @param uri
 * @param username
 * @param password
 * @param minIdle
 * @param maxActive
 */
public DBConnectionManager(String driver, String uri, String username, String password, int minIdle,
        int maxActive) {

    Classes.verifyAvailable(driver, "database driver");

    GenericObjectPool connectionPool = new GenericObjectPool(null);
    connectionPool.setMinIdle(minIdle);
    connectionPool.setMaxActive(maxActive);

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(uri, username, password);

    @SuppressWarnings("unused")
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);

    this._dataSource = new PoolingDataSource(connectionPool);

    // Was using DdlUtils here, but it did not correctly work for all column properties.
    //this.checkDBSchema();
}