Example usage for org.apache.commons.pool.impl GenericKeyedObjectPool DEFAULT_MAX_TOTAL

List of usage examples for org.apache.commons.pool.impl GenericKeyedObjectPool DEFAULT_MAX_TOTAL

Introduction

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

Prototype

int DEFAULT_MAX_TOTAL

To view the source code for org.apache.commons.pool.impl GenericKeyedObjectPool DEFAULT_MAX_TOTAL.

Click Source Link

Document

The default cap on the the overall maximum number of objects that can exist at one time.

Usage

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

/**
 * Constructor for BasicDataSource.//ww w.  j  a va 2s . 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:net.ontopia.persistence.proxy.DBCPConnectionFactory.java

protected void initPool() {
    // Set up connection pool
    pool = new GenericObjectPool(null);

    // Read/Write by default
    boolean readonly = defaultReadOnly;
    // Auto-commit disabled by default
    boolean autocommit = readonly;
    log.debug("Creating new DBCP connection factory, readonly=" + readonly + ", autocommit=" + autocommit);

    // Set minimum pool size (default: 20)
    String _minsize = PropertyUtils.getProperty(properties,
            "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.MinimumSize", false);
    int minsize = (_minsize == null ? 20 : Integer.parseInt(_minsize));
    log.debug("Setting ConnectionPool.MinimumSize '" + minsize + "'");
    pool.setMaxIdle(minsize); // 0 = no limit

    // Set maximum pool size (default: Integer.MAX_VALUE)
    String _maxsize = PropertyUtils.getProperty(properties,
            "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.MaximumSize", false);
    int maxsize = (_maxsize == null ? 0 : Integer.parseInt(_maxsize));
    log.debug("Setting ConnectionPool.MaximumSize '" + maxsize + "'");
    pool.setMaxActive(maxsize); // 0 = no limit

    // Set user timeout (default: never)
    String _utimeout = PropertyUtils.getProperty(properties,
            "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.UserTimeout", false);
    int utimeout = (_utimeout == null ? -1 : Integer.parseInt(_utimeout));
    pool.setMaxWait(utimeout); // -1 = never

    // Set soft maximum - emergency objects (default: true)
    boolean softmax = PropertyUtils.isTrue(properties,
            "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.SoftMaximum", true);
    log.debug("Setting ConnectionPool.SoftMaximum '" + softmax + "'");
    if (softmax)/*from   w w  w  .  j  a  v a  2  s .c  om*/
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    else
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);

    // allow the user to overwrite exhausted options
    // warning: when set to fail, make sure Maximum and Minimum are set correctly
    // warning: when set to block, make sure a propper usertimeout is set, or pool will block
    //          forever
    String _whenExhaustedAction = PropertyUtils.getProperty(properties,
            "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.WhenExhaustedAction", false);
    if (EXHAUSED_BLOCK.equals(_whenExhaustedAction))
        pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK);
    if (EXHAUSED_GROW.equals(_whenExhaustedAction))
        pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW);
    if (EXHAUSED_FAIL.equals(_whenExhaustedAction))
        pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);

    if (pool.getWhenExhaustedAction() == GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK)
        log.debug("Pool is set to block on exhaused");
    if (pool.getWhenExhaustedAction() == GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW)
        log.debug("Pool is set to grow on exhaused");
    if (pool.getWhenExhaustedAction() == GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL)
        log.debug("Pool is set to fail on exhaused");

    // Statement pool
    GenericKeyedObjectPoolFactory stmpool = null;
    if (PropertyUtils.isTrue(properties, "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.PoolStatements",
            true)) {
        log.debug("Using prepared statement pool: Yes");
        stmpool = new GenericKeyedObjectPoolFactory(null, -1, // unlimited maxActive (per key)
                GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL, 0, // maxWait
                1, // maxIdle (per key) 
                GenericKeyedObjectPool.DEFAULT_MAX_TOTAL);
    } else {
        log.debug("Using prepared statement pool: No");
    }

    // Test on borrow
    pool.setTestOnBorrow(true);

    // Get validation query
    String vquery = PropertyUtils.getProperty(properties,
            "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.ValidationQuery", false);
    if (vquery == null)
        vquery = "select seq_count from TM_ADMIN_SEQUENCE where seq_name = '<GLOBAL>'";

    try {
        // Make sure driver is registered
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Class.forName(getDriver(), true, classLoader);
        // Create connection factory
        ConnectionFactory cfactory;
        if (getUserName() == null || getPassword() == null) {
            Properties props = new Properties();
            props.putAll(properties);
            cfactory = new DriverManagerConnectionFactory(getConnectionString(), props);
        } else {
            cfactory = new DriverManagerConnectionFactory(getConnectionString(), getUserName(), getPassword());
        }

        // Create data source
        this.pcfactory = new TraceablePoolableConnectionFactory(cfactory, pool, stmpool, vquery, readonly,
                autocommit);

        // Set default transaction isolation level
        pcfactory.setDefaultTransactionIsolation(defaultTransactionIsolation);

        this.datasource = new PoolingDataSource(pool);
    } catch (Exception e) {
        throw new OntopiaRuntimeException("Problems occurred when setting up DBCP connection pool.", e);
    }
}

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

public void testConstructors() {

    // Make constructor arguments all different from defaults
    int maxActive = 1;
    int maxIdle = 2;
    long maxWait = 3;
    int minIdle = 4;
    int maxTotal = 5;
    long minEvictableIdleTimeMillis = 6;
    int numTestsPerEvictionRun = 7;
    boolean testOnBorrow = true;
    boolean testOnReturn = true;
    boolean testWhileIdle = true;
    long timeBetweenEvictionRunsMillis = 8;
    byte whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
    boolean lifo = false;

    GenericKeyedObjectPool pool = new GenericKeyedObjectPool();
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_ACTIVE, pool.getMaxActive());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_IDLE, pool.getMaxIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_WAIT, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            pool.getMinEvictableIdleTimeMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, pool.getNumTestsPerEvictionRun());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW, pool.getTestOnBorrow());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN, pool.getTestOnReturn());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE, pool.getTestWhileIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
    config.lifo = lifo;/*from ww  w .  j a  v a 2 s .co  m*/
    config.maxActive = maxActive;
    config.maxIdle = maxIdle;
    config.minIdle = minIdle;
    config.maxTotal = maxTotal;
    config.maxWait = maxWait;
    config.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    config.numTestsPerEvictionRun = numTestsPerEvictionRun;
    config.testOnBorrow = testOnBorrow;
    config.testOnReturn = testOnReturn;
    config.testWhileIdle = testWhileIdle;
    config.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    config.whenExhaustedAction = whenExhaustedAction;
    pool = new GenericKeyedObjectPool(null, config);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(minIdle, pool.getMinIdle());
    assertEquals(maxTotal, pool.getMaxTotal());
    assertEquals(minEvictableIdleTimeMillis, pool.getMinEvictableIdleTimeMillis());
    assertEquals(numTestsPerEvictionRun, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(testWhileIdle, pool.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(lifo, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_IDLE, pool.getMaxIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_WAIT, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            pool.getMinEvictableIdleTimeMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, pool.getNumTestsPerEvictionRun());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW, pool.getTestOnBorrow());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN, pool.getTestOnReturn());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE, pool.getTestWhileIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_IDLE, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            pool.getMinEvictableIdleTimeMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, pool.getNumTestsPerEvictionRun());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW, pool.getTestOnBorrow());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN, pool.getTestOnReturn());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE, pool.getTestWhileIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, testOnBorrow,
            testOnReturn);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_IDLE, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            pool.getMinEvictableIdleTimeMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE, pool.getTestWhileIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, maxIdle);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            pool.getMinEvictableIdleTimeMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, pool.getNumTestsPerEvictionRun());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW, pool.getTestOnBorrow());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN, pool.getTestOnReturn());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE, pool.getTestWhileIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, maxIdle, testOnBorrow,
            testOnReturn);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            pool.getMinEvictableIdleTimeMillis());
    assertEquals(GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE, pool.getTestWhileIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, maxIdle, testOnBorrow,
            testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis,
            testWhileIdle);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, pool.getMaxTotal());
    assertEquals(minEvictableIdleTimeMillis, pool.getMinEvictableIdleTimeMillis());
    assertEquals(numTestsPerEvictionRun, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(testWhileIdle, pool.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, maxIdle, maxTotal,
            testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun,
            minEvictableIdleTimeMillis, testWhileIdle);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(GenericKeyedObjectPool.DEFAULT_MIN_IDLE, pool.getMinIdle());
    assertEquals(maxTotal, pool.getMaxTotal());
    assertEquals(minEvictableIdleTimeMillis, pool.getMinEvictableIdleTimeMillis());
    assertEquals(numTestsPerEvictionRun, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(testWhileIdle, pool.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, maxIdle, maxTotal, minIdle,
            testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun,
            minEvictableIdleTimeMillis, testWhileIdle);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(minIdle, pool.getMinIdle());
    assertEquals(maxTotal, pool.getMaxTotal());
    assertEquals(minEvictableIdleTimeMillis, pool.getMinEvictableIdleTimeMillis());
    assertEquals(numTestsPerEvictionRun, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(testWhileIdle, pool.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(GenericKeyedObjectPool.DEFAULT_LIFO, pool.getLifo());

    pool = new GenericKeyedObjectPool(null, maxActive, whenExhaustedAction, maxWait, maxIdle, maxTotal, minIdle,
            testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun,
            minEvictableIdleTimeMillis, testWhileIdle, lifo);
    assertEquals(maxActive, pool.getMaxActive());
    assertEquals(maxIdle, pool.getMaxIdle());
    assertEquals(maxWait, pool.getMaxWait());
    assertEquals(minIdle, pool.getMinIdle());
    assertEquals(maxTotal, pool.getMaxTotal());
    assertEquals(minEvictableIdleTimeMillis, pool.getMinEvictableIdleTimeMillis());
    assertEquals(numTestsPerEvictionRun, pool.getNumTestsPerEvictionRun());
    assertEquals(testOnBorrow, pool.getTestOnBorrow());
    assertEquals(testOnReturn, pool.getTestOnReturn());
    assertEquals(testWhileIdle, pool.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, pool.getTimeBetweenEvictionRunsMillis());
    assertEquals(whenExhaustedAction, pool.getWhenExhaustedAction());
    assertEquals(lifo, pool.getLifo());
}

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

protected KeyedObjectPoolFactory createStatementPoolFactory(JdbcConnectionDescriptor jcd) {
    final String platform = jcd.getDbms();
    if (platform.startsWith("Oracle9i")) {
        // mkalen: let the platform set Oracle-specific statement pooling
        return null;
    }/* w w  w.j  a  v a  2  s. c o m*/

    // Set up statement pool, if desired
    GenericKeyedObjectPoolFactory statementPoolFactory = null;
    final Properties properties = jcd.getConnectionPoolDescriptor().getDbcpProperties();
    final String poolStmtParam = properties.getProperty(PARAM_NAME_POOL_STATEMENTS);
    if (poolStmtParam != null && Boolean.valueOf(poolStmtParam).booleanValue()) {
        int maxOpenPreparedStatements = GenericKeyedObjectPool.DEFAULT_MAX_TOTAL;
        final String maxOpenPrepStmtString = properties.getProperty(PARAM_NAME_STATEMENT_POOL_MAX_TOTAL);
        if (maxOpenPrepStmtString != null) {
            maxOpenPreparedStatements = Integer.parseInt(maxOpenPrepStmtString);
        }
        // Use the same values as Commons DBCP BasicDataSource
        statementPoolFactory = new GenericKeyedObjectPoolFactory(null, -1, // unlimited maxActive (per key)
                GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL, 0, // maxWait
                1, // maxIdle (per key)
                maxOpenPreparedStatements);
    }
    return statementPoolFactory;
}

From source file:org.apache.synapse.commons.datasource.factory.DataSourceInformationFactory.java

/**
 * Factory method to create a DataSourceInformation instance based on given properties
 *
 * @param dsName     DataSource Name/*  w w w.j a v a 2s.co  m*/
 * @param properties Properties to create and configure DataSource
 * @return DataSourceInformation instance
 */
public static DataSourceInformation createDataSourceInformation(String dsName, Properties properties) {

    if (dsName == null || "".equals(dsName)) {
        if (log.isDebugEnabled()) {
            log.debug("DataSource name is either empty or null, ignoring..");
        }
        return null;
    }

    StringBuffer buffer = new StringBuffer();
    buffer.append(DataSourceConstants.PROP_SYNAPSE_PREFIX_DS);
    buffer.append(DataSourceConstants.DOT_STRING);
    buffer.append(dsName);
    buffer.append(DataSourceConstants.DOT_STRING);

    // Prefix for getting particular data source's properties
    String prefix = buffer.toString();

    String driver = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_DRIVER_CLS_NAME,
            null);
    if (driver == null) {
        handleException(prefix + DataSourceConstants.PROP_DRIVER_CLS_NAME + " cannot be found.");
    }

    String url = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_URL, null);
    if (url == null) {
        handleException(prefix + DataSourceConstants.PROP_URL + " cannot be found.");
    }

    DataSourceInformation datasourceInformation = new DataSourceInformation();
    datasourceInformation.setAlias(dsName);

    datasourceInformation.setDriver(driver);
    datasourceInformation.setUrl(url);

    String dataSourceName = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_DS_NAME,
            dsName, String.class);
    datasourceInformation.setDatasourceName(dataSourceName);

    String dsType = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_TYPE,
            DataSourceConstants.PROP_BASIC_DATA_SOURCE, String.class);

    datasourceInformation.setType(dsType);

    String repositoryType = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_REGISTRY, DataSourceConstants.PROP_REGISTRY_MEMORY, String.class);

    datasourceInformation.setRepositoryType(repositoryType);

    Integer maxActive = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_MAX_ACTIVE,
            GenericObjectPool.DEFAULT_MAX_ACTIVE, Integer.class);
    datasourceInformation.setMaxActive(maxActive);

    Integer maxIdle = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_MAX_IDLE,
            GenericObjectPool.DEFAULT_MAX_IDLE, Integer.class);
    datasourceInformation.setMaxIdle(maxIdle);

    Long maxWait = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_MAX_WAIT,
            GenericObjectPool.DEFAULT_MAX_WAIT, Long.class);

    datasourceInformation.setMaxWait(maxWait);

    // Construct DriverAdapterCPDS reference
    String suffix = DataSourceConstants.PROP_CPDS_ADAPTER + DataSourceConstants.DOT_STRING
            + DataSourceConstants.PROP_CLASS_NAME;
    String className = MiscellaneousUtil.getProperty(properties, prefix + suffix,
            DataSourceConstants.PROP_CPDS_ADAPTER_DRIVER);
    datasourceInformation.addParameter(suffix, className);
    suffix = DataSourceConstants.PROP_CPDS_ADAPTER + DataSourceConstants.DOT_STRING
            + DataSourceConstants.PROP_FACTORY;
    String factory = MiscellaneousUtil.getProperty(properties, prefix + suffix,
            DataSourceConstants.PROP_CPDS_ADAPTER_DRIVER);
    datasourceInformation.addParameter(suffix, factory);
    suffix = DataSourceConstants.PROP_CPDS_ADAPTER + DataSourceConstants.DOT_STRING
            + DataSourceConstants.PROP_NAME;
    String name = MiscellaneousUtil.getProperty(properties, prefix + suffix, "cpds");
    datasourceInformation.addParameter(suffix, name);

    boolean defaultAutoCommit = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_DEFAULT_AUTO_COMMIT, true, Boolean.class);

    boolean defaultReadOnly = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_DEFAULT_READ_ONLY, false, Boolean.class);

    boolean testOnBorrow = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_TEST_ON_BORROW, true, Boolean.class);

    boolean testOnReturn = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_TEST_ON_RETURN, false, Boolean.class);

    long timeBetweenEvictionRunsMillis = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS, Long.class);

    int numTestsPerEvictionRun = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_NUM_TESTS_PER_EVICTION_RUN,
            GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, Integer.class);

    long minEvictableIdleTimeMillis = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, Long.class);

    boolean testWhileIdle = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_TEST_WHILE_IDLE, false, Boolean.class);

    String validationQuery = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_VALIDATION_QUERY, null);

    int minIdle = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_MIN_IDLE,
            GenericObjectPool.DEFAULT_MIN_IDLE, Integer.class);

    int initialSize = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_INITIAL_SIZE,
            0, Integer.class);

    int defaultTransactionIsolation = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_DEFAULT_TRANSACTION_ISOLATION, -1, Integer.class);

    String defaultCatalog = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_DEFAULT_CATALOG, null);

    boolean accessToUnderlyingConnectionAllowed = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED, false, Boolean.class);

    boolean removeAbandoned = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_REMOVE_ABANDONED, false, Boolean.class);

    int removeAbandonedTimeout = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_REMOVE_ABANDONED_TIMEOUT, 300, Integer.class);

    boolean logAbandoned = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_LOG_ABANDONED, false, Boolean.class);

    boolean poolPreparedStatements = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_POOL_PREPARED_STATEMENTS, false, Boolean.class);

    int maxOpenPreparedStatements = MiscellaneousUtil.getProperty(properties,
            prefix + DataSourceConstants.PROP_MAX_OPEN_PREPARED_STATEMENTS,
            GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, Integer.class);

    datasourceInformation.setDefaultAutoCommit(defaultAutoCommit);
    datasourceInformation.setDefaultReadOnly(defaultReadOnly);
    datasourceInformation.setTestOnBorrow(testOnBorrow);
    datasourceInformation.setTestOnReturn(testOnReturn);
    datasourceInformation.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    datasourceInformation.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    datasourceInformation.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    datasourceInformation.setTestWhileIdle(testWhileIdle);
    datasourceInformation.setMinIdle(minIdle);
    datasourceInformation.setDefaultTransactionIsolation(defaultTransactionIsolation);
    datasourceInformation.setAccessToUnderlyingConnectionAllowed(accessToUnderlyingConnectionAllowed);
    datasourceInformation.setRemoveAbandoned(removeAbandoned);
    datasourceInformation.setRemoveAbandonedTimeout(removeAbandonedTimeout);
    datasourceInformation.setLogAbandoned(logAbandoned);
    datasourceInformation.setPoolPreparedStatements(poolPreparedStatements);
    datasourceInformation.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
    datasourceInformation.setInitialSize(initialSize);

    if (validationQuery != null && !"".equals(validationQuery)) {
        datasourceInformation.setValidationQuery(validationQuery);
    }

    if (defaultCatalog != null && !"".equals(defaultCatalog)) {
        datasourceInformation.setDefaultCatalog(defaultCatalog);
    }

    datasourceInformation.addProperty(prefix + DataSourceConstants.PROP_IC_FACTORY,
            MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_IC_FACTORY, null));
    //Provider URL
    datasourceInformation.addProperty(prefix + DataSourceConstants.PROP_PROVIDER_URL,
            MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_PROVIDER_URL, null));

    datasourceInformation.addProperty(prefix + DataSourceConstants.PROP_PROVIDER_PORT,
            MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_PROVIDER_PORT, null));

    String passwordPrompt = MiscellaneousUtil.getProperty(properties,
            prefix + SecurityConstants.PROP_PASSWORD_PROMPT, "Password for datasource " + dsName, String.class);

    SecretInformation secretInformation = SecretInformationFactory.createSecretInformation(properties, prefix,
            passwordPrompt);
    secretInformation.setToken(dsName + "." + SecurityConstants.PROP_PASSWORD);
    datasourceInformation.setSecretInformation(secretInformation);

    return datasourceInformation;
}

From source file:org.apache.synapse.util.DataSourceRegistrar.java

/**
 * Helper method to set all BasicDataSource specific parameter
 *
 * @param reference  The naming reference instance
 * @param properties The properties which contains required parameter value
 * @param prefix     The key prefix for which is used to get data from given properties
 *///  w ww  .j a  va  2 s.c  o  m
private static void setBasicDataSourceParameters(Reference reference, Properties properties, String prefix) {
    String minIdle = getProperty(properties, prefix + PROP_MINIDLE,
            String.valueOf(GenericObjectPool.DEFAULT_MIN_IDLE));
    String initialSize = getProperty(properties, prefix + PROP_INITIALSIZE, String.valueOf(0));
    String defaultTransactionIsolation = getProperty(properties, prefix + PROP_DEFAULTTRANSACTIONISOLATION,
            null);
    String defaultCatalog = getProperty(properties, prefix + PROP_DEFAULTCATALOG, null);
    String accessToUnderlyingConnectionAllowed = getProperty(properties,
            prefix + PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED, String.valueOf(false));
    String removeAbandoned = getProperty(properties, prefix + PROP_REMOVEABANDONED, String.valueOf(false));
    String removeAbandonedTimeout = getProperty(properties, prefix + PROP_REMOVEABANDONEDTIMEOUT,
            String.valueOf(300));
    String logAbandoned = getProperty(properties, prefix + PROP_LOGABANDONED, String.valueOf(false));
    String poolPreparedStatements = getProperty(properties, prefix + PROP_POOLPREPAREDSTATEMENTS,
            String.valueOf(false));
    String maxOpenPreparedStatements = getProperty(properties, prefix + PROP_MAXOPENPREPAREDSTATEMENTS,
            String.valueOf(GenericKeyedObjectPool.DEFAULT_MAX_TOTAL));

    reference.add(new StringRefAddr(PROP_MINIDLE, minIdle));
    if (defaultTransactionIsolation != null && !"".equals(defaultTransactionIsolation)) {
        reference.add(new StringRefAddr(PROP_DEFAULTTRANSACTIONISOLATION, defaultTransactionIsolation));
    }
    reference.add(
            new StringRefAddr(PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED, accessToUnderlyingConnectionAllowed));
    reference.add(new StringRefAddr(PROP_REMOVEABANDONED, removeAbandoned));
    reference.add(new StringRefAddr(PROP_REMOVEABANDONEDTIMEOUT, removeAbandonedTimeout));
    reference.add(new StringRefAddr(PROP_LOGABANDONED, logAbandoned));
    reference.add(new StringRefAddr(PROP_POOLPREPAREDSTATEMENTS, poolPreparedStatements));
    reference.add(new StringRefAddr(PROP_MAXOPENPREPAREDSTATEMENTS, maxOpenPreparedStatements));
    reference.add(new StringRefAddr(PROP_INITIALSIZE, initialSize));
    if (defaultCatalog != null && !"".equals(defaultCatalog)) {
        reference.add(new StringRefAddr(PROP_DEFAULTCATALOG, defaultCatalog));
    }
}