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

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

Introduction

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

Prototype

public synchronized void setMinIdle(int minIdle) 

Source Link

Document

Sets the minimum number of idle connections in the pool.

Usage

From source file:com.alibaba.otter.node.etl.common.datasource.impl.DBDataSourceService.java

private DataSource createDataSource(String url, String userName, String password, String driverClassName,
        DataMediaType dataMediaType, String encoding) {
    BasicDataSource dbcpDs = new BasicDataSource();

    dbcpDs.setInitialSize(initialSize);// ?
    dbcpDs.setMaxActive(maxActive);// ?????
    dbcpDs.setMaxIdle(maxIdle);// ??
    dbcpDs.setMinIdle(minIdle);// ?0?
    dbcpDs.setMaxWait(maxWait);// ??-1?
    dbcpDs.setRemoveAbandoned(true);// ??removeAbandonedTimeout
    dbcpDs.setLogAbandoned(true);// ??
    dbcpDs.setRemoveAbandonedTimeout(removeAbandonedTimeout); // ?
    dbcpDs.setNumTestsPerEvictionRun(numTestsPerEvictionRun);// ??
    dbcpDs.setTestOnBorrow(false);// ??
    dbcpDs.setTestOnReturn(false);// ??
    dbcpDs.setTestWhileIdle(true);// ????
    dbcpDs.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // ????????
    dbcpDs.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // ???????

    // ??// www  . j  a  v  a  2s .c om
    dbcpDs.setDriverClassName(driverClassName);
    dbcpDs.setUrl(url);
    dbcpDs.setUsername(userName);
    dbcpDs.setPassword(password);

    if (dataMediaType.isOracle()) {
        dbcpDs.addConnectionProperty("restrictGetTables", "true");
        dbcpDs.setValidationQuery("select 1 from dual");
    } else if (dataMediaType.isMysql()) {
        // open the batch mode for mysql since 5.1.8
        dbcpDs.addConnectionProperty("useServerPrepStmts", "false");
        dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true");
        dbcpDs.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");// 0000-00-00null
        dbcpDs.addConnectionProperty("yearIsDateType", "false");// ??year?date?
        dbcpDs.addConnectionProperty("noDatetimeStringSync", "true");// ,???
        if (StringUtils.isNotEmpty(encoding)) {
            if (StringUtils.equalsIgnoreCase(encoding, "utf8mb4")) {
                dbcpDs.addConnectionProperty("characterEncoding", "utf8");
                dbcpDs.setConnectionInitSqls(Arrays.asList("set names utf8mb4"));
            } else {
                dbcpDs.addConnectionProperty("characterEncoding", encoding);
            }
        }
        dbcpDs.setValidationQuery("select 1");
    } else {
        logger.error("ERROR ## Unknow database type");
    }

    return dbcpDs;
}

From source file:cn.cuizuoli.gotour.config.DataSourceConfig.java

@Bean
public DataSource userDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(env.getRequiredProperty("user.jdbc.driverClassName"));
    dataSource.setUrl(env.getRequiredProperty("user.jdbc.url"));
    dataSource.setUsername(env.getRequiredProperty("user.jdbc.username"));
    dataSource.setPassword(env.getRequiredProperty("user.jdbc.password"));
    dataSource.setInitialSize(env.getRequiredProperty("jdbc.initialSize", Integer.class));
    dataSource.setMaxActive(env.getRequiredProperty("jdbc.maxActive", Integer.class));
    dataSource.setMaxIdle(env.getRequiredProperty("jdbc.maxIdle", Integer.class));
    dataSource.setMinIdle(env.getRequiredProperty("jdbc.minIdle", Integer.class));
    dataSource.setDefaultAutoCommit(env.getRequiredProperty("jdbc.defaultAutoCommit", Boolean.class));
    dataSource.setPoolPreparedStatements(env.getRequiredProperty("jdbc.poolPreparedStatements", Boolean.class));
    dataSource.setValidationQuery(env.getRequiredProperty("jdbc.validationQuery"));
    dataSource.setTestOnBorrow(env.getRequiredProperty("jdbc.testOnBorrow", Boolean.class));
    dataSource.setTestOnReturn(env.getRequiredProperty("jdbc.testOnReturn", Boolean.class));
    dataSource.setTestWhileIdle(env.getRequiredProperty("jdbc.testWhileIdle", Boolean.class));
    dataSource.setTimeBetweenEvictionRunsMillis(
            env.getRequiredProperty("jdbc.timeBetweenEvictionRunsMillis", Long.class));
    dataSource.setNumTestsPerEvictionRun(env.getRequiredProperty("jdbc.numTestsPerEvictionRun", Integer.class));
    dataSource.setMinEvictableIdleTimeMillis(
            env.getRequiredProperty("jdbc.minEvictableIdleTimeMillis", Long.class));
    return dataSource;
}

From source file:cn.cuizuoli.gotour.config.DataSourceConfig.java

@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(env.getRequiredProperty("gotour.jdbc.driverClassName"));
    dataSource.setUrl(env.getRequiredProperty("gotour.jdbc.url"));
    dataSource.setUsername(env.getRequiredProperty("gotour.jdbc.username"));
    dataSource.setPassword(env.getRequiredProperty("gotour.jdbc.password"));
    dataSource.setInitialSize(env.getRequiredProperty("jdbc.initialSize", Integer.class));
    dataSource.setMaxActive(env.getRequiredProperty("jdbc.maxActive", Integer.class));
    dataSource.setMaxIdle(env.getRequiredProperty("jdbc.maxIdle", Integer.class));
    dataSource.setMinIdle(env.getRequiredProperty("jdbc.minIdle", Integer.class));
    dataSource.setDefaultAutoCommit(env.getRequiredProperty("jdbc.defaultAutoCommit", Boolean.class));
    dataSource.setPoolPreparedStatements(env.getRequiredProperty("jdbc.poolPreparedStatements", Boolean.class));
    dataSource.setValidationQuery(env.getRequiredProperty("jdbc.validationQuery"));
    dataSource.setTestOnBorrow(env.getRequiredProperty("jdbc.testOnBorrow", Boolean.class));
    dataSource.setTestOnReturn(env.getRequiredProperty("jdbc.testOnReturn", Boolean.class));
    dataSource.setTestWhileIdle(env.getRequiredProperty("jdbc.testWhileIdle", Boolean.class));
    dataSource.setTimeBetweenEvictionRunsMillis(
            env.getRequiredProperty("jdbc.timeBetweenEvictionRunsMillis", Long.class));
    dataSource.setNumTestsPerEvictionRun(env.getRequiredProperty("jdbc.numTestsPerEvictionRun", Integer.class));
    dataSource.setMinEvictableIdleTimeMillis(
            env.getRequiredProperty("jdbc.minEvictableIdleTimeMillis", Long.class));
    return dataSource;
}

From source file:cn.cuizuoli.gotour.config.DataSourceConfig.java

@Bean
public DataSource slaveDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(env.getRequiredProperty("gotour.slave.jdbc.driverClassName"));
    dataSource.setUrl(env.getRequiredProperty("gotour.slave.jdbc.url"));
    dataSource.setUsername(env.getRequiredProperty("gotour.slave.jdbc.username"));
    dataSource.setPassword(env.getRequiredProperty("gotour.slave.jdbc.password"));
    dataSource.setInitialSize(env.getRequiredProperty("jdbc.initialSize", Integer.class));
    dataSource.setMaxActive(env.getRequiredProperty("jdbc.maxActive", Integer.class));
    dataSource.setMaxIdle(env.getRequiredProperty("jdbc.maxIdle", Integer.class));
    dataSource.setMinIdle(env.getRequiredProperty("jdbc.minIdle", Integer.class));
    dataSource.setDefaultAutoCommit(env.getRequiredProperty("jdbc.defaultAutoCommit", Boolean.class));
    dataSource.setPoolPreparedStatements(env.getRequiredProperty("jdbc.poolPreparedStatements", Boolean.class));
    dataSource.setValidationQuery(env.getRequiredProperty("jdbc.validationQuery"));
    dataSource.setTestOnBorrow(env.getRequiredProperty("jdbc.testOnBorrow", Boolean.class));
    dataSource.setTestOnReturn(env.getRequiredProperty("jdbc.testOnReturn", Boolean.class));
    dataSource.setTestWhileIdle(env.getRequiredProperty("jdbc.testWhileIdle", Boolean.class));
    dataSource.setTimeBetweenEvictionRunsMillis(
            env.getRequiredProperty("jdbc.timeBetweenEvictionRunsMillis", Long.class));
    dataSource.setNumTestsPerEvictionRun(env.getRequiredProperty("jdbc.numTestsPerEvictionRun", Integer.class));
    dataSource.setMinEvictableIdleTimeMillis(
            env.getRequiredProperty("jdbc.minEvictableIdleTimeMillis", Long.class));
    return dataSource;
}

From source file:com.alibaba.otter.common.push.datasource.media.MediaPushDataSource.java

protected DataSource doCreateDataSource(String url) {
    BasicDataSource dbcpDs = new BasicDataSource();

    dbcpDs.setInitialSize(initialSize);// ?
    dbcpDs.setMaxActive(maxActive);// ?????
    dbcpDs.setMaxIdle(maxIdle);// ??
    dbcpDs.setMinIdle(minIdle);// ?0?
    dbcpDs.setMaxWait(maxWait);// ??-1?
    dbcpDs.setRemoveAbandoned(true);// ??removeAbandonedTimeout
    dbcpDs.setLogAbandoned(true);// ??
    dbcpDs.setRemoveAbandonedTimeout(removeAbandonedTimeout); // ?
    dbcpDs.setNumTestsPerEvictionRun(numTestsPerEvictionRun);// ??
    dbcpDs.setTestOnBorrow(false);// ??
    dbcpDs.setTestOnReturn(false);// ??
    dbcpDs.setTestWhileIdle(true);// ????
    dbcpDs.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // ????????
    dbcpDs.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // ???????

    // ??//from  w  w  w. j ava2s  .co m
    dbcpDs.setDriverClassName(driverClassName);
    dbcpDs.setUrl(url);
    dbcpDs.setUsername(userName);
    dbcpDs.setPassword(password);

    if (dataMediaType.isOracle()) {
        dbcpDs.addConnectionProperty("restrictGetTables", "true");
        dbcpDs.setValidationQuery("select 1 from dual");
    } else if (dataMediaType.isMysql()) {
        // open the batch mode for mysql since 5.1.8
        dbcpDs.addConnectionProperty("useServerPrepStmts", "false");
        dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true");
        dbcpDs.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");// 0000-00-00null
        dbcpDs.addConnectionProperty("yearIsDateType", "false");// ??year?date?
        if (StringUtils.isNotEmpty(encoding)) {
            dbcpDs.addConnectionProperty("characterEncoding", encoding);
        }
        dbcpDs.setValidationQuery("select 1");
    } else {
        logger.error("ERROR ## Unknow database type");
    }

    return dbcpDs;
}

From source file:com.jolbox.benchmark.BenchmarkTests.java

/**
 * /*from   ww  w .  ja  va 2 s.c  o  m*/
 *
 * @param doPreparedStatement 
 * @return time taken
 * @throws PropertyVetoException 
 * @throws InterruptedException 
 * @throws SQLException 
 */
private DataSource multiThreadedDBCP(boolean doPreparedStatement)
        throws PropertyVetoException, InterruptedException, SQLException {
    BasicDataSource cpds = new BasicDataSource();
    cpds.setDriverClassName("com.jolbox.bonecp.MockJDBCDriver");
    cpds.setUrl(url);
    cpds.setUsername(username);
    cpds.setPassword(password);
    cpds.setMaxIdle(-1);
    cpds.setMinIdle(-1);
    if (doPreparedStatement) {
        cpds.setPoolPreparedStatements(true);
        cpds.setMaxOpenPreparedStatements(max_statement);
    }
    cpds.setInitialSize(pool_size);
    cpds.setMaxActive(pool_size);
    return cpds;
}

From source file:com.jolbox.benchmark.BenchmarkTests.java

/**
 * /*from ww w .ja  va2 s . c  o m*/
 *
 * @return time taken
 * @throws SQLException
 */
private long singleDBCP() throws SQLException {
    // Start DBCP

    BasicDataSource cpds = new BasicDataSource();
    cpds.setDriverClassName("com.jolbox.bonecp.MockJDBCDriver");
    cpds.setUrl(url);
    cpds.setUsername(username);
    cpds.setPassword(password);
    cpds.setMaxIdle(-1);
    cpds.setMinIdle(-1);
    cpds.setMaxOpenPreparedStatements(max_statement);
    cpds.setInitialSize(pool_size);
    cpds.setMaxActive(pool_size);
    cpds.getConnection(); // call to initialize possible lazy structures etc 

    long start = System.currentTimeMillis();
    for (int i = 0; i < MAX_CONNECTIONS; i++) {
        Connection conn = cpds.getConnection();
        conn.close();
    }
    long end = (System.currentTimeMillis() - start);
    //      System.out.println("DBCP Single thread benchmark: "+end);

    cpds.close();
    return end;

}

From source file:com.jolbox.benchmark.BenchmarkTests.java

/**
 * Benchmarks PreparedStatement functionality (single thread) 
 * @return result//from  w w  w. j  a v a  2s.  c  om
 * 
 * @throws PropertyVetoException
 * @throws SQLException
 */
private long testPreparedStatementSingleThreadDBCP() throws PropertyVetoException, SQLException {
    BasicDataSource cpds = new BasicDataSource();
    cpds.setDriverClassName("com.jolbox.bonecp.MockJDBCDriver");
    cpds.setUrl(url);
    cpds.setUsername(username);
    cpds.setPassword(password);
    cpds.setMaxIdle(-1);
    cpds.setMinIdle(-1);
    cpds.setPoolPreparedStatements(true);
    cpds.setMaxOpenPreparedStatements(30);
    cpds.setInitialSize(pool_size);
    cpds.setMaxActive(pool_size);
    Connection conn = cpds.getConnection();

    long start = System.currentTimeMillis();
    for (int i = 0; i < MAX_CONNECTIONS; i++) {
        Statement st = conn.prepareStatement(TEST_QUERY);
        st.close();
    }
    conn.close();

    long end = (System.currentTimeMillis() - start);
    System.out.println("DBCP PreparedStatement Single thread benchmark: " + end);
    results.add("DBCP, " + end);
    // dispose of pool
    cpds.close();
    return end;
}

From source file:com.bstek.dorado.core.store.SqlBaseStoreSupport.java

protected synchronized DataSource getDataSource() throws Exception {
    if (dataSource != null) {
        return dataSource;
    }//from  www .  j  av a2 s  .  c  o m

    if (StringUtils.isBlank(namespace)) {
        throw new IllegalArgumentException("The namespace of store cannot be empty. ");
    }

    prepareNamespace();

    BasicDataSource pds = new BasicDataSource();
    dataSource = pds;

    pds.setDriverClassName(driverClassName);
    pds.setUrl(getConnectionUrl());
    pds.setUsername(username);
    pds.setPassword(password);
    pds.setDefaultCatalog(defaultCatalog);

    if (defaultAutoCommit != null) {
        pds.setDefaultAutoCommit(defaultAutoCommit.booleanValue());
    }
    if (defaultReadOnly != null) {
        pds.setDefaultReadOnly(defaultReadOnly.booleanValue());
    }
    if (defaultTransactionIsolation != null) {
        pds.setDefaultTransactionIsolation(defaultTransactionIsolation.intValue());
    }
    if (maxActive != null) {
        pds.setMaxActive(maxActive.intValue());
    }
    if (maxIdle != null) {
        pds.setMaxIdle(maxIdle.intValue());
    }
    if (minIdle != null) {
        pds.setMinIdle(minIdle.intValue());
    }
    if (initialSize != null) {
        pds.setInitialSize(initialSize.intValue());
    }
    if (maxWait != null) {
        pds.setMaxWait(maxWait.longValue());
    }
    if (timeBetweenEvictionRunsMillis != null) {
        pds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis.longValue());
    }
    if (minEvictableIdleTimeMillis != null) {
        pds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis.longValue());
    }
    return dataSource;
}

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 w ww . j  a  v  a2s  . c  o m*/
    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;
}