Example usage for org.apache.commons.dbcp BasicDataSource setMaxActive

List of usage examples for org.apache.commons.dbcp BasicDataSource setMaxActive

Introduction

In this page you can find the example usage for org.apache.commons.dbcp BasicDataSource setMaxActive.

Prototype

public synchronized void setMaxActive(int maxActive) 

Source Link

Document

Sets the maximum number of active connections that can be allocated at the same time.

Usage

From source file:org.docksidestage.dockside.JdbcBeansJavaConfig.java

@Bean(name = "dataSource")
public DataSource createDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl(maihamaDbUrl);//from  w w w . ja v a 2  s .  c o m
    ds.setUsername("sa");
    ds.setPassword("");
    ds.setMaxActive(20);
    return ds;
}

From source file:org.docksidestage.oracle.JdbcBeansJavaConfig.java

@Bean(name = "dataSource")
public DataSource createDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    ds.setUrl(JdbcDiconResolver.resolveUrl("jdbc:oracle:thin:@localhost:1521:XE"));
    ds.setUsername("maihamadb");
    ds.setPassword("maihamadb");
    ds.setMaxActive(20);
    return ds;/*  www  . j  a v a2s.co  m*/
}

From source file:org.docksidestage.oracle.JdbcBeansResolaJavaConfig.java

@Bean(name = "resolaDataSource")
public DataSource createDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    ds.setUrl(JdbcDiconResolver.resolveUrl("jdbc:oracle:thin:@localhost:1521:XE"));
    ds.setUsername("resortlinedb");
    ds.setPassword("resortlinedb");
    ds.setMaxActive(20);
    return ds;//  w  ww .  jav a 2 s.  c  o  m
}

From source file:org.eclipse.dirigible.repository.datasource.DataSourceFacade.java

private DataSource createDirectDataSource(Properties properties) {
    String id = properties.getProperty(DataSourceFacade.PARAM_DB_ID);
    String name = properties.getProperty(DataSourceFacade.PARAM_DB_NAME);
    String url = properties.getProperty(DataSourceFacade.PARAM_DB_LOC);
    String driver = properties.getProperty(DataSourceFacade.PARAM_DB_DRIVER);
    String user = properties.getProperty(DataSourceFacade.PARAM_DB_USER);
    String password = properties.getProperty(DataSourceFacade.PARAM_DB_PASSWORD);
    String defaultAutoCommit = properties.getProperty(DataSourceFacade.PARAM_DB_AUTO_COMMIT);
    String maxActive = properties.getProperty(DataSourceFacade.PARAM_DB_AUTO_MAX_ACTIVE);
    String maxIdle = properties.getProperty(DataSourceFacade.PARAM_DB_AUTO_MAX_IDLE);
    String maxWait = properties.getProperty(DataSourceFacade.PARAM_DB_AUTO_MAX_WAIT);

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(driver);
    basicDataSource.setUrl(url);/* w  w w . ja  v  a 2  s .  c o  m*/
    basicDataSource.setUsername(user);
    basicDataSource.setPassword(password);
    basicDataSource.setDefaultAutoCommit(Boolean.parseBoolean(defaultAutoCommit));
    basicDataSource.setMaxActive(maxActive != null ? Integer.parseInt(maxActive) : 100);
    basicDataSource.setMaxIdle(maxIdle != null ? Integer.parseInt(maxIdle) : 30);
    basicDataSource.setMaxWait(maxWait != null ? Integer.parseInt(maxWait) : 10000);

    return basicDataSource;

}

From source file:org.eobjects.analyzer.connection.JdbcDatastore.java

public DataSource createDataSource() {
    initializeDriver();/*  w w w .  j  a  va 2 s.  c o  m*/

    BasicDataSource ds = new BasicDataSource();
    ds.setMaxActive(-1);
    ds.setDefaultAutoCommit(false);
    ds.setUrl(_jdbcUrl);

    if (_username != null && _password != null) {
        ds.setUsername(_username);
        ds.setPassword(_password);
    }
    return ds;
}

From source file:org.ff4j.test.utils.JdbcTestHelper.java

/**
 * Initialize DataSource with a pool of connections to HQL database.
 *
 * @return//from   w  w w  . j  a v a 2 s.  co m
 *      current data source
 */
public static DataSource createInMemoryHQLDataSource() {
    // Init DataSource
    BasicDataSource dbcpDataSource = new BasicDataSource();
    dbcpDataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    dbcpDataSource.setUsername("sa");
    dbcpDataSource.setPassword("");
    dbcpDataSource.setUrl("jdbc:hsqldb:mem:.");
    dbcpDataSource.setMaxActive(3);
    dbcpDataSource.setMaxIdle(2);
    dbcpDataSource.setInitialSize(2);
    dbcpDataSource.setValidationQuery("select 1 from INFORMATION_SCHEMA.SYSTEM_USERS;");
    dbcpDataSource.setPoolPreparedStatements(true);
    return dbcpDataSource;
}

From source file:org.geosde.core.jdbc.data.datasource.DataSourceUtil.java

/**
 * Builds up a default DBCP DataSource that easy to use connection factories
 * can use to setup a connection pool./*w  w  w . j ava2  s.  c o  m*/
 * 
 * @param url
 *            the jdbc url
 * @param driverName
 *            the jdbc driver full qualified class name
 * @param username
 * @param password
 * @param maxActive maximum number of concurrent connections in the pool
 * @param minIdle minimum number of concurrent connections in the pool
 * @param validationQuery
 *            the validation query to be used for connection liveliness on
 *            borrow, or null, if no check is to be performed
 * @param cachePreparedStatements
 *            wheter to cache prepared statements or not
 * @return
 * @throws DataSourceException
 */
public static ManageableDataSource buildDefaultDataSource(String url, String driverName, String username,
        String password, int maxActive, int minIdle, String validationQuery, boolean cachePreparedStatements,
        int removeAbandonedTimeout) throws DataSourceException {
    // basics
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverName);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    dataSource.setAccessToUnderlyingConnectionAllowed(true);

    // pool size
    dataSource.setMaxActive(maxActive);
    dataSource.setMinIdle(minIdle);

    // pool eviction settings
    dataSource.setMinEvictableIdleTimeMillis(1000 * 20);
    dataSource.setTimeBetweenEvictionRunsMillis(1000 * 10);

    // connection validation
    if (validationQuery != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(validationQuery);
    }

    // prepared statement cache
    if (cachePreparedStatements) {
        dataSource.setPoolPreparedStatements(true);
        dataSource.setMaxOpenPreparedStatements(10);
    }

    // remove abandoned connections (I know it's deprecated, but we do want
    // something shaving off lost connections. Let's give them 5 minutes of 
    // continuous usage
    if (removeAbandonedTimeout > 0) {
        dataSource.setRemoveAbandoned(true);
        dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        dataSource.setLogAbandoned(true);
    }

    Connection conn = null;
    try {
        conn = dataSource.getConnection();
    } catch (Exception e) {
        throw new DataSourceException("Connection test failed ", e);
    } finally {
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
            }
    }

    return new DBCPDataSource(dataSource);
}

From source file:org.geosde.core.jdbc.data.datasource.DBCPDataSourceFactory.java

public DataSource createNewDataSource(Map params) throws IOException {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName((String) DRIVERCLASS.lookUp(params));
    dataSource.setUrl((String) JDBC_URL.lookUp(params));
    dataSource.setUsername((String) USERNAME.lookUp(params));
    dataSource.setPassword((String) PASSWORD.lookUp(params));
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    dataSource.setMaxActive(((Integer) MAXACTIVE.lookUp(params)).intValue());
    dataSource.setMaxIdle(((Integer) MAXIDLE.lookUp(params)).intValue());

    // check the data source is properly setup by trying to gather a connection out of it
    Connection conn = null;/*  ww  w . ja va2s.  c o m*/
    try {
        conn = dataSource.getConnection();
    } catch (SQLException e) {
        throw new DataSourceException("Connection pool improperly set up: " + e.getMessage(), e);
    } finally {
        // close the connection at once
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
            }
    }

    return dataSource;
}

From source file:org.geosde.core.jdbc.JDBCDataStoreFactory.java

/**
 * DataSource access allowing SQL use: intended to allow client code to query available schemas.
 * <p>//from w  ww  .ja  v  a2s . c  o m
 * This DataSource is the clients responsibility to close() when they are finished using it.
 * </p> 
 * @param params Map of connection parameter.
 * @return DataSource for SQL use
 * @throws IOException
 */
public BasicDataSource createDataSource(Map params) throws IOException {
    //create a datasource
    BasicDataSource dataSource = new BasicDataSource();

    // driver
    dataSource.setDriverClassName(getDriverClassName());

    // url
    dataSource.setUrl(getJDBCUrl(params));

    // username (on embedded dbs it can be optional)
    String user = (String) USER.lookUp(params);
    if (user != null) {
        dataSource.setUsername(user);
    }

    // password
    String passwd = (String) PASSWD.lookUp(params);
    if (passwd != null) {
        dataSource.setPassword(passwd);
    }

    // max wait
    Integer maxWait = (Integer) MAXWAIT.lookUp(params);
    if (maxWait != null && maxWait != -1) {
        dataSource.setMaxWait(maxWait * 1000);
    }

    // connection pooling options
    Integer minConn = (Integer) MINCONN.lookUp(params);
    if (minConn != null) {
        dataSource.setMinIdle(minConn);
    }

    Integer maxConn = (Integer) MAXCONN.lookUp(params);
    if (maxConn != null) {
        dataSource.setMaxActive(maxConn);
    }

    Boolean validate = (Boolean) VALIDATECONN.lookUp(params);
    if (validate != null && validate && getValidationQuery() != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(getValidationQuery());
    }

    Boolean testWhileIdle = (Boolean) TEST_WHILE_IDLE.lookUp(params);
    if (testWhileIdle != null) {
        dataSource.setTestWhileIdle(testWhileIdle);
    }

    Integer timeBetweenEvictorRuns = (Integer) TIME_BETWEEN_EVICTOR_RUNS.lookUp(params);
    if (timeBetweenEvictorRuns != null && timeBetweenEvictorRuns > 0) {
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictorRuns * 1000l);
    }

    Integer minEvictableTime = (Integer) MIN_EVICTABLE_TIME.lookUp(params);
    if (minEvictableTime != null) {
        dataSource.setMinEvictableIdleTimeMillis(minEvictableTime * 1000l);
    }

    Integer evictorTestsPerRun = (Integer) EVICTOR_TESTS_PER_RUN.lookUp(params);
    if (evictorTestsPerRun != null) {
        dataSource.setNumTestsPerEvictionRun(evictorTestsPerRun);
    }

    // some datastores might need this
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    return dataSource;
}

From source file:org.geoserver.security.jdbc.AbstractJDBCService.java

/**
 * initialize a {@link DataSource} form a
 * {@link JdbcSecurityServiceConfig} object
 * /*  w  w w .ja va2 s .  co  m*/
 * @param config
 * @throws IOException
 */
public void initializeDSFromConfig(SecurityNamedServiceConfig namedConfig) throws IOException {
    JDBCSecurityServiceConfig config = (JDBCSecurityServiceConfig) namedConfig;
    if (config.isJndi()) {
        String jndiName = config.getJndiName();
        try {
            Context initialContext = new InitialContext();
            datasource = (DataSource) initialContext.lookup(jndiName);
        } catch (NamingException e) {
            throw new IOException(e);
        }
    } else {
        BasicDataSource bds = new BasicDataSource();
        bds.setDriverClassName(config.getDriverClassName());
        bds.setUrl(config.getConnectURL());
        bds.setUsername(config.getUserName());
        bds.setPassword(config.getPassword());
        bds.setDefaultAutoCommit(false);
        bds.setDefaultTransactionIsolation(DEFAULT_ISOLATION_LEVEL);
        bds.setMaxActive(10);
        datasource = bds;
    }
}