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

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

Introduction

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

Prototype

public GenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait,
        int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis,
        int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) 

Source Link

Document

Create a new GenericObjectPool using the specified values.

Usage

From source file:gov.lanl.util.DBCPUtils.java

/**
 * Set-up a DBCP DataSource from core connection properties.
 * @param connectURI jdbc connection uri
 * @param jdbcDriverName qualified classpath to jdbc driver for database
 * @param username database user account
 * @param password database password/*from  w w  w .j av  a 2 s .  com*/
 * @param maxActive max simultaneous db connections (default: 50)
 * @param maxIdle max idle db connections (default: 10)
 */
public static DataSource setupDataSource(String connectURI, String jdbcDriverName, String username,
        String password, int maxActive, int maxIdle) throws Exception {
    try {
        java.lang.Class.forName(jdbcDriverName).newInstance();
    } catch (Exception e) {
        log.error(
                "Error when attempting to obtain DB Driver: " + jdbcDriverName + " on " + new Date().toString(),
                e);
        throw new ResolverException(e);
    }

    if (maxActive <= 0)
        maxActive = 50;
    if (maxIdle <= 0)
        maxIdle = 10;

    GenericObjectPool connectionPool = new GenericObjectPool(null, // PoolableObjectFactory, can be null
            maxActive, // max active
            GenericObjectPool.WHEN_EXHAUSTED_BLOCK, // action when exhausted
            3000, // max wait (milli seconds)
            maxIdle, // max idle
            false, // test on borrow
            false, // test on return
            60000, // time between eviction runs (millis)
            5, // number to test on eviction run
            30000, // min evictable idle time (millis)
            true // test while idle
    );

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username, password);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);

    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

    return dataSource;
}

From source file:mondrian.rolap.RolapConnectionPool.java

/**
 * Gets or creates a connection pool for a particular connect
 * specification.//from  w ww  .  j av  a2 s  .c  o m
 */
private synchronized ObjectPool getPool(Object key, ConnectionFactory connectionFactory) {
    ObjectPool connectionPool = mapConnectKeyToPool.get(key);
    if (connectionPool == null) {
        // use GenericObjectPool, which provides for resource limits
        connectionPool = new GenericObjectPool(null, // PoolableObjectFactory, can be null
                50, // max active
                GenericObjectPool.WHEN_EXHAUSTED_BLOCK, // action when exhausted
                3000, // max wait (milli seconds)
                10, // max idle
                false, // test on borrow
                false, // test on return
                60000, // time between eviction runs (millis)
                5, // number to test on eviction run
                30000, // min evictable idle time (millis)
                true); // test while idle

        // create a PoolableConnectionFactory
        AbandonedConfig abandonedConfig = new AbandonedConfig();
        // flag to remove abandoned connections from pool
        abandonedConfig.setRemoveAbandoned(true);
        // timeout (seconds) before removing abandoned connections
        abandonedConfig.setRemoveAbandonedTimeout(300);
        // Flag to log stack traces for application code which abandoned a
        // Statement or Connection
        abandonedConfig.setLogAbandoned(true);
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
                // the connection factory
                connectionFactory,
                // the object pool
                connectionPool,
                // statement pool factory for pooling prepared statements,
                // or null for no pooling
                null,
                // validation query (must return at least 1 row e.g. Oracle:
                // select count(*) from dual) to test connection, can be
                // null
                null,
                // default "read only" setting for borrowed connections
                false,
                // default "auto commit" setting for returned connections
                true,
                // AbandonedConfig object configures how to handle abandoned
                // connections
                abandonedConfig);

        // "poolableConnectionFactory" has registered itself with
        // "connectionPool", somehow, so we don't need the value any more.
        Util.discard(poolableConnectionFactory);
        mapConnectKeyToPool.put(key, connectionPool);
    }
    return connectionPool;
}

From source file:net.agmodel.metbroker.driver.impl.JmaMsmJp.java

/** 
 * set up JDBC parameter.//from  w ww .  j  av  a2s.c  om
 * This driver uses postgresql's JDBC driver.
 * (non-Javadoc)
 * @see net.agmodel.metbroker.driver.MetDriver#init(net.agmodel.metbroker.driver.DriverConfig)
 */
public void init(DriverConfig driverConfig) throws DriverException {
    logger.debug("initialize JMA MSM JP data source");
    try {
        Class.forName("org.postgresql.Driver");

        int maxActive = Integer.parseInt(driverConfig.getParam("maxactive"));
        int maxWait = Integer.parseInt(driverConfig.getParam("maxwait"));
        int maxIdle = Integer.parseInt(driverConfig.getParam("maxidle"));
        String user = driverConfig.getParam("user");
        String password = driverConfig.getParam("password");
        String host = driverConfig.getParam("host");
        int port = Integer.parseInt(driverConfig.getParam("port"));
        String db = driverConfig.getParam("db");

        ObjectPool connectionPool = new GenericObjectPool(null, maxActive,
                GenericObjectPool.WHEN_EXHAUSTED_BLOCK, maxWait, maxIdle, false, false, 60000, 3, 10000, false);

        Properties p = new Properties();
        p.put("user", user);
        p.put("password", password);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
                "jdbc:postgresql://" + host + ":" + port + "/" + db, p);
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, null, false, true);
        dataSource = new PoolingDataSource(connectionPool);

        logger.debug("JMA MSM JP DataSource created");
    } catch (Exception e) {
        e.printStackTrace();
        throw new DriverException(e.getMessage());
    }
}

From source file:net.agmodel.metbroker.driver.impl.JmaGsmJp.java

/** 
 * set up JDBC parameter.//from  ww  w  .j  a  v a 2 s  .  co  m
 * This driver uses postgresql's JDBC driver.
 * 
 * (non-Javadoc)
 * @see net.agmodel.metbroker.driver.MetDriver#init(net.agmodel.metbroker.driver.DriverConfig)
 */
public void init(DriverConfig driverConfig) throws DriverException {
    logger.debug("initialize JMA GSM JP data source");
    try {
        Class.forName("org.postgresql.Driver");

        int maxActive = Integer.parseInt(driverConfig.getParam("maxactive"));
        int maxWait = Integer.parseInt(driverConfig.getParam("maxwait"));
        int maxIdle = Integer.parseInt(driverConfig.getParam("maxidle"));
        String user = driverConfig.getParam("user");
        String password = driverConfig.getParam("password");
        String host = driverConfig.getParam("host");
        int port = Integer.parseInt(driverConfig.getParam("port"));
        String db = driverConfig.getParam("db");

        ObjectPool connectionPool = new GenericObjectPool(null, maxActive,
                GenericObjectPool.WHEN_EXHAUSTED_BLOCK, maxWait, maxIdle, false, false, 60000, 3, 10000, false);

        Properties p = new Properties();
        p.put("user", user);
        p.put("password", password);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
                "jdbc:postgresql://" + host + ":" + port + "/" + db, p);
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, null, false, true);
        dataSource = new PoolingDataSource(connectionPool);

        logger.debug("JMA GSM JP DataSource created");
    } catch (Exception e) {
        e.printStackTrace();
        throw new DriverException(e.getMessage());
    }
}

From source file:net.agmodel.metbroker.driver.impl.JmaGsmGl.java

/** 
 * <PRE>//from  w ww . j a  va  2s  . com
 * set up JDBC parameter.
 * This driver uses postgresql's JDBC driver.
 * </PRE>
 * (non-Javadoc)
 * @see net.agmodel.metbroker.driver.MetDriver#init(net.agmodel.metbroker.driver.DriverConfig)
 */
public void init(DriverConfig driverConfig) throws DriverException {
    logger.debug("initialize JMA GSM GLobal data source");
    try {
        Class.forName("org.postgresql.Driver");

        int maxActive = Integer.parseInt(driverConfig.getParam("maxactive"));
        int maxWait = Integer.parseInt(driverConfig.getParam("maxwait"));
        int maxIdle = Integer.parseInt(driverConfig.getParam("maxidle"));
        String user = driverConfig.getParam("user");
        String password = driverConfig.getParam("password");
        String host = driverConfig.getParam("host");
        int port = Integer.parseInt(driverConfig.getParam("port"));
        String db = driverConfig.getParam("db");

        ObjectPool connectionPool = new GenericObjectPool(null, maxActive,
                GenericObjectPool.WHEN_EXHAUSTED_BLOCK, maxWait, maxIdle, false, false, 60000, 3, 10000, false);

        Properties p = new Properties();
        p.put("user", user);
        p.put("password", password);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
                "jdbc:postgresql://" + host + ":" + port + "/" + db, p);
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, null, false, true);
        dataSource = new PoolingDataSource(connectionPool);

        logger.debug("JMA GSM Global DataSource created");
    } catch (Exception e) {
        e.printStackTrace();
        throw new DriverException(e.getMessage());
    }
}

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

public void testInvalidWhenExhaustedAction() throws Exception {
    try {/*w  ww  .j av  a2 s.c  o  m*/
        pool.setWhenExhaustedAction(Byte.MAX_VALUE);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        // expected
    }

    try {
        ObjectPool pool = new GenericObjectPool(new SimpleFactory(), GenericObjectPool.DEFAULT_MAX_ACTIVE,
                Byte.MAX_VALUE, GenericObjectPool.DEFAULT_MAX_WAIT, GenericObjectPool.DEFAULT_MAX_IDLE, false,
                false, GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
                GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,
                GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, false);
        assertNotNull(pool);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        // expected
    }
}

From source file:com.cloud.utils.db.TransactionLegacy.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void initDataSource(Properties dbProps) {
    try {// w  ww .j a v  a  2s  .  com
        if (dbProps.size() == 0)
            return;

        s_dbHAEnabled = Boolean.valueOf(dbProps.getProperty("db.ha.enabled"));
        s_logger.info("Is Data Base High Availiability enabled? Ans : " + s_dbHAEnabled);
        String loadBalanceStrategy = dbProps.getProperty("db.ha.loadBalanceStrategy");
        // FIXME:  If params are missing...default them????
        final int cloudMaxActive = Integer.parseInt(dbProps.getProperty("db.cloud.maxActive"));
        final int cloudMaxIdle = Integer.parseInt(dbProps.getProperty("db.cloud.maxIdle"));
        final long cloudMaxWait = Long.parseLong(dbProps.getProperty("db.cloud.maxWait"));
        final String cloudUsername = dbProps.getProperty("db.cloud.username");
        final String cloudPassword = dbProps.getProperty("db.cloud.password");
        final String cloudHost = dbProps.getProperty("db.cloud.host");
        final String cloudDriver = dbProps.getProperty("db.cloud.driver");
        final int cloudPort = Integer.parseInt(dbProps.getProperty("db.cloud.port"));
        final String cloudDbName = dbProps.getProperty("db.cloud.name");
        final boolean cloudAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.cloud.autoReconnect"));
        final String cloudValidationQuery = dbProps.getProperty("db.cloud.validationQuery");
        final String cloudIsolationLevel = dbProps.getProperty("db.cloud.isolation.level");

        int isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
        if (cloudIsolationLevel == null) {
            isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
        } else if (cloudIsolationLevel.equalsIgnoreCase("readcommitted")) {
            isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
        } else if (cloudIsolationLevel.equalsIgnoreCase("repeatableread")) {
            isolationLevel = Connection.TRANSACTION_REPEATABLE_READ;
        } else if (cloudIsolationLevel.equalsIgnoreCase("serializable")) {
            isolationLevel = Connection.TRANSACTION_SERIALIZABLE;
        } else if (cloudIsolationLevel.equalsIgnoreCase("readuncommitted")) {
            isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
        } else {
            s_logger.warn("Unknown isolation level " + cloudIsolationLevel + ".  Using read uncommitted");
        }

        final boolean cloudTestOnBorrow = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testOnBorrow"));
        final boolean cloudTestWhileIdle = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testWhileIdle"));
        final long cloudTimeBtwEvictionRunsMillis = Long
                .parseLong(dbProps.getProperty("db.cloud.timeBetweenEvictionRunsMillis"));
        final long cloudMinEvcitableIdleTimeMillis = Long
                .parseLong(dbProps.getProperty("db.cloud.minEvictableIdleTimeMillis"));
        final boolean cloudPoolPreparedStatements = Boolean
                .parseBoolean(dbProps.getProperty("db.cloud.poolPreparedStatements"));
        final String url = dbProps.getProperty("db.cloud.url.params");

        String cloudDbHAParams = null;
        String cloudSlaves = null;
        if (s_dbHAEnabled) {
            cloudDbHAParams = getDBHAParams("cloud", dbProps);
            cloudSlaves = dbProps.getProperty("db.cloud.slaves");
            s_logger.info("The slaves configured for Cloud Data base is/are : " + cloudSlaves);
        }

        final boolean useSSL = Boolean.parseBoolean(dbProps.getProperty("db.cloud.useSSL"));
        if (useSSL) {
            System.setProperty("javax.net.ssl.keyStore", dbProps.getProperty("db.cloud.keyStore"));
            System.setProperty("javax.net.ssl.keyStorePassword",
                    dbProps.getProperty("db.cloud.keyStorePassword"));
            System.setProperty("javax.net.ssl.trustStore", dbProps.getProperty("db.cloud.trustStore"));
            System.setProperty("javax.net.ssl.trustStorePassword",
                    dbProps.getProperty("db.cloud.trustStorePassword"));
        }

        final GenericObjectPool cloudConnectionPool = new GenericObjectPool(null, cloudMaxActive,
                GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, cloudMaxWait, cloudMaxIdle, cloudTestOnBorrow,
                false, cloudTimeBtwEvictionRunsMillis, 1, cloudMinEvcitableIdleTimeMillis, cloudTestWhileIdle);

        final String cloudConnectionUri = cloudDriver + "://" + cloudHost
                + (s_dbHAEnabled ? "," + cloudSlaves : "") + ":" + cloudPort + "/" + cloudDbName
                + "?autoReconnect=" + cloudAutoReconnect + (url != null ? "&" + url : "")
                + (useSSL ? "&useSSL=true" : "") + (s_dbHAEnabled ? "&" + cloudDbHAParams : "")
                + (s_dbHAEnabled ? "&loadBalanceStrategy=" + loadBalanceStrategy : "");
        DriverLoader.loadDriver(cloudDriver);

        final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory(cloudConnectionUri,
                cloudUsername, cloudPassword);

        final KeyedObjectPoolFactory poolableObjFactory = (cloudPoolPreparedStatements
                ? new StackKeyedObjectPoolFactory()
                : null);

        final PoolableConnectionFactory cloudPoolableConnectionFactory = new PoolableConnectionFactory(
                cloudConnectionFactory, cloudConnectionPool, poolableObjFactory, cloudValidationQuery, false,
                false, isolationLevel);

        // Default Data Source for CloudStack
        s_ds = new PoolingDataSource(cloudPoolableConnectionFactory.getPool());

        // Configure the usage db
        final int usageMaxActive = Integer.parseInt(dbProps.getProperty("db.usage.maxActive"));
        final int usageMaxIdle = Integer.parseInt(dbProps.getProperty("db.usage.maxIdle"));
        final long usageMaxWait = Long.parseLong(dbProps.getProperty("db.usage.maxWait"));
        final String usageUsername = dbProps.getProperty("db.usage.username");
        final String usagePassword = dbProps.getProperty("db.usage.password");
        final String usageHost = dbProps.getProperty("db.usage.host");
        final String usageDriver = dbProps.getProperty("db.usage.driver");
        final int usagePort = Integer.parseInt(dbProps.getProperty("db.usage.port"));
        final String usageDbName = dbProps.getProperty("db.usage.name");
        final boolean usageAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.usage.autoReconnect"));
        final String usageUrl = dbProps.getProperty("db.usage.url.params");

        final GenericObjectPool usageConnectionPool = new GenericObjectPool(null, usageMaxActive,
                GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, usageMaxWait, usageMaxIdle);

        final String usageConnectionUri = usageDriver + "://" + usageHost
                + (s_dbHAEnabled ? "," + dbProps.getProperty("db.cloud.slaves") : "") + ":" + usagePort + "/"
                + usageDbName + "?autoReconnect=" + usageAutoReconnect
                + (usageUrl != null ? "&" + usageUrl : "")
                + (s_dbHAEnabled ? "&" + getDBHAParams("usage", dbProps) : "")
                + (s_dbHAEnabled ? "&loadBalanceStrategy=" + loadBalanceStrategy : "");
        DriverLoader.loadDriver(usageDriver);

        final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory(usageConnectionUri,
                usageUsername, usagePassword);

        final PoolableConnectionFactory usagePoolableConnectionFactory = new PoolableConnectionFactory(
                usageConnectionFactory, usageConnectionPool, new StackKeyedObjectPoolFactory(), null, false,
                false);

        // Data Source for usage server
        s_usageDS = new PoolingDataSource(usagePoolableConnectionFactory.getPool());

        try {
            // Configure the simulator db
            final int simulatorMaxActive = Integer.parseInt(dbProps.getProperty("db.simulator.maxActive"));
            final int simulatorMaxIdle = Integer.parseInt(dbProps.getProperty("db.simulator.maxIdle"));
            final long simulatorMaxWait = Long.parseLong(dbProps.getProperty("db.simulator.maxWait"));
            final String simulatorUsername = dbProps.getProperty("db.simulator.username");
            final String simulatorPassword = dbProps.getProperty("db.simulator.password");
            final String simulatorHost = dbProps.getProperty("db.simulator.host");
            final String simulatorDriver = dbProps.getProperty("db.simulator.driver");
            final int simulatorPort = Integer.parseInt(dbProps.getProperty("db.simulator.port"));
            final String simulatorDbName = dbProps.getProperty("db.simulator.name");
            final boolean simulatorAutoReconnect = Boolean
                    .parseBoolean(dbProps.getProperty("db.simulator.autoReconnect"));

            final GenericObjectPool simulatorConnectionPool = new GenericObjectPool(null, simulatorMaxActive,
                    GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, simulatorMaxWait, simulatorMaxIdle);

            final String simulatorConnectionUri = simulatorDriver + "://" + simulatorHost + ":" + simulatorPort
                    + "/" + simulatorDbName + "?autoReconnect=" + simulatorAutoReconnect;
            DriverLoader.loadDriver(simulatorDriver);

            final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory(
                    simulatorConnectionUri, simulatorUsername, simulatorPassword);

            final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory(
                    simulatorConnectionFactory, simulatorConnectionPool, new StackKeyedObjectPoolFactory(),
                    null, false, false);
            s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool());
        } catch (Exception e) {
            s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS");
        }
    } catch (final Exception e) {
        s_ds = getDefaultDataSource("cloud");
        s_usageDS = getDefaultDataSource("cloud_usage");
        s_simulatorDS = getDefaultDataSource("cloud_simulator");
        s_logger.warn(
                "Unable to load db configuration, using defaults with 5 connections. Falling back on assumed datasource on localhost:3306 using username:password=cloud:cloud. Please check your configuration",
                e);
    }
}

From source file:org.sakaiproject.nakamura.lite.storage.AbstractClientConnectionPool.java

@Activate
public void activate(Map<String, Object> properties) throws ClassNotFoundException {
    // for testing purposes
    if (configuration == null) {
        configuration = (Configuration) properties.get(Configuration.class.getName());
    }/* w  w  w . j  a v  a2 s .  co  m*/
    indexColums = ImmutableSet.of(configuration.getIndexColumnNames());
    int maxActive = StorageClientUtils.getSetting(properties.get(MAX_ACTIVE), 200);
    byte whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION;
    String whenExhausted = (String) properties.get(WHEN_EHAUSTED);
    if ("fail".equals(whenExhausted)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
    } else if ("grow".equals(whenExhausted)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
    } else if ("block".equals(whenExhausted)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
    }
    long maxWait = StorageClientUtils.getSetting(properties.get(MAX_WAIT), 10L);
    int maxIdle = StorageClientUtils.getSetting(properties.get(MAX_IDLE), 5);
    boolean testOnBorrow = StorageClientUtils.getSetting(properties.get(TEST_ON_BORROW), true);
    boolean testOnReturn = StorageClientUtils.getSetting(properties.get(TEST_ON_RETURN), true);
    long timeBetweenEvictionRunsMillis = StorageClientUtils
            .getSetting(properties.get(TIME_BETWEEN_EVICTION_RUNS_MILLIS), 60000L);
    int numTestsPerEvictionRun = StorageClientUtils.getSetting(properties.get(NUM_TESTS_PER_EVICTION_RUN),
            1000);
    long minEvictableIdleTimeMillis = StorageClientUtils
            .getSetting(properties.get(MIN_EVICTABLE_IDLE_TIME_MILLIS), 10000L);
    boolean testWhileIdle = StorageClientUtils.getSetting(properties.get(TEST_WHILE_IDLE), false);

    pool = new GenericObjectPool(getConnectionPoolFactory(), maxActive, whenExhaustedAction, maxWait, maxIdle,
            testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun,
            minEvictableIdleTimeMillis, testWhileIdle);

    // set the maximum size of a string, if this is not 0, strings over this size will become files.
    StringType.setLengthLimit(StorageClientUtils.getSetting(properties.get(LONG_STRING_SIZE), 0));

}

From source file:org.sakaiproject.nakamura.lite.storage.spi.AbstractClientConnectionPool.java

@Activate
public void activate(Map<String, Object> properties) throws ClassNotFoundException {
    // for testing purposes
    if (configuration == null) {
        configuration = (Configuration) properties.get(Configuration.class.getName());
    }/*from   www. j a v  a 2  s . co m*/
    indexColumns = ImmutableSet.copyOf(configuration.getIndexColumnNames());
    indexColumnsTypes = ImmutableSet.copyOf(configuration.getIndexColumnTypes());
    int maxActive = StorageClientUtils.getSetting(properties.get(MAX_ACTIVE), 200);
    byte whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION;
    String whenExhausted = (String) properties.get(WHEN_EHAUSTED);
    if ("fail".equals(whenExhausted)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
    } else if ("grow".equals(whenExhausted)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
    } else if ("block".equals(whenExhausted)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
    }
    long maxWait = StorageClientUtils.getSetting(properties.get(MAX_WAIT), 10L);
    int maxIdle = StorageClientUtils.getSetting(properties.get(MAX_IDLE), 5);
    boolean testOnBorrow = StorageClientUtils.getSetting(properties.get(TEST_ON_BORROW), true);
    boolean testOnReturn = StorageClientUtils.getSetting(properties.get(TEST_ON_RETURN), true);
    long timeBetweenEvictionRunsMillis = StorageClientUtils
            .getSetting(properties.get(TIME_BETWEEN_EVICTION_RUNS_MILLIS), 60000L);
    int numTestsPerEvictionRun = StorageClientUtils.getSetting(properties.get(NUM_TESTS_PER_EVICTION_RUN),
            1000);
    long minEvictableIdleTimeMillis = StorageClientUtils
            .getSetting(properties.get(MIN_EVICTABLE_IDLE_TIME_MILLIS), 10000L);
    boolean testWhileIdle = StorageClientUtils.getSetting(properties.get(TEST_WHILE_IDLE), false);

    pool = new GenericObjectPool(getConnectionPoolFactory(), maxActive, whenExhaustedAction, maxWait, maxIdle,
            testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun,
            minEvictableIdleTimeMillis, testWhileIdle);

    // set the maximum size of a string, if this is not 0, strings over this size will become files.
    StringType.setLengthLimit(
            StorageClientUtils.getSetting(properties.get(LONG_STRING_SIZE), DEFAULT_LONG_STRING_SIZE));
    // location of the long string store.
    LongString.setBase(StorageClientUtils.getSetting(properties.get(LONG_STRING_STORE_BASE),
            StorageClientUtils.getSetting(properties.get(FS_STORE_BASE_DIR), DEFAULT_FILE_STORE)));

}

From source file:uk.ac.sanger.cgp.dbcon.pooling.Pool.java

/**
 * Creates the pool and the data source which provides the pooling ability
 *//*from   ww w .j  a va2 s .com*/
private void createPool() {
    PoolableObjectFactory pof = null;

    // Creation of the generic pool and linking a factory to it
    GenericObjectPool underlyingConnectionPool = new GenericObjectPool(pof, config.getMaxActive(),
            config.getExhausted(), config.getMaxWait(), config.getMaxIdle(), config.isTestOnBorrow(),
            config.isTestOnReturn(), config.getTimeBetweenEvictRun(), config.getNumTestsPerEvictionRun(),
            config.getMinEvictTime(), config.isTestWhileIdle());

    setUnderlyingConnectionPool(underlyingConnectionPool);

    // This section allows for PreparedStatements to be used
    GenericKeyedObjectPoolFactory kopf = null;

    if (config.getCachedPreparedStatements() != 0) {
        kopf = new GenericKeyedObjectPoolFactory(null, -1, // unlimited maxActive
                // (per key)
                GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL, 0, // maxWait
                1, // maxIdle (per key)
                config.getCachedPreparedStatements());
    }

    // Creating the correct connection factory
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(config.getWorkingUrl(),
            config.getUsername(), config.getPassword());

    // Final construction of the poolable connection factory from:
    // PoolableConnectionFactory(ConnectionFactory connFactory, ObjectPool pool,
    // KeyedObjectPoolFactory stmtPoolFactory, String validationQuery,
    // boolean defaultReadOnly, boolean defaultAutoCommit)

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            getUnderlyingConnectionPool(), kopf, null, false, false);

    // Setting the validation query
    poolableConnectionFactory.setValidationQuery(config.getValidationQuery());

    // Creating the pooled datasource
    setDataSource(new PoolingDataSource(getUnderlyingConnectionPool()));
}