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

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

Introduction

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

Prototype

public synchronized void setInitialSize(int initialSize) 

Source Link

Document

Sets the initial size of the connection pool.

Note: this method currently has no effect once the pool has been initialized.

Usage

From source file:de.siemens.quantarch.bugs.IssueTrackerParser.java

/**
 * Build the database connection//from  w  w w .java  2 s  . co m
 * 
 * @return
 */
private DataSource buildDataSource() {
    // create the data source for database
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://" + projectConfig.getDbHost() + ":3306/" + projectConfig.getDbName());
    dataSource.setUsername(projectConfig.getDbUser());
    dataSource.setPassword(projectConfig.getDbPassword());
    dataSource.setInitialSize(30);
    return dataSource;
}

From source file:binky.reportrunner.service.impl.DatasourceServiceImpl.java

private DataSource getDs(RunnerDataSource runnerDs)
        throws SecurityException, InstantiationException, IllegalAccessException, ClassNotFoundException,
        PropertyVetoException, NamingException, EncryptionException {

    final String jndiDataSource = runnerDs.getJndiName();

    if (StringUtils.isBlank(jndiDataSource)) {
        EncryptionUtil enc = new EncryptionUtil();
        logger.info("using dbcp pooled connection for: " + runnerDs.getDataSourceName());

        String jdbcUser = runnerDs.getUsername();
        if (StringUtils.isBlank(runnerDs.getPassword()))
            throw new SecurityException("password is empty");
        String jdbcPassword = enc.decrpyt(secureKey, runnerDs.getPassword());

        String jdbcUrl = runnerDs.getJdbcUrl();
        String databaseDriver = runnerDs.getJdbcClass();

        Class.forName(databaseDriver).newInstance();

        BasicDataSource ds1 = new BasicDataSource();
        ds1.setDriverClassName(databaseDriver);
        ds1.setUrl(jdbcUrl);//from   w ww.j a v a  2 s .c o m
        ds1.setUsername(jdbcUser);
        ds1.setPassword(jdbcPassword);
        ds1.setInitialSize(runnerDs.getInitialPoolSize());
        ds1.setMaxActive(runnerDs.getMaxPoolSize());

        ds1.setRemoveAbandoned(true);
        ds1.setRemoveAbandonedTimeout(600);

        // do not want anything updating anything
        ds1.setDefaultReadOnly(true);

        ds1.setLogAbandoned(true);
        ds1.setTestOnBorrow(true);
        ds1.setTestOnReturn(true);
        ds1.setTestWhileIdle(true);

        // does this work across all RBMS? - no it doesn't
        //ds1.setValidationQuery("select 1");
        //ds1.setValidationQueryTimeout(300);

        return ds1;
    } else {
        logger.info(
                "getting datasource from JNDI url: " + jndiDataSource + " for " + runnerDs.getDataSourceName());
        Context initContext = new InitialContext();
        DataSource ds = (DataSource) initContext.lookup("java:/comp/env/" + jndiDataSource);
        return ds;
    }
}

From source file:fr.cnes.sitools.datasource.jdbc.business.SitoolsSQLDataSourceFactory.java

/**
 * Local creation of a DataSource/*w  w  w .j a  va 2s .  c o m*/
 * 
 * @param driver
 *          the database driver
 * @param connectURI
 *          the URI to connect
 * @param userName
 *          the database user name
 * @param password
 *          the password
 * @param schemaOnConnection
 *          the schema on connection
 * @return SitoolsDataSource a standard data source for SITools
 */
public SitoolsSQLDataSource setupDataSource(String driver, String connectURI, String userName, String password,
        String schemaOnConnection) {

    String key = connectURI + "@" + userName;
    SitoolsSQLDataSource foundDatasource = dataSources.get(key);
    if (foundDatasource == null) {

        BasicDataSource ds = new BasicDataSource();
        // OSGi
        ds.setDriverClassLoader(getClass().getClassLoader());
        ds.setDriverClassName(driver);
        ds.setUsername(userName);
        ds.setPassword(password);
        ds.setUrl(connectURI);
        ds.setMaxActive(10);
        ds.setInitialSize(1);
        // test that the connection is alive on each request. If not It will be dropped from the pool and another
        // connection will be created
        if (!"org.hsqldb.jdbcDriver".equals(driver)) {
            ds.setTestOnBorrow(true);
            ds.setValidationQuery("SELECT 1");
        }
        // ds.setDefaultReadOnly(false);
        // ds.setDefaultAutoCommit(true);
        JDBCDataSource jdbcDS = new JDBCDataSource();
        jdbcDS.setName(key);
        jdbcDS.setDriverClass(driver);
        foundDatasource = new SitoolsSQLDataSource(jdbcDS, ds, schemaOnConnection);
        dataSources.put(key, foundDatasource);
    }
    return foundDatasource;
}

From source file:net.comze.framework.orm.datasource.DbcpDataSourceFactory.java

private BasicDataSource initialize(Properties properties) {
    ObjectUtils.notNull(properties, "properties");
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(properties.getProperty(JDBC_DRIVER));
    basicDataSource.setUrl(properties.getProperty(JDBC_URL));
    basicDataSource.setUsername(properties.getProperty(JDBC_USERNAME));
    basicDataSource.setPassword(properties.getProperty(JDBC_PASSWORD));

    if (properties.containsKey(DBCP_INITIALSIZE)) {
        basicDataSource.setInitialSize(Integer.parseInt(properties.getProperty(DBCP_INITIALSIZE)));
    }/*from   ww  w  .jav  a 2 s  . c om*/
    if (properties.containsKey(DBCP_MAXACTIVE)) {
        basicDataSource.setMaxActive(Integer.parseInt(properties.getProperty(DBCP_MAXACTIVE)));
    }
    if (properties.containsKey(DBCP_MAXIDLE)) {
        basicDataSource.setMaxIdle(Integer.parseInt(properties.getProperty(DBCP_MAXIDLE)));
    }
    if (properties.containsKey(DBCP_MAXWAIT)) {
        basicDataSource.setMaxWait(Long.parseLong(properties.getProperty(DBCP_MAXWAIT)));
    }
    if (properties.containsKey(DBCP_MINEVICTABLEIDLETIMEMILLIS)) {
        basicDataSource.setMinEvictableIdleTimeMillis(
                Long.parseLong(properties.getProperty(DBCP_MINEVICTABLEIDLETIMEMILLIS)));
    }
    if (properties.containsKey(DBCP_MINIDLE)) {
        basicDataSource.setMinIdle(Integer.parseInt(properties.getProperty(DBCP_MINIDLE)));
    }
    if (properties.containsKey(DBCP_NUMTESTSPEREVICTIONRUN)) {
        basicDataSource.setNumTestsPerEvictionRun(
                Integer.parseInt(properties.getProperty(DBCP_NUMTESTSPEREVICTIONRUN)));
    }
    if (properties.containsKey(DBCP_REMOVEABANDONED)) {
        basicDataSource.setRemoveAbandoned(Boolean.parseBoolean(properties.getProperty(DBCP_REMOVEABANDONED)));
    }
    if (properties.containsKey(DBCP_REMOVEABANDONEDTIMEOUT)) {
        basicDataSource.setRemoveAbandonedTimeout(
                Integer.parseInt(properties.getProperty(DBCP_REMOVEABANDONEDTIMEOUT)));
    }
    if (properties.containsKey(DBCP_TESTONBORROW)) {
        basicDataSource.setTestOnBorrow(Boolean.parseBoolean(properties.getProperty(DBCP_TESTONBORROW)));
    }
    if (properties.containsKey(DBCP_TESTONCREATE)) {
        basicDataSource.setTestOnReturn(Boolean.parseBoolean(properties.getProperty(DBCP_TESTONCREATE)));
    }
    if (properties.containsKey(DBCP_TESTONRETURN)) {
        basicDataSource.setTestOnReturn(Boolean.parseBoolean(properties.getProperty(DBCP_TESTONRETURN)));
    }
    if (properties.containsKey(DBCP_TESTWHILEIDLE)) {
        basicDataSource.setTestWhileIdle(Boolean.parseBoolean(properties.getProperty(DBCP_TESTWHILEIDLE)));
    }
    if (properties.containsKey(DBCP_TIMEBETWEENEVICTIONRUNSMILLIS)) {
        basicDataSource.setTimeBetweenEvictionRunsMillis(
                Long.parseLong(properties.getProperty(DBCP_TIMEBETWEENEVICTIONRUNSMILLIS)));
    }
    if (properties.containsKey(DBCP_VALIDATIONQUERY)) {
        basicDataSource.setValidationQuery(properties.getProperty(DBCP_VALIDATIONQUERY));
    }
    if (properties.containsKey(DBCP_VALIDATIONQUERYTIMEOUT)) {
        basicDataSource.setValidationQueryTimeout(
                Integer.parseInt(properties.getProperty(DBCP_VALIDATIONQUERYTIMEOUT)));
    }
    return basicDataSource;
}

From source file:fr.cnes.sitools.datasource.jdbc.business.SitoolsDataSourceFactory.java

/**
 * Setup a dataSource for "users". Usage is for all users for consultation functions.
 * //from   ww  w. java2s  .  co m
 * @param dataSource
 *          the DataSource to update
 * @return SitoolsDataSource the new DataSource
 */
public SitoolsDataSource setupDataSourceForUsers(JDBCDataSource dataSource) {
    String key = dataSource.getId();
    SitoolsDataSource foundDatasource = dataSources.get(key);
    if (foundDatasource == null) {

        BasicDataSource ds = new BasicDataSource();
        // OSGi
        ds.setDriverClassLoader(getClass().getClassLoader());
        ds.setDriverClassName(dataSource.getDriverClass());
        ds.setUsername(dataSource.getUserLogin());
        ds.setPassword(dataSource.getUserPassword());
        ds.setUrl(dataSource.getUrl());
        ds.setMaxActive(dataSource.getMaxActive());
        ds.setInitialSize(dataSource.getInitialSize());
        ds.setDefaultReadOnly(true);
        if ((dataSource.getSchemaOnConnection() != null) && !dataSource.getSchemaOnConnection().equals("")) {
            ds.setDefaultCatalog(dataSource.getSchemaOnConnection());
        }
        foundDatasource = new SitoolsDataSource(dataSource, ds, dataSource.getSchemaOnConnection());
        dataSources.put(key, foundDatasource);
    }
    return foundDatasource;
}

From source file:fr.cnes.sitools.datasource.jdbc.business.SitoolsSQLDataSourceFactory.java

/**
 * Setup a dataSource for "users". Usage is for all users for consultation functions.
 * /*w  w  w  . jav  a  2s  .  c  o  m*/
 * @param dataSource
 *          the DataSource to update
 * @return SitoolsDataSource the new DataSource
 */
public SitoolsSQLDataSource setupDataSourceForUsers(JDBCDataSource dataSource) {
    String key = dataSource.getId();
    SitoolsSQLDataSource foundDatasource = dataSources.get(key);
    if (foundDatasource == null) {

        BasicDataSource ds = new BasicDataSource();
        // OSGi
        ds.setDriverClassLoader(getClass().getClassLoader());
        ds.setDriverClassName(dataSource.getDriverClass());
        ds.setUsername(dataSource.getUserLogin());
        ds.setPassword(dataSource.getUserPassword());
        ds.setUrl(dataSource.getUrl());
        ds.setMaxActive(dataSource.getMaxActive());
        ds.setInitialSize(dataSource.getInitialSize());
        ds.setDefaultReadOnly(true);
        // test that the connection is alive on each request. If not It will be dropped from the pool and another
        // connection will be created
        ds.setTestOnBorrow(true);
        ds.setValidationQuery("SELECT 1");
        if ((dataSource.getSchemaOnConnection() != null) && !dataSource.getSchemaOnConnection().equals("")) {
            ds.setDefaultCatalog(dataSource.getSchemaOnConnection());
        }
        foundDatasource = new SitoolsSQLDataSource(dataSource, ds, dataSource.getSchemaOnConnection());
        dataSources.put(key, foundDatasource);
    }
    return foundDatasource;
}

From source file:com.atypon.wayf.guice.WayfGuiceModule.java

@Provides
@Singleton//from   w ww .j  a  va 2  s  .com
public NamedParameterJdbcTemplate getJdbcTemplate(@Named("jdbc.driver") String driver,
        @Named("jdbc.username") String username, @Named("jdbc.password") String password,
        @Named("jdbc.url") String url, @Named("jdbc.maxActive") Integer maxActive,
        @Named("jdbc.maxIdle") Integer maxIdle, @Named("jdbc.initialSize") Integer initialSize,
        @Named("jdbc.validationQuery") String validationQuery) {
    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName(driver);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    dataSource.setUrl(url);
    dataSource.setMaxActive(maxActive);
    dataSource.setMaxIdle(maxIdle);
    dataSource.setInitialSize(initialSize);
    dataSource.setValidationQuery(validationQuery);

    return new NamedParameterJdbcTemplate(dataSource);
}

From source file:com.google.gerrit.server.schema.DataSourceProvider.java

private DataSource open(final SitePaths site, final Config cfg, final Context context,
        final DataSourceType dst) {
    ConfigSection dbs = new ConfigSection(cfg, "database");
    String driver = dbs.optional("driver");
    if (Strings.isNullOrEmpty(driver)) {
        driver = dst.getDriver();//from ww  w.j a v a 2 s.c  om
    }

    String url = dbs.optional("url");
    if (Strings.isNullOrEmpty(url)) {
        url = dst.getUrl();
    }

    String username = dbs.optional("username");
    String password = dbs.optional("password");

    boolean usePool;
    if (context == Context.SINGLE_USER) {
        usePool = false;
    } else {
        usePool = cfg.getBoolean("database", "connectionpool", dst.usePool());
    }

    if (usePool) {
        final BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        if (username != null && !username.isEmpty()) {
            ds.setUsername(username);
        }
        if (password != null && !password.isEmpty()) {
            ds.setPassword(password);
        }
        ds.setMaxActive(cfg.getInt("database", "poollimit", 8));
        ds.setMinIdle(cfg.getInt("database", "poolminidle", 4));
        ds.setMaxIdle(cfg.getInt("database", "poolmaxidle", 4));
        ds.setMaxWait(ConfigUtil.getTimeUnit(cfg, "database", null, "poolmaxwait",
                MILLISECONDS.convert(30, SECONDS), MILLISECONDS));
        ds.setInitialSize(ds.getMinIdle());
        return ds;

    } else {
        // Don't use the connection pool.
        //
        try {
            final Properties p = new Properties();
            p.setProperty("driver", driver);
            p.setProperty("url", url);
            if (username != null) {
                p.setProperty("user", username);
            }
            if (password != null) {
                p.setProperty("password", password);
            }
            return new SimpleDataSource(p);
        } catch (SQLException se) {
            throw new ProvisionException("Database unavailable", se);
        }
    }
}

From source file:com.googlesource.gerrit.plugins.verifystatus.server.schema.CiDataSourceProvider.java

private DataSource open(Context context, CiDataSourceType dst) {
    // ConfigSection dbs = new ConfigSection(cfg, "database");
    String driver = config.getString("driver");
    if (Strings.isNullOrEmpty(driver)) {
        driver = dst.getDriver();//  w w w . java 2  s.  co m
    }

    String url = config.getString("dbUrl");
    if (Strings.isNullOrEmpty(url)) {
        url = dst.getUrl();
    }

    String username = config.getString("username");
    String password = config.getString("password");
    String interceptor = config.getString("dataSourceInterceptorClass");

    boolean usePool;
    if (context == Context.SINGLE_USER) {
        usePool = false;
    } else {
        usePool = config.getBoolean("connectionpool", dst.usePool());
    }

    if (usePool) {
        final BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        if (username != null && !username.isEmpty()) {
            ds.setUsername(username);
        }
        if (password != null && !password.isEmpty()) {
            ds.setPassword(password);
        }
        ds.setMaxActive(config.getInt("poollimit", DEFAULT_POOL_LIMIT));
        ds.setMinIdle(config.getInt("poolminidle", 4));
        ds.setMaxIdle(config.getInt("poolmaxidle", 4));
        String valueString = config.getString("poolmaxwait");
        if (Strings.isNullOrEmpty(valueString)) {
            ds.setMaxWait(MILLISECONDS.convert(30, SECONDS));
        } else {
            ds.setMaxWait(ConfigUtil.getTimeUnit(valueString, MILLISECONDS.convert(30, SECONDS), MILLISECONDS));
        }
        ds.setInitialSize(ds.getMinIdle());
        exportPoolMetrics(ds);
        return intercept(interceptor, ds);
    }
    // Don't use the connection pool.
    try {
        Properties p = new Properties();
        p.setProperty("driver", driver);
        p.setProperty("url", url);
        if (username != null) {
            p.setProperty("user", username);
        }
        if (password != null) {
            p.setProperty("password", password);
        }
        return intercept(interceptor, new SimpleDataSource(p));
    } catch (SQLException se) {
        throw new ProvisionException("Database unavailable", se);
    }
}

From source file:com.googlesource.gerrit.plugins.ci.server.schema.CiDataSourceProvider.java

private DataSource open(Context context, CiDataSourceType dst) {
    //ConfigSection dbs = new ConfigSection(cfg, "database");
    String driver = config.getString("driver");
    if (Strings.isNullOrEmpty(driver)) {
        driver = dst.getDriver();/*from w ww  .j av a  2s  .  co m*/
    }

    String url = config.getString("dbUrl");
    if (Strings.isNullOrEmpty(url)) {
        url = dst.getUrl();
    }

    String username = config.getString("username");
    String password = config.getString("password");
    String interceptor = config.getString("dataSourceInterceptorClass");

    boolean usePool;
    if (context == Context.SINGLE_USER) {
        usePool = false;
    } else {
        usePool = config.getBoolean("connectionpool", dst.usePool());
    }

    if (usePool) {
        final BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        if (username != null && !username.isEmpty()) {
            ds.setUsername(username);
        }
        if (password != null && !password.isEmpty()) {
            ds.setPassword(password);
        }
        ds.setMaxActive(config.getInt("poollimit", DEFAULT_POOL_LIMIT));
        ds.setMinIdle(config.getInt("poolminidle", 4));
        ds.setMaxIdle(config.getInt("poolmaxidle", 4));
        String valueString = config.getString("poolmaxwait");
        if (Strings.isNullOrEmpty(valueString)) {
            ds.setMaxWait(MILLISECONDS.convert(30, SECONDS));
        } else {
            ds.setMaxWait(ConfigUtil.getTimeUnit(valueString, MILLISECONDS.convert(30, SECONDS), MILLISECONDS));
        }
        ds.setInitialSize(ds.getMinIdle());
        exportPoolMetrics(ds);
        return intercept(interceptor, ds);
    } else {
        // Don't use the connection pool.
        //
        try {
            Properties p = new Properties();
            p.setProperty("driver", driver);
            p.setProperty("url", url);
            if (username != null) {
                p.setProperty("user", username);
            }
            if (password != null) {
                p.setProperty("password", password);
            }
            return intercept(interceptor, new SimpleDataSource(p));
        } catch (SQLException se) {
            throw new ProvisionException("Database unavailable", se);
        }
    }
}