Example usage for org.apache.commons.pool.impl GenericKeyedObjectPool DEFAULT_TEST_ON_BORROW

List of usage examples for org.apache.commons.pool.impl GenericKeyedObjectPool DEFAULT_TEST_ON_BORROW

Introduction

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

Prototype

boolean DEFAULT_TEST_ON_BORROW

To view the source code for org.apache.commons.pool.impl GenericKeyedObjectPool DEFAULT_TEST_ON_BORROW.

Click Source Link

Document

The default "test on borrow" value.

Usage

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

public void testConstructors() {

    // Make constructor arguments all different from defaults
    int maxActive = 1;
    int maxIdle = 2;
    long maxWait = 3;
    int minIdle = 4;
    int maxTotal = 5;
    long minEvictableIdleTimeMillis = 6;
    int numTestsPerEvictionRun = 7;
    boolean testOnBorrow = true;
    boolean testOnReturn = true;
    boolean testWhileIdle = true;
    long timeBetweenEvictionRunsMillis = 8;
    byte whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
    boolean lifo = false;

    GenericKeyedObjectPool pool = new GenericKeyedObjectPool();
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_ACTIVE, pool.getMaxActive());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_IDLE, pool.getMaxIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_WAIT, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            pool.getMinEvictableIdleTimeMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, pool.getNumTestsPerEvictionRun());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW, pool.getTestOnBorrow());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN, pool.getTestOnReturn());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE, pool.getTestWhileIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
    config.lifo = lifo;/*from w w  w. j a va2 s  .  co  m*/
    config.maxActive = maxActive;
    config.maxIdle = maxIdle;
    config.minIdle = minIdle;
    config.maxTotal = maxTotal;
    config.maxWait = maxWait;
    config.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    config.numTestsPerEvictionRun = numTestsPerEvictionRun;
    config.testOnBorrow = testOnBorrow;
    config.testOnReturn = testOnReturn;
    config.testWhileIdle = testWhileIdle;
    config.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    config.whenExhaustedAction = whenExhaustedAction;
    pool = new GenericKeyedObjectPool(null, config);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(minIdle, pool.getMinIdle());
    assertEquals(maxTotal, pool.getMaxTotal());
    assertEquals(minEvictableIdleTimeMillis, pool.getMinEvictableIdleTimeMillis());
    assertEquals(numTestsPerEvictionRun, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(testWhileIdle, pool.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(lifo, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_IDLE, pool.getMaxIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_WAIT, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            pool.getMinEvictableIdleTimeMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, pool.getNumTestsPerEvictionRun());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW, pool.getTestOnBorrow());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN, pool.getTestOnReturn());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE, pool.getTestWhileIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_IDLE, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            pool.getMinEvictableIdleTimeMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, pool.getNumTestsPerEvictionRun());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW, pool.getTestOnBorrow());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN, pool.getTestOnReturn());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE, pool.getTestWhileIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, testOnBorrow,
            testOnReturn);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_IDLE, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            pool.getMinEvictableIdleTimeMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE, pool.getTestWhileIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, maxIdle);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            pool.getMinEvictableIdleTimeMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, pool.getNumTestsPerEvictionRun());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW, pool.getTestOnBorrow());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN, pool.getTestOnReturn());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE, pool.getTestWhileIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, maxIdle, testOnBorrow,
            testOnReturn);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            pool.getMinEvictableIdleTimeMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE, pool.getTestWhileIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, maxIdle, testOnBorrow,
            testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis,
            testWhileIdle);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(minEvictableIdleTimeMillis, pool.getMinEvictableIdleTimeMillis());
    assertEquals(numTestsPerEvictionRun, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(testWhileIdle, pool.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, maxIdle, maxTotal,
            testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun,
            minEvictableIdleTimeMillis, testWhileIdle);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(maxTotal, pool.getMaxTotal());
    assertEquals(minEvictableIdleTimeMillis, pool.getMinEvictableIdleTimeMillis());
    assertEquals(numTestsPerEvictionRun, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(testWhileIdle, pool.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, maxIdle, maxTotal, minIdle,
            testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun,
            minEvictableIdleTimeMillis, testWhileIdle);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(minIdle, pool.getMinIdle());
    assertEquals(maxTotal, pool.getMaxTotal());
    assertEquals(minEvictableIdleTimeMillis, pool.getMinEvictableIdleTimeMillis());
    assertEquals(numTestsPerEvictionRun, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(testWhileIdle, pool.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, maxIdle, maxTotal, minIdle,
            testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun,
            minEvictableIdleTimeMillis, testWhileIdle, lifo);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(minIdle, pool.getMinIdle());
    assertEquals(maxTotal, pool.getMaxTotal());
    assertEquals(minEvictableIdleTimeMillis, pool.getMinEvictableIdleTimeMillis());
    assertEquals(numTestsPerEvictionRun, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(testWhileIdle, pool.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(lifo, pool.getLifo());
}

From source file:org.openanzo.datasource.nodecentric.internal.NodeCentricDatasource.java

/**
 * Initialize a jdbc connection pool/*w ww. ja va2s .c o m*/
 * 
 * @param type
 *            either rw or query pool
 * @param maxActive
 *            maximum number of connections in pool
 * @param configuration
 *            configuration properties used for creating database connections
 * @return connection pool
 * @throws AnzoException
 *             {@link ExceptionConstants#RDB.DRIVER_NAME} if there was a problem loading class for database driver
 */
private GenericObjectPool initializeConnectionFactory(boolean write, int maxActive) throws AnzoException {
    // Will use in jndi
    // DataSource ds = (DataSource) ctx.lookup(RepositoryProperties.getDatabaseJndiName(properties));
    try {
        Class.forName(configuration.getDriverClassName());
    } catch (ClassNotFoundException e1) {
        throw new AnzoException(ExceptionConstants.RDB.DRIVER_NAME, e1, configuration.getDriverClassName());
    }
    Properties props = new Properties();
    props.put("user", configuration.getUser());
    props.put("password", configuration.getPassword());
    props.put("SetBigStringTryClob", "true");
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(configuration.getJdbcUrl(), props);
    GenericObjectPool connectionPool = new GenericObjectPool();
    connectionPool.setMaxActive(maxActive);
    connectionPool.setMaxIdle(Math.max(1, maxActive / 2));
    connectionPool.setMinIdle(0);
    connectionPool.setMinEvictableIdleTimeMillis(1000 * 60 * 10);
    connectionPool.setTimeBetweenEvictionRunsMillis(1000 * 60 * 10);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    connectionPool.setMaxWait(GenericKeyedObjectPool.DEFAULT_MAX_WAIT);
    connectionPool.setTestOnBorrow(true);
    GenericKeyedObjectPoolFactory statementPool = new GenericKeyedObjectPoolFactory(null, PS_CACHE_SIZE,
            GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, GenericKeyedObjectPool.DEFAULT_MAX_WAIT, PS_CACHE_SIZE,
            PS_CACHE_SIZE, GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW,
            GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN,
            GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,
            GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE);
    PoolableConnectionFactory pcf = new PoolableConnectionFactory(connectionFactory, connectionPool,
            statementPool, configuration.getValidationQuery(), false, true) {
        @Override
        public synchronized Object makeObject() throws Exception {
            Connection connection = (Connection) super.makeObject();
            initializeConnection(connection);
            return connection;
        }
    };

    if (configuration.getSupportsIsolation() && write)
        pcf.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
    else if (configuration.getSupportsIsolation() && !write)
        pcf.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
    return connectionPool;
}