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

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

Introduction

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

Prototype

public synchronized void setLifo(boolean lifo) 

Source Link

Document

Sets the LIFO property of the pool.

Usage

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

private void checkEvictionOrder(boolean lifo) throws Exception {
    SimpleFactory factory = new SimpleFactory();
    GenericObjectPool pool = new GenericObjectPool(factory);
    pool.setNumTestsPerEvictionRun(2);//from  w ww  . ja  v  a  2s. c  o  m
    pool.setMinEvictableIdleTimeMillis(100);
    pool.setLifo(lifo);
    for (int i = 0; i < 5; i++) {
        pool.addObject();
        Thread.sleep(100);
    }
    // Order, oldest to youngest, is "0", "1", ...,"4"
    pool.evict(); // Should evict "0" and "1"
    Object obj = pool.borrowObject();
    assertTrue("oldest not evicted", !obj.equals("0"));
    assertTrue("second oldest not evicted", !obj.equals("1"));
    // 2 should be next out for FIFO, 4 for LIFO
    assertEquals("Wrong instance returned", lifo ? "4" : "2", obj);

    // Two eviction runs in sequence
    factory = new SimpleFactory();
    pool = new GenericObjectPool(factory);
    pool.setNumTestsPerEvictionRun(2);
    pool.setMinEvictableIdleTimeMillis(100);
    pool.setLifo(lifo);
    for (int i = 0; i < 5; i++) {
        pool.addObject();
        Thread.sleep(100);
    }
    pool.evict(); // Should evict "0" and "1"
    pool.evict(); // Should evict "2" and "3"
    obj = pool.borrowObject();
    assertEquals("Wrong instance remaining in pool", "4", obj);
}

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);//w ww . j  a v a2  s  .  c om
    pool.setTestWhileIdle(true);
    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

private void checkEvictorVisiting(boolean lifo) throws Exception {
    VisitTrackerFactory factory = new VisitTrackerFactory();
    GenericObjectPool pool = new GenericObjectPool(factory);
    pool.setNumTestsPerEvictionRun(2);//from   w w w .  j  a  va 2 s  .  c  om
    pool.setMinEvictableIdleTimeMillis(-1);
    pool.setTestWhileIdle(true);
    pool.setLifo(lifo);
    pool.setTestOnReturn(false);
    pool.setTestOnBorrow(false);
    for (int i = 0; i < 8; i++) {
        pool.addObject();
    }
    pool.evict(); // Visit oldest 2 - 0 and 1
    Object obj = pool.borrowObject();
    pool.returnObject(obj);
    obj = pool.borrowObject();
    pool.returnObject(obj);
    // borrow, return, borrow, return
    // FIFO will move 0 and 1 to end
    // LIFO, 7 out, then in, then out, then in
    pool.evict(); // Should visit 2 and 3 in either case
    for (int i = 0; i < 8; i++) {
        VisitTracker tracker = (VisitTracker) pool.borrowObject();
        if (tracker.getId() >= 4) {
            assertEquals("Unexpected instance visited " + tracker.getId(), 0, tracker.getValidateCount());
        } else {
            assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 1,
                    tracker.getValidateCount());
        }
    }

    factory = new VisitTrackerFactory();
    pool = new GenericObjectPool(factory);
    pool.setNumTestsPerEvictionRun(3);
    pool.setMinEvictableIdleTimeMillis(-1);
    pool.setTestWhileIdle(true);
    pool.setLifo(lifo);
    pool.setTestOnReturn(false);
    pool.setTestOnBorrow(false);
    for (int i = 0; i < 8; i++) {
        pool.addObject();
    }
    pool.evict(); // 0, 1, 2
    pool.evict(); // 3, 4, 5
    obj = pool.borrowObject();
    pool.returnObject(obj);
    obj = pool.borrowObject();
    pool.returnObject(obj);
    obj = pool.borrowObject();
    pool.returnObject(obj);
    // borrow, return, borrow, return
    // FIFO 3,4,5,6,7,0,1,2
    // LIFO 7,6,5,4,3,2,1,0
    // In either case, pointer should be at 6
    pool.evict();
    // Should hit 6,7,0 - 0 for second time
    for (int i = 0; i < 8; i++) {
        VisitTracker tracker = (VisitTracker) pool.borrowObject();
        if (tracker.getId() != 0) {
            assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 1,
                    tracker.getValidateCount());
        } else {
            assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 2,
                    tracker.getValidateCount());
        }
    }
    // Randomly generate a pools with random numTests
    // and make sure evictor cycles through elements appropriately
    int[] smallPrimes = { 2, 3, 5, 7 };
    Random random = new Random();
    random.setSeed(System.currentTimeMillis());
    for (int i = 0; i < 4; i++) {
        pool.setNumTestsPerEvictionRun(smallPrimes[i]);
        for (int j = 0; j < 5; j++) {
            pool = new GenericObjectPool(factory);
            pool.setNumTestsPerEvictionRun(3);
            pool.setMinEvictableIdleTimeMillis(-1);
            pool.setTestWhileIdle(true);
            pool.setLifo(lifo);
            pool.setTestOnReturn(false);
            pool.setTestOnBorrow(false);
            pool.setMaxIdle(-1);
            int instanceCount = 10 + random.nextInt(20);
            pool.setMaxActive(instanceCount);
            for (int k = 0; k < instanceCount; k++) {
                pool.addObject();
            }

            // Execute a random number of evictor runs
            int runs = 10 + random.nextInt(50);
            for (int k = 0; k < runs; k++) {
                pool.evict();
            }

            // Number of times evictor should have cycled through the pool
            int cycleCount = (runs * pool.getNumTestsPerEvictionRun()) / instanceCount;

            // Look at elements and make sure they are visited cycleCount
            // or cycleCount + 1 times
            VisitTracker tracker = null;
            int visitCount = 0;
            for (int k = 0; k < instanceCount; k++) {
                tracker = (VisitTracker) pool.borrowObject();
                assertTrue(pool.getNumActive() <= pool.getMaxActive());
                visitCount = tracker.getValidateCount();
                assertTrue(visitCount >= cycleCount && visitCount <= cycleCount + 1);
            }
        }
    }
}

From source file:org.apache.hadoop.hive.metastore.datasource.DbCPDataSourceProvider.java

@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);
    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, GenericObjectPool.DEFAULT_MAX_IDLE);
    int connectionMinIlde = hdpConfig.getInt(CONNECTION_MIN_IDLE_PROPERTY, GenericObjectPool.DEFAULT_MIN_IDLE);
    boolean testOnBorrow = hdpConfig.getBoolean(CONNECTION_TEST_BORROW_PROPERTY,
            GenericObjectPool.DEFAULT_TEST_ON_BORROW);
    long evictionTimeMillis = hdpConfig.getLong(CONNECTION_MIN_EVICT_MILLIS_PROPERTY,
            GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
    boolean testWhileIdle = hdpConfig.getBoolean(CONNECTION_TEST_IDLEPROPERTY,
            GenericObjectPool.DEFAULT_TEST_WHILE_IDLE);
    long timeBetweenEvictionRuns = hdpConfig.getLong(CONNECTION_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
    int numTestsPerEvictionRun = hdpConfig.getInt(CONNECTION_NUM_TESTS_PER_EVICTION_RUN,
            GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN);
    boolean testOnReturn = hdpConfig.getBoolean(CONNECTION_TEST_ON_RETURN,
            GenericObjectPool.DEFAULT_TEST_ON_RETURN);
    long softMinEvictableIdleTimeMillis = hdpConfig.getLong(CONNECTION_SOFT_MIN_EVICTABLE_IDLE_TIME,
            GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
    boolean lifo = hdpConfig.getBoolean(CONNECTION_LIFO, GenericObjectPool.DEFAULT_LIFO);

    GenericObjectPool objectPool = new GenericObjectPool();
    objectPool.setMaxActive(maxPoolSize);
    objectPool.setMaxWait(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);

    ConnectionFactory connFactory = new DriverManagerConnectionFactory(driverUrl, user, passwd);
    // This doesn't get used, but it's still necessary, see
    // https://git1-us-west.apache.org/repos/asf?p=commons-dbcp.git;a=blob;f=doc/ManualPoolingDataSourceExample.java;
    // h=f45af2b8481f030b27364e505984c0eef4f35cdb;hb=refs/heads/DBCP_1_5_x_BRANCH
    new PoolableConnectionFactory(connFactory, objectPool, null, null, false, true);

    return new PoolingDataSource(objectPool);
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

public synchronized static void checkGlobalPoolingDataSource(String url, String user, String pass) {
    if (globalDbConPool != null) {
        return;//  w  w  w. j ava 2  s  .co m
    } else {
        LOG.error(
                "#################################################################### init master connetion pool");
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, user, pass);
        GenericObjectPool connectionPool = new GenericObjectPool();
        connectionPool.setMaxActive(poolActiveSize);
        connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
        connectionPool.setMaxWait(10000);
        connectionPool.setTestOnBorrow(true);
        connectionPool.setTestWhileIdle(true);
        connectionPool.setTimeBetweenEvictionRunsMillis(30000);
        connectionPool.setMinEvictableIdleTimeMillis(300000);
        connectionPool.setLifo(true);

        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, null, false, true);

        globalDbConPool = new PoolingDataSource(connectionPool);
        LOG.error(
                "#################################################################### init global connetion pool over");
    }
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

public synchronized static PoolingDataSource getSegPoolingDataSource(String url, String user, String pass) {
    url = url.toLowerCase();//from www.j ava 2s  . c  o  m
    PoolingDataSource pool = SegmentDbConPool.get(url);
    if (pool != null) {
        return pool;
    } else {
        LOG.debug(
                "#################################################################### init global connetion pool:"
                        + url);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, user, pass);
        GenericObjectPool connectionPool = new GenericObjectPool();
        connectionPool.setMaxActive(poolActiveSize);
        connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
        connectionPool.setMaxWait(10000);
        connectionPool.setTestOnBorrow(true);
        connectionPool.setTestWhileIdle(true);
        connectionPool.setTimeBetweenEvictionRunsMillis(30000);
        connectionPool.setMinEvictableIdleTimeMillis(300000);
        connectionPool.setLifo(true);

        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, null, false, true);

        pool = new PoolingDataSource(connectionPool);
        SegmentDbConPool.put(url, pool);

        LOG.debug(
                "#################################################################### init global connetion pool:"
                        + url + " over");
        return pool;
    }
}