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

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

Introduction

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

Prototype

public void setTestOnBorrow(boolean testOnBorrow) 

Source Link

Document

When true, objects will be PoolableObjectFactory#validateObject validated before being returned by the #borrowObject method.

Usage

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

/**
 * register the data source for H2 DB/* ww  w  .ja va 2s. 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//from w ww  . java 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  ww w  . ja  v a 2 s .c om
 *
 * @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//  w  w  w . ja  v a 2 s . co m
 */
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:com.foxelbox.foxbukkit.database.DatabaseConnectionPool.java

private DatabaseConnectionPool() {
    try {/*  www.  j  a v  a  2 s .  c  om*/
        Class.forName("com.mysql.jdbc.Driver");
    } catch (Exception e) {
        System.err.println("Error loading JBBC MySQL: " + e.toString());
    }

    GenericObjectPool connectionPool = new GenericObjectPool(null);
    connectionPool.setMaxActive(10);
    connectionPool.setMaxIdle(5);
    connectionPool.setTestOnBorrow(true);
    connectionPool.setTestOnReturn(true);
    connectionPool.setTestWhileIdle(true);

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
            FoxBukkit.instance.configuration.getValue("database-uri",
                    "jdbc:mysql://localhost:3306/foxbukkit_database"),
            FoxBukkit.instance.configuration.getValue("database-user", "root"),
            FoxBukkit.instance.configuration.getValue("database-password", "password"));
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, new StackKeyedObjectPoolFactory(), "SELECT 1", false, true);
    poolableConnectionFactory.setValidationQueryTimeout(3);

    dataSource = new PoolingDataSource(connectionPool);

    try {
        initialize();
    } catch (SQLException exc) {
        System.err.println("Error initializing MySQL Database");
        exc.printStackTrace();
    }
}

From source file:eu.peppol.jdbc.OxalisDataSourceFactoryDbcpImplTest.java

@Test
public void testHandleStaleConnections() throws Exception {
    ConnectionFactory driverConnectionFactory = createConnectionFactory(true);

    GenericObjectPool genericObjectPool = new GenericObjectPool(null);
    genericObjectPool.setMaxActive(1);// w ww .  j a v a2s.  c  o m

    genericObjectPool.setTestOnBorrow(true);
    /*
            genericObjectPool.setTestWhileIdle(true);   // Test idle instances visited by the pool maintenance thread and destroy any that fail validation
            genericObjectPool.setTimeBetweenEvictionRunsMillis(10000);      // Test every 10 seconds
            genericObjectPool.setMaxWait(10000);
    */

    PoolingDataSource poolingDataSource = createPoolingDataSource(driverConnectionFactory, genericObjectPool);
    runTwoSqlStatementsWithTwoConnections(poolingDataSource);
}

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

/**
 * Constructor for BasicDataSource./*w w  w.  j a va2  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.adaptris.jdbc.connection.FailoverDatasourceTest.java

@Test(expected = SQLException.class)
public void testBorrow() throws Exception {
    GenericObjectPool objPool = new GenericObjectPool(new UselessLifeguard(config(), true, false),
            maxPoolSize(), GenericObjectPool.WHEN_EXHAUSTED_BLOCK, timeToWait());
    objPool.setTestOnBorrow(true);
    objPool.setTestWhileIdle(true);//w  w w . j  a v  a  2 s .co m
    overrideObjectPool(objPool);
    getConnection();
}

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);
    }//from   ww w. j  ava  2 s  .co 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:com.nesscomputing.syslog4j.impl.pool.generic.GenericSyslogPoolFactory.java

protected void configureGenericObjectPool(GenericObjectPool<AbstractSyslogWriter> genericObjectPool)
        throws SyslogRuntimeException {
    SyslogPoolConfigIF poolConfig = null;

    try {//w  ww .ja v a2s. co 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());
}