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

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

Introduction

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

Prototype

public synchronized void setMaxActive(int maxActive) 

Source Link

Document

Sets the cap on the number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time.

Usage

From source file:org.datacleaner.util.ws.PooledServiceSession.java

/**
 * Creates a connection pool that can be used for one or more
 * {@link PooledServiceSession} objects.
 * /*  w  w  w.  ja  v  a2  s .c  o  m*/
 * @param maxConnections
 * @return
 */
public static GenericObjectPool<Integer> createConnectionPool(int maxConnections) {
    GenericObjectPool<Integer> connectionPool = new GenericObjectPool<Integer>(
            new ConnectionPoolObjectFactory());
    connectionPool.setMaxActive(maxConnections);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    return connectionPool;
}

From source file:org.graylog2.syslog4j.impl.pool.generic.GenericSyslogPoolFactory.java

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

    try {//from   w w  w  .  j  a v  a 2s. 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:org.idevlab.rjc.ds.PoolableDataSource.java

private synchronized GenericObjectPool createPool() {
    if (closed) {
        throw new RedisException("Data source is closed");
    }/*from www  . j  a  va 2  s. c  om*/

    if (connectionPool == null) {
        GenericObjectPool gop = new GenericObjectPool();
        gop.setMaxActive(maxActive);
        gop.setMaxIdle(maxIdle);
        gop.setMinIdle(minIdle);
        gop.setMaxWait(maxWait);
        gop.setTestOnBorrow(testOnBorrow);
        gop.setTestOnReturn(testOnReturn);
        gop.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        gop.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        gop.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        gop.setTestWhileIdle(testWhileIdle);
        connectionPool = gop;
        createConnectionFactory();
        try {
            for (int i = 0; i < initialSize; i++) {
                connectionPool.addObject();
            }
        } catch (Exception e) {
            throw new RedisException("Error preloading the connection pool", e);
        }
    }

    return connectionPool;
}

From source file:org.jaffa.persistence.engines.jdbcengine.datasource.DbcpDataSourceConnectionFactory.java

private void createDbcpDataSource() throws SQLException {
    try {//from  w  w w  . j  a v a  2s  .  c o m
        synchronized (dataSourceLock) {
            if (c_dataSource == null) {
                // First we load the underlying JDBC driver.
                Class.forName(getDriverClass());

                // Next, we'll need a ObjectPool that serves as the actual pool of connections.
                // We'll use a GenericObjectPool instance
                GenericObjectPool connectionPool = new GenericObjectPool(null);
                if (getMaximumConnections() != null)
                    connectionPool.setMaxActive(getMaximumConnections());
                if (getMinimumConnections() != null)
                    connectionPool.setMaxIdle(getMinimumConnections());
                if (getMaxWait() != null)
                    connectionPool.setMaxWait(getMaxWait());
                if (getTestOnBorrow() != null)
                    connectionPool.setTestOnBorrow(getTestOnBorrow());
                if (getTestOnReturn() != null)
                    connectionPool.setTestOnReturn(getTestOnReturn());

                // Next, we'll create a ConnectionFactory that the pool will use to create Connections.
                // We'll use the DriverManagerConnectionFactory
                ConnectionFactory connectionFactory = null;
                if (m_driverProperties == null || m_driverProperties.isEmpty())
                    connectionFactory = new DriverManagerConnectionFactory(getUrl(), getUser(), getPassword());
                else
                    connectionFactory = new DriverManagerConnectionFactory(getUrl(), getDriverProperties());
                // Now we'll create the PoolableConnectionFactory, which wraps the "real" Connections created by the ConnectionFactory with the classes that implement the pooling functionality.
                KeyedObjectPoolFactory stmtPoolFactory = new StackKeyedObjectPoolFactory();
                String validationQuery = getValidationQuery();
                boolean defaultReadOnly = false;
                boolean defaultAutoCommit = false;
                PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
                        connectionFactory, connectionPool, stmtPoolFactory, validationQuery, defaultReadOnly,
                        defaultAutoCommit);

                // Finally, we create the PoolingDriver itself, passing in the object pool we created.
                c_dataSource = new PoolingDataSource(connectionPool);

                // This will allow us to access the underlying Connection objects, required by the JdbcSecurityPlugin
                ((PoolingDataSource) c_dataSource).setAccessToUnderlyingConnectionAllowed(true);

                if (log.isDebugEnabled())
                    log.debug("Created the Dbcp DataSource");
            }
        }
    } catch (Exception e) {
        String str = "Error in creating the Dbcp DataSource";
        log.error(str, e);
        throw new SQLException(str);
    }
}

From source file:org.jvoicexml.implementation.pool.KeyedResourcePool.java

/**
 * Adds the given resource factory.//from  ww w.  j  a  v  a2 s .  com
 * @param resourceFactory The {@link ResourceFactory} to add.
 * @exception Exception error populating the pool
 */
public void addResourceFactory(final ResourceFactory<T> resourceFactory) throws Exception {
    final PoolableObjectFactory factory = new PoolableResourceFactory<T>(resourceFactory);
    final GenericObjectPool pool = new GenericObjectPool(factory);
    final int instances = resourceFactory.getInstances();
    pool.setMinIdle(instances);
    pool.setMaxActive(instances);
    pool.setMaxIdle(instances);
    pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
    final String type = resourceFactory.getType();
    pools.put(type, pool);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("loading resources of type '" + type + "'...");
    }
    for (int i = 0; i < instances; i++) {
        pool.addObject();
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("...resources loaded.");
    }
}

From source file:org.kitodo.data.database.persistence.apache.ConnectionManager.java

/**
 *
 * @param connectURI//from   w  ww.j ava 2  s  .co m
 *            - JDBC Connection URI.
 * @param username
 *            - JDBC Connection username.
 * @param password
 *            - JDBC Connection password.
 * @param minIdle
 *            - Minimum number of idel connection in the connection pool.
 * @param maxActive
 *            - Connection Pool Maximum Capacity (Size).
 */
public static DataSource setupDataSource(String connectURI, String username, String password, int minIdle,
        int maxActive) {
    //
    // First, we'll need a ObjectPool that serves as the actual pool of
    // connections.
    //
    // We'll use a GenericObjectPool instance, although any ObjectPool
    // implementation will suffice.
    //
    GenericObjectPool connectionPool = new GenericObjectPool(null);

    connectionPool.setMinIdle(minIdle);
    connectionPool.setMaxActive(maxActive);

    ConnectionManager._pool = connectionPool;
    // we keep it for two reasons
    // #1 We need it for statistics/debugging
    // #2 PoolingDataSource does not have getPool() method, for some
    // obscure, weird reason.

    //
    // Next, we'll create a ConnectionFactory that the pool will use to
    // create Connections.
    // We'll use the DriverManagerConnectionFactory, using the connect
    // string from configuration
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username, password);

    //
    // Now we'll create the PoolableConnectionFactory, which wraps the
    // "real" Connections created by
    // the ConnectionFactory with the classes that implement the pooling
    // functionality.
    //
    new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);

    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

    return dataSource;
}

From source file:org.okinawaopenlabs.orientdb.client.ConnectionManagerJdbc.java

/**
 * @param config/*from  w ww .  j a  v  a  2 s.c  om*/
 * @throws SQLException
 */
private void initializeDataSource(Config config) {
    String user = config.getString(CONFIG_KEY_DB_USER);
    String password = config.getString(CONFIG_KEY_DB_PASSWORD);

    Properties params = new Properties();

    if (isNotEmpty(user) && isNotEmpty(password)) {
        params.put("user", user);
        params.put("password", password);
    }

    // ??
    String driver = config.getString(CONFIG_KEY_DB_DRIVER);
    boolean loadSuccess = DbUtils.loadDriver(driver);
    if (!loadSuccess) {
        String message = "failed to load driver.";
        throw new RuntimeException(message);
    }

    // ??DataSource ??
    @SuppressWarnings("rawtypes")
    GenericObjectPool pool = new GenericObjectPool();
    // ???
    int maxActive = config.getInt(CONFIG_KEY_DB_MAX_ACTIVE_CONN, 100);
    long maxWait = Long.parseLong(config.getString(CONFIG_KEY_DB_WAIT, "-1"));
    pool.setMaxActive(maxActive);
    pool.setMaxIdle(maxActive);
    pool.setMaxWait(maxWait);

    driverUrl = config.getString(CONFIG_KEY_DB_URL);
    ConnectionFactory connFactory = new DriverManagerConnectionFactory(driverUrl, params);
    new PoolableConnectionFactory(connFactory, pool, null, null, // validationQuery
            false, // defaultReadOnly
            false); // defaultAutoCommit
    dataSource = new PoolingDataSource(pool);
}

From source file:org.openanzo.datasource.nodecentric.internal.NodeCentricDatasource.java

/**
 * Initialize a jdbc connection pool/*from   w ww. j  a  v a 2  s  .  co m*/
 * 
 * @param type
 *            either rw or query pool
 * @param maxActive
 *            maximum number of connections in pool
 * @param configuration
 *            configuration properties used for creating database connections
 * @return connection pool
 * @throws AnzoException
 *             {@link ExceptionConstants#RDB.DRIVER_NAME} if there was a problem loading class for database driver
 */
private GenericObjectPool initializeConnectionFactory(boolean write, int maxActive) throws AnzoException {
    // Will use in jndi
    // DataSource ds = (DataSource) ctx.lookup(RepositoryProperties.getDatabaseJndiName(properties));
    try {
        Class.forName(configuration.getDriverClassName());
    } catch (ClassNotFoundException e1) {
        throw new AnzoException(ExceptionConstants.RDB.DRIVER_NAME, e1, configuration.getDriverClassName());
    }
    Properties props = new Properties();
    props.put("user", configuration.getUser());
    props.put("password", configuration.getPassword());
    props.put("SetBigStringTryClob", "true");
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(configuration.getJdbcUrl(), props);
    GenericObjectPool connectionPool = new GenericObjectPool();
    connectionPool.setMaxActive(maxActive);
    connectionPool.setMaxIdle(Math.max(1, maxActive / 2));
    connectionPool.setMinIdle(0);
    connectionPool.setMinEvictableIdleTimeMillis(1000 * 60 * 10);
    connectionPool.setTimeBetweenEvictionRunsMillis(1000 * 60 * 10);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    connectionPool.setMaxWait(GenericKeyedObjectPool.DEFAULT_MAX_WAIT);
    connectionPool.setTestOnBorrow(true);
    GenericKeyedObjectPoolFactory statementPool = new GenericKeyedObjectPoolFactory(null, PS_CACHE_SIZE,
            GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, GenericKeyedObjectPool.DEFAULT_MAX_WAIT, PS_CACHE_SIZE,
            PS_CACHE_SIZE, GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW,
            GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN,
            GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,
            GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE);
    PoolableConnectionFactory pcf = new PoolableConnectionFactory(connectionFactory, connectionPool,
            statementPool, configuration.getValidationQuery(), false, true) {
        @Override
        public synchronized Object makeObject() throws Exception {
            Connection connection = (Connection) super.makeObject();
            initializeConnection(connection);
            return connection;
        }
    };

    if (configuration.getSupportsIsolation() && write)
        pcf.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
    else if (configuration.getSupportsIsolation() && !write)
        pcf.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
    return connectionPool;
}

From source file:org.openanzo.jdbc.container.RDBQuadStorePool.java

private void setupPool(GenericObjectPool pool, RDBQuadStoreFactory factory, int maxActive, int maxIdle,
        long blockWait) {
    pool.setFactory(factory);/*from  w ww. j  a va 2  s. c o m*/
    pool.setMaxActive(maxActive);
    pool.setMaxIdle(maxIdle);
    pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    pool.setMaxWait(blockWait);
}

From source file:org.openbravo.database.ConnectionProviderImpl.java

public void addNewPool(String dbDriver, String dbServer, String dbLogin, String dbPassword, int minConns,
        int maxConns, double maxConnTime, String dbSessionConfig, String rdbms, String name) throws Exception {
    log4j.debug("Loading underlying JDBC driver.");
    try {/* w w w.  j av a2  s.  co m*/
        Class.forName(dbDriver);
    } catch (ClassNotFoundException e) {
        throw new Exception(e);
    }
    log4j.debug("Done.");

    GenericObjectPool connectionPool = new GenericObjectPool(null);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    connectionPool.setMaxActive(maxConns);
    connectionPool.setTestOnBorrow(false);
    connectionPool.setTestOnReturn(false);
    connectionPool.setTestWhileIdle(false);

    KeyedObjectPoolFactory keyedObject = new StackKeyedObjectPoolFactory();
    ConnectionFactory connectionFactory = new OpenbravoDriverManagerConnectionFactory(dbServer, dbLogin,
            dbPassword, dbSessionConfig, rdbms);
    @SuppressWarnings("unused")
    // required by dbcp
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, keyedObject, null, false, true);

    Class.forName("org.apache.commons.dbcp.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    driver.registerPool(contextName + "_" + name, connectionPool);

    if (this.defaultPoolName == null || this.defaultPoolName.equals("")) {
        this.defaultPoolName = name;
        this.bbdd = dbServer;
        this.rdbms = rdbms;
    }
}