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

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

Introduction

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

Prototype

public synchronized void setMaxWait(long maxWait) 

Source Link

Document

Sets the maximum amount of time (in milliseconds) the #borrowObject method should block before throwing an exception when the pool is exhausted and the #setWhenExhaustedAction "when exhausted" action is #WHEN_EXHAUSTED_BLOCK .

Usage

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

/**
 * register the data source for H2 DB/*from  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 = "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/* ww w .j a  v  a  2  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/*from  w ww . jav 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:eu.peppol.jdbc.OxalisDataSourceFactoryDbcpImpl.java

/**
 * Creates a DataSource with connection pooling as provided by Apache DBCP
 *
 * @return a DataSource//from  w  w w  .  ja  v  a2s .com
 */
public static DataSource configureAndCreateDataSource() {

    log.debug("Configuring DataSource wrapped in a Database Connection Pool, using custom loader");

    GlobalConfiguration globalConfiguration = GlobalConfigurationImpl.getInstance();

    String jdbcDriverClassPath = globalConfiguration.getJdbcDriverClassPath();

    log.debug("Loading JDBC Driver with custom class path: " + jdbcDriverClassPath);
    // Creates a new class loader, which will be used for loading our JDBC driver
    URLClassLoader urlClassLoader = getOxalisClassLoaderForJdbc(jdbcDriverClassPath);

    String className = globalConfiguration.getJdbcDriverClassName();
    String connectURI = globalConfiguration.getJdbcConnectionURI();
    String userName = globalConfiguration.getJdbcUsername();
    String password = globalConfiguration.getJdbcPassword();

    log.debug("className=" + className);
    log.debug("connectURI=" + connectURI);
    log.debug("userName=" + userName);
    log.debug("password=" + password);

    // Loads the JDBC Driver in a separate class loader
    Driver driver = getJdbcDriver(jdbcDriverClassPath, urlClassLoader, className);

    Properties properties = new Properties();
    properties.put("user", userName);
    properties.put("password", password);

    // DBCP factory which will produce JDBC Driver instances
    ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, connectURI, properties);

    // DBCP object pool holding our driver connections
    GenericObjectPool genericObjectPool = new GenericObjectPool(null);
    genericObjectPool.setMaxActive(100);
    genericObjectPool.setMaxIdle(30);
    genericObjectPool.setMaxWait(10000);

    genericObjectPool.setTestOnBorrow(true); // Test the connection returned from the pool

    genericObjectPool.setTestWhileIdle(true); // Test idle instances visited by the pool maintenance thread and destroy any that fail validation
    genericObjectPool.setTimeBetweenEvictionRunsMillis(60 * 60 * 1000); // Test every hour

    // DBCP Factory holding the pooled connection, which are created by the driver connection factory and held in the supplied pool
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory,
            genericObjectPool, null, null, false, true);

    String validationQuery = globalConfiguration.getValidationQuery();
    poolableConnectionFactory.setValidationQuery(validationQuery);

    // Creates the actual DataSource instance
    PoolingDataSource poolingDataSource = new PoolingDataSource(genericObjectPool);

    return poolingDataSource;

}

From source file:launcher.nub.multithread.Joiner.java

/**
 * Starts//ww  w .j a  va  2 s.c om
 */
protected void go() {
    GenericObjectPool pool = new GenericObjectPool(new JoinerFactory());
    pool.setMaxActive(12);
    // wait forever
    pool.setMaxWait(-1);

    // hacktastic ;o)
    for (int i = 2; i < 1542; i++) {
        logger.info("Queing up Id: " + i);
        Thread t = new Thread(new Runner(taxonConceptDAO, taxonomyUtils, i, pool));
        t.start();
    }
}

From source file:launcher.multithread.Extract.java

/**
 * Starts/*from www  .j  a v a 2s  .  c om*/
 */
protected void go() {
    GenericObjectPool pool = new GenericObjectPool(new ExtractorFactory());
    pool.setMaxActive(15);
    // wait forever
    pool.setMaxWait(-1);

    // hacktastic ;o)
    /*
    int[] rapIds = {218,67,92,26,497,374,1858,1039,429,716};      
    for(int i=0; i<rapIds.length; i++) {
       int rapId = rapIds[i];
    */
    for (int rapId = 0; rapId < 2000; rapId++) {

        // these have been done... 
        if (rapId == 218 || rapId == 67 || rapId == 92 || rapId == 26 || rapId == 497 || rapId == 374
                || rapId == 1858 || rapId == 1039 || rapId == 429 || rapId == 716) {
            continue;
        }

        logger.info("Queing up Id: " + rapId);
        SequenceProcessor workflow = (SequenceProcessor) context.getBean("GBIF:INDEX:1.0:extractOccurrence");
        Map<String, Object> seed = new HashMap<String, Object>();
        GregorianCalendar cal = new GregorianCalendar();
        cal.add(Calendar.MONTH, -6);
        seed.put("pageFrom", cal.getTime());
        seed.put("resourceAccessPointId", new Long(rapId));
        Thread t = new Thread(new Runner(seed, workflow, pool));
        t.start();
    }
}

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

/**
 * Constructor for BasicDataSource.//from w w  w  .  j  a va  2  s  .c o  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 {/* www  .  j  av  a  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);
    }// w w w  .  ja v a 2 s .  c o m

    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

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);
    pool.setMaxActive(threads);/*  w  w  w  .  ja  v a2 s  .  c o  m*/
    // 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);
}