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

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

Introduction

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

Prototype

public synchronized void setWhenExhaustedAction(byte whenExhaustedAction) 

Source Link

Document

Sets the action to take when the #borrowObject method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).

Usage

From source file:com.panet.imeta.core.database.ConnectionPoolUtil.java

private static void createPool(DatabaseMeta databaseMeta, String partitionId, int initialSize, int maximumSize)
        throws KettleDatabaseException {
    LogWriter.getInstance().logBasic(databaseMeta.toString(),
            Messages.getString("Database.CreatingConnectionPool", databaseMeta.getName()));
    GenericObjectPool gpool = new GenericObjectPool();

    gpool.setMaxIdle(-1);/*from   www . ja v a  2  s .c  o  m*/
    gpool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    gpool.setMaxActive(maximumSize);

    String clazz = databaseMeta.getDriverClass();
    try {
        Class.forName(clazz).newInstance();
    } catch (Exception e) {
        throw new KettleDatabaseException(Messages.getString(
                "Database.UnableToLoadConnectionPoolDriver.Exception", databaseMeta.getName(), clazz), e);
    }

    String url;
    String userName;
    String password;

    try {
        url = databaseMeta.environmentSubstitute(databaseMeta.getURL(partitionId));
        userName = databaseMeta.environmentSubstitute(databaseMeta.getUsername());
        password = databaseMeta.environmentSubstitute(databaseMeta.getPassword());
    } catch (RuntimeException e) {
        url = databaseMeta.getURL(partitionId);
        userName = databaseMeta.getUsername();
        password = databaseMeta.getPassword();
    }

    // Get the list of pool properties
    Properties originalProperties = databaseMeta.getConnectionPoolingProperties();
    //Add user/pass
    originalProperties.setProperty("user", Const.NVL(userName, ""));
    originalProperties.setProperty("password", Const.NVL(password, ""));

    // Now, replace the environment variables in there...
    Properties properties = new Properties();
    Iterator<Object> iterator = originalProperties.keySet().iterator();
    while (iterator.hasNext()) {
        String key = (String) iterator.next();
        String value = originalProperties.getProperty(key);
        properties.put(key, databaseMeta.environmentSubstitute(value));
    }

    // Create factory using these properties.
    //
    ConnectionFactory cf = new DriverManagerConnectionFactory(url, properties);

    new PoolableConnectionFactory(cf, gpool, null, null, false, false);

    for (int i = 0; i < initialSize; i++) {
        try {
            gpool.addObject();
        } catch (Exception e) {
            throw new KettleDatabaseException(
                    Messages.getString("Database.UnableToPreLoadConnectionToConnectionPool.Exception"), e);
        }
    }

    pd.registerPool(databaseMeta.getName(), gpool);

    LogWriter.getInstance().logBasic(databaseMeta.toString(),
            Messages.getString("Database.CreatedConnectionPool", databaseMeta.getName()));
}

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

/**
 * register the data source for H2 DB/*from  w w  w.  ja va  2s  .com*/
 *
 * @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/*www . j  a v a2 s  .  c  o m*/
 *
 * @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/*w w  w . j a v a  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:com.iciql.test.IciqlSuite.java

/**
 * Open a new Db object. All connections are cached and re-used to eliminate
 * embedded database startup costs./*from w w  w. ja  v a 2 s  .  c  o  m*/
 * 
 * @return a fresh Db object
 */
public static Db openNewDb() {
    String testUrl = System.getProperty("iciql.url", DEFAULT_TEST_DB.url);
    String testUser = System.getProperty("iciql.user", DEFAULT_TEST_DB.username);
    String testPassword = System.getProperty("iciql.password", DEFAULT_TEST_DB.password);

    Db db = null;
    PoolingDataSource dataSource = dataSources.get(testUrl);
    if (dataSource == null) {
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(testUrl, testUser,
                testPassword);
        GenericObjectPool pool = new GenericObjectPool();
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
        PoolableConnectionFactory factory = new PoolableConnectionFactory(connectionFactory, pool, null, null,
                false, true);
        dataSource = new PoolingDataSource(pool);
        dataSources.put(testUrl, dataSource);
        connectionFactories.put(testUrl, factory);
    }
    db = Db.open(dataSource);

    // drop views
    db.dropView(ProductView.class);
    db.dropView(ProductViewInherited.class);
    db.dropView(ProductViewFromQuery.class);
    db.dropView(ProductViewInheritedComplex.class);

    // drop tables
    db.dropTable(BooleanModel.class);
    db.dropTable(ComplexObject.class);
    db.dropTable(Customer.class);
    db.dropTable(DefaultValuesModel.class);
    db.dropTable(EnumIdModel.class);
    db.dropTable(EnumOrdinalModel.class);
    db.dropTable(EnumStringModel.class);
    db.dropTable(Order.class);
    db.dropTable(PrimitivesModel.class);
    db.dropTable(Product.class);
    db.dropTable(ProductAnnotationOnly.class);
    db.dropTable(ProductInheritedAnnotation.class);
    db.dropTable(ProductMixedAnnotation.class);
    db.dropTable(SupportedTypes.class);
    db.dropTable(JoinTest.UserId.class);
    db.dropTable(JoinTest.UserNote.class);
    db.dropTable(EnumsTest.BadEnums.class);
    db.dropTable(MultipleBoolsModel.class);
    db.dropTable(ProductAnnotationOnlyWithForeignKey.class);
    db.dropTable(CategoryAnnotationOnly.class);

    return db;
}

From source file:Pool162.java

public void testWhenExhaustedBlockInterupt() throws Exception {
    GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
    //Set this value to 1 to get the deadlock. No deadlock when it sets to 
    //other values.
    pool.setMaxActive(2);// w w  w  .  j  a v a  2s.  co  m
    pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);

    Object obj1 = pool.borrowObject();

    // Make sure one object was obtained
    if (obj1 == null) {
        throw new Exception("obj1 is null");
    }

    // Create a separate thread to try and borrow another object
    WaitingTestThread wtt = new WaitingTestThread(pool, 200);
    wtt.start();
    // Give wtt time to start
    Thread.sleep(200);
    //bug trigger #1
    wtt.interrupt();
    //bug trigger #1
    // Give interrupt time to take effect
    Thread.sleep(200);
    // Return object to the pool
    pool.returnObject(obj1);
    Object obj2 = null;
    obj2 = pool.borrowObject();
    if (obj2 == null) {
        throw new Exception("obj2 is null");
    }
    pool.returnObject(obj2);
    pool.close();
}

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

/**
 * Constructor for BasicDataSource./*  w w  w .  j  ava 2  s . co m*/
 * @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 {// ww  w  .  ja va  2s. c om
        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: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 va  2 s.  co  m*/
    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 testSettersAndGetters() throws Exception {
    GenericObjectPool pool = new GenericObjectPool();
    {/*from  w w w .j a  v a 2 s.co  m*/
        pool.setFactory(new SimpleFactory());
    }
    {
        pool.setMaxActive(123);
        assertEquals(123, pool.getMaxActive());
    }
    {
        pool.setMaxIdle(12);
        assertEquals(12, pool.getMaxIdle());
    }
    {
        pool.setMaxWait(1234L);
        assertEquals(1234L, pool.getMaxWait());
    }
    {
        pool.setMinEvictableIdleTimeMillis(12345L);
        assertEquals(12345L, pool.getMinEvictableIdleTimeMillis());
    }
    {
        pool.setNumTestsPerEvictionRun(11);
        assertEquals(11, pool.getNumTestsPerEvictionRun());
    }
    {
        pool.setTestOnBorrow(true);
        assertTrue(pool.getTestOnBorrow());
        pool.setTestOnBorrow(false);
        assertTrue(!pool.getTestOnBorrow());
    }
    {
        pool.setTestOnReturn(true);
        assertTrue(pool.getTestOnReturn());
        pool.setTestOnReturn(false);
        assertTrue(!pool.getTestOnReturn());
    }
    {
        pool.setTestWhileIdle(true);
        assertTrue(pool.getTestWhileIdle());
        pool.setTestWhileIdle(false);
        assertTrue(!pool.getTestWhileIdle());
    }
    {
        pool.setTimeBetweenEvictionRunsMillis(11235L);
        assertEquals(11235L, pool.getTimeBetweenEvictionRunsMillis());
    }
    {
        pool.setSoftMinEvictableIdleTimeMillis(12135L);
        assertEquals(12135L, pool.getSoftMinEvictableIdleTimeMillis());
    }
    {
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction());
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_FAIL, pool.getWhenExhaustedAction());
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
    }
}