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

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

Introduction

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

Prototype

public synchronized void setMaxActive(int maxActive) 

Source Link

Document

Sets the cap on the number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time.

Usage

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

public void testMaxWaitMultiThreaded() throws Exception {
    final long maxWait = 500; // wait for connection
    final long holdTime = 2 * maxWait; // how long to hold connection
    final int threads = 10; // number of threads to grab the object initially
    SimpleFactory factory = new SimpleFactory();
    GenericObjectPool pool = new GenericObjectPool(factory);
    pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    pool.setMaxWait(maxWait);/*from  w  w  w.j  a v  a2  s.com*/
    pool.setMaxActive(threads);
    // Create enough threads so half the threads will have to wait
    WaitingTestThread wtt[] = new WaitingTestThread[threads * 2];
    for (int i = 0; i < wtt.length; i++) {
        wtt[i] = new WaitingTestThread(pool, holdTime);
    }
    long origin = System.currentTimeMillis() - 1000;
    for (int i = 0; i < wtt.length; i++) {
        wtt[i].start();
    }
    int failed = 0;
    for (int i = 0; i < wtt.length; i++) {
        wtt[i].join();
        if (wtt[i]._thrown != null) {
            failed++;
        }
    }
    if (DISPLAY_THREAD_DETAILS || wtt.length / 2 != failed) {
        System.out.println("MaxWait: " + maxWait + " HoldTime: " + holdTime + " MaxActive: " + threads
                + " Threads: " + wtt.length + " Failed: " + failed);
        for (int i = 0; i < wtt.length; i++) {
            WaitingTestThread wt = wtt[i];
            System.out.println("Preborrow: " + (wt.preborrow - origin) + " Postborrow: "
                    + (wt.postborrow != 0 ? wt.postborrow - origin : -1) + " BorrowTime: "
                    + (wt.postborrow != 0 ? wt.postborrow - wt.preborrow : -1) + " PostReturn: "
                    + (wt.postreturn != 0 ? wt.postreturn - origin : -1) + " Ended: " + (wt.ended - origin)
                    + " ObjId: " + wt.objectId);
        }
    }
    assertEquals("Expected half the threads to fail", wtt.length / 2, failed);
}

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();
        }/*from   w  w w .  j ava 2 s . c om*/

        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: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);/* w w w  .  ja  v  a 2s. co m*/
    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.aitools.util.db.DBConnectionManager.java

/**
 * Create a new database connection manager using the given driver (classname)
 * and database URI (DBMS-specific)./* w w w  . j  a v a2  s . com*/
 * 
 * @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();
}

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);//from ww  w.j ava  2 s.  c  o  m

    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;//from   www  .j a v a2  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  w w w  . j av a  2  s .  co  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;
    }
}

From source file:org.apache.hadoop.hive.metastore.txn.TxnHandler.java

private static synchronized DataSource setupJdbcConnectionPool(HiveConf conf, int maxPoolSize,
        long getConnectionTimeoutMs) throws SQLException {
    String driverUrl = HiveConf.getVar(conf, HiveConf.ConfVars.METASTORECONNECTURLKEY);
    String user = getMetastoreJdbcUser(conf);
    String passwd = getMetastoreJdbcPasswd(conf);
    String connectionPooler = conf.getVar(HiveConf.ConfVars.METASTORE_CONNECTION_POOLING_TYPE).toLowerCase();

    if ("bonecp".equals(connectionPooler)) {
        BoneCPConfig config = new BoneCPConfig();
        config.setJdbcUrl(driverUrl);//from ww  w .j  av  a 2  s. c  o  m
        //if we are waiting for connection for a long time, something is really wrong
        //better raise an error than hang forever
        //see DefaultConnectionStrategy.getConnectionInternal()
        config.setConnectionTimeoutInMs(getConnectionTimeoutMs);
        config.setMaxConnectionsPerPartition(maxPoolSize);
        config.setPartitionCount(1);
        config.setUser(user);
        config.setPassword(passwd);
        doRetryOnConnPool = true; // Enable retries to work around BONECP bug.
        return new BoneCPDataSource(config);
    } else if ("dbcp".equals(connectionPooler)) {
        GenericObjectPool objectPool = new GenericObjectPool();
        //https://commons.apache.org/proper/commons-pool/api-1.6/org/apache/commons/pool/impl/GenericObjectPool.html#setMaxActive(int)
        objectPool.setMaxActive(maxPoolSize);
        objectPool.setMaxWait(getConnectionTimeoutMs);
        ConnectionFactory connFactory = new DriverManagerConnectionFactory(driverUrl, user, passwd);
        // This doesn't get used, but it's still necessary, see
        // http://svn.apache.org/viewvc/commons/proper/dbcp/branches/DBCP_1_4_x_BRANCH/doc/ManualPoolingDataSourceExample.java?view=markup
        PoolableConnectionFactory poolConnFactory = new PoolableConnectionFactory(connFactory, objectPool, null,
                null, false, true);
        return new PoolingDataSource(objectPool);
    } else if ("hikaricp".equals(connectionPooler)) {
        HikariConfig config = new HikariConfig();
        config.setMaximumPoolSize(maxPoolSize);
        config.setJdbcUrl(driverUrl);
        config.setUsername(user);
        config.setPassword(passwd);
        //https://github.com/brettwooldridge/HikariCP
        config.setConnectionTimeout(getConnectionTimeoutMs);

        return new HikariDataSource(config);
    } else if ("none".equals(connectionPooler)) {
        LOG.info("Choosing not to pool JDBC connections");
        return new NoPoolConnectionPool(conf);
    } else {
        throw new RuntimeException("Unknown JDBC connection pooling " + connectionPooler);
    }
}

From source file:org.apache.ojb.broker.accesslayer.ConnectionFactoryDBCPImpl.java

protected ObjectPool createConnectionPool(GenericObjectPool.Config config, AbandonedConfig ac) {
    final GenericObjectPool connectionPool;
    final boolean doRemoveAbandoned = ac != null && ac.getRemoveAbandoned();

    if (doRemoveAbandoned) {
        connectionPool = new AbandonedObjectPool(null, ac);
    } else {/* www.ja  v a2s. c  om*/
        connectionPool = new GenericObjectPool();
    }
    connectionPool.setMaxActive(config.maxActive);
    connectionPool.setMaxIdle(config.maxIdle);
    connectionPool.setMinIdle(config.minIdle);
    connectionPool.setMaxWait(config.maxWait);
    connectionPool.setTestOnBorrow(config.testOnBorrow);
    connectionPool.setTestOnReturn(config.testOnReturn);
    connectionPool.setTimeBetweenEvictionRunsMillis(config.timeBetweenEvictionRunsMillis);
    connectionPool.setNumTestsPerEvictionRun(config.numTestsPerEvictionRun);
    connectionPool.setMinEvictableIdleTimeMillis(config.minEvictableIdleTimeMillis);
    connectionPool.setTestWhileIdle(config.testWhileIdle);
    return connectionPool;
}

From source file:org.apache.slide.store.impl.rdbms.JDBCStore.java

/**
 * Initializes driver.//from   ww w .j  a  va2  s. c  o m
 * <p/>
 * Occurs in four steps :
 * <li>Driver class is loaded</li>
 * <li>Driver is instantiated</li>
 * <li>Driver registration in the driver manager</li>
 * <li>Creation of the basic tables, if they didn't exist before</li>
 * 
 * @exception ServiceInitializationFailedException Throws an exception 
 * if the data source has already been initialized before
 */
public synchronized void initialize(NamespaceAccessToken token) throws ServiceInitializationFailedException {

    // XXX might be done already in setParameter
    if (!alreadyInitialized) {
        try {
            // Loading and registering driver
            getLogger().log("Loading and registering driver '" + driver + "'", LOG_CHANNEL, Logger.INFO);
            Class driverClass = Class.forName(driver);
            Driver driverInstance = (Driver) driverClass.newInstance();

            String levelString = isolationLevelToString(isolationLevel);

            getLogger().log("Setting isolation level '" + levelString + "'", LOG_CHANNEL, Logger.INFO);

            // use DBCP pooling if enabled
            if (useDbcpPooling) {
                getLogger().log("Using DBCP pooling", LOG_CHANNEL, Logger.INFO);
                GenericObjectPool connectionPool = new GenericObjectPool(null);
                if (maxPooledConnections != -1) {
                    connectionPool.setMaxActive(maxPooledConnections);
                }
                getLogger().log("Number of connections set to " + connectionPool.getMaxActive(), LOG_CHANNEL,
                        Logger.INFO);

                DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, user,
                        password);
                new PoolableConnectionFactory(connectionFactory, connectionPool,
                        // TODO switching on pooling of prepared statements causes problems with closing of connections
                        // switched off for now
                        //                  new StackKeyedObjectPoolFactory(),
                        null, null, false, false, isolationLevel);
                PoolingDriver driver = new PoolingDriver();
                driver.registerPool(dbcpPoolName, connectionPool);
                // already done when loding PoolingDriver class 
                //                DriverManager.registerDriver(driver);
            } else {
                DriverManager.registerDriver(driverInstance);
                getLogger().log("Not using DBCP pooling", LOG_CHANNEL, Logger.WARNING);
            }
        } catch (Exception e) {
            getLogger().log("Loading and registering driver '" + driver + "' failed (" + e.getMessage() + ")",
                    LOG_CHANNEL, Logger.ERROR);
            throw new ServiceInitializationFailedException(this, e);
        } finally {
            alreadyInitialized = true;
        }
    }

}