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

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

Introduction

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

Prototype

public synchronized void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) 

Source Link

Document

Sets the value of the #numTestsPerEvictionRun property.

Usage

From source file:com.alibaba.cobar.manager.test.ConnectionTest.java

/**
 * @param args/*from w w w  .  java  2  s.  c  o m*/
 */
public static void main(String[] args) {
    try {
        BasicDataSource ds = new BasicDataSource();
        ds.setUsername("test");
        ds.setPassword("");
        ds.setUrl("jdbc:mysql://10.20.153.178:9066/");
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setMaxActive(-1);
        ds.setMinIdle(0);
        ds.setTimeBetweenEvictionRunsMillis(600000);
        ds.setNumTestsPerEvictionRun(Integer.MAX_VALUE);
        ds.setMinEvictableIdleTimeMillis(GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
        Connection conn = ds.getConnection();

        Statement stm = conn.createStatement();
        stm.execute("show @@version");

        ResultSet rst = stm.getResultSet();
        rst.next();
        String version = rst.getString("VERSION");

        System.out.println(version);

    } catch (Exception exception) {
        System.out.println("10.20.153.178:9066   " + exception.getMessage() + exception);
    }
}

From source file:com.alibaba.cobar.manager.qa.modle.CobarFactory.java

public static CobarAdapter getCobarAdapter(String cobarNodeName) throws IOException {
    CobarAdapter cAdapter = new CobarAdapter();
    Properties prop = new Properties();
    prop.load(CobarFactory.class.getClassLoader().getResourceAsStream("cobarNode.properties"));
    BasicDataSource ds = new BasicDataSource();
    String user = prop.getProperty(cobarNodeName + ".user").trim();
    String password = prop.getProperty(cobarNodeName + ".password").trim();
    String ip = prop.getProperty(cobarNodeName + ".ip").trim();
    int managerPort = Integer.parseInt(prop.getProperty(cobarNodeName + ".manager.port").trim());
    int maxActive = -1;
    int minIdle = 0;
    long timeBetweenEvictionRunsMillis = 10 * 60 * 1000;
    int numTestsPerEvictionRun = Integer.MAX_VALUE;
    long minEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
    ds.setUsername(user);/*w  w  w  .  j a  va  2 s . c om*/
    ds.setPassword(password);
    ds.setUrl(new StringBuilder().append("jdbc:mysql://").append(ip).append(":").append(managerPort).append("/")
            .toString());
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setMaxActive(maxActive);
    ds.setMinIdle(minIdle);
    ds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    ds.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    ds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

    cAdapter.setDataSource(ds);
    return cAdapter;
}

From source file:com.pinterest.pinlater.backends.mysql.MySQLDataSources.java

private static DataSource createDataSource(String host, int port, String user, String passwd, int poolSize,
        int maxWaitMillis, int socketTimeoutMillis) {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource/*  www  . j  a v  a2s.  com*/
            .setUrl(String.format(
                    "jdbc:mysql://%s:%d?" + "connectTimeout=5000&" + "socketTimeout=%d&"
                            + "enableQueryTimeouts=false&" + "cachePrepStmts=true&" + "characterEncoding=UTF-8",
                    host, port, socketTimeoutMillis));
    dataSource.setUsername(user);
    dataSource.setPassword(passwd);
    dataSource.setDefaultAutoCommit(true);
    dataSource.setInitialSize(poolSize);
    dataSource.setMaxActive(poolSize);
    dataSource.setMaxIdle(poolSize);
    // deal with idle connection eviction
    dataSource.setValidationQuery("SELECT 1 FROM DUAL");
    dataSource.setTestOnBorrow(false);
    dataSource.setTestOnReturn(false);
    dataSource.setTestWhileIdle(true);
    dataSource.setMinEvictableIdleTimeMillis(5 * 60 * 1000);
    dataSource.setTimeBetweenEvictionRunsMillis(3 * 60 * 1000);
    dataSource.setNumTestsPerEvictionRun(poolSize);
    // max wait in milliseconds for a connection.
    dataSource.setMaxWait(maxWaitMillis);
    // force connection pool initialization.
    Connection conn = null;
    try {
        // Here not getting the connection from ThreadLocal no need to worry about that.
        conn = dataSource.getConnection();
    } catch (SQLException e) {
        LOG.error(String.format(
                "Failed to get a mysql connection when creating DataSource, " + "host: %s, port: %d", host,
                port), e);
    } finally {
        JdbcUtils.closeConnection(conn);
    }
    return dataSource;
}

From source file:com.alibaba.cobar.manager.dao.delegate.DataSourceCreator.java

@Override
public DataSource createDataSource(String ip, int port, String user, String password) {
    BasicDataSource ds = new BasicDataSource();
    ds.setUsername(user);//from   ww  w .j  av  a2 s.c  o m
    ds.setPassword(password);
    ds.setUrl(new StringBuilder().append("jdbc:mysql://").append(ip).append(":").append(port).append("/")
            .toString());
    ds.setDriverClassName(driverClassName);
    ds.setMaxActive(maxActive);
    ds.setMinIdle(minIdle);
    ds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    ds.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    ds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    return ds;
}

From source file:com.widsons.spr4.conf.DataBaseConf.java

@Bean(destroyMethod = "close")
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl(dbUrl);//  w w w  .j  a va  2 s. com
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    dataSource.setDriverClassName(driverClassName);
    dataSource.setTestOnBorrow(true);
    dataSource.setTestOnReturn(true);
    dataSource.setTestWhileIdle(true);
    dataSource.setTimeBetweenEvictionRunsMillis(1800000);
    dataSource.setNumTestsPerEvictionRun(3);
    dataSource.setMinEvictableIdleTimeMillis(1800000);
    return dataSource;
}

From source file:fr.gouv.diplomatie.applitutoriel.integration.conf.DataSourceConf.java

@Bean
public DataSource dataSource() {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(url);/*w  w w . jav  a2  s . c  o  m*/
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setMaxActive(maxActive);
    dataSource.setMaxIdle(maxIdle);
    dataSource.setMaxWait(maxWait);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setTestWhileIdle(testWhileIdle);
    dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    dataSource.setRemoveAbandoned(removeAbandoned);
    dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
    dataSource.setLogAbandoned(logAbandoned);
    return dataSource;
}

From source file:$.DataSourceConf.java

@Bean
    public DataSource dataSource() {
        final BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);//from   w  w  w .j a  va2s . c o  m
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setValidationQuery(validationQuery);
        dataSource.setMaxActive(maxActive);
        dataSource.setMaxIdle(maxIdle);
        dataSource.setMaxWait(maxWait);
        dataSource.setTestOnBorrow(testOnBorrow);
        dataSource.setTestWhileIdle(testWhileIdle);
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        dataSource.setRemoveAbandoned(removeAbandoned);
        dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        dataSource.setLogAbandoned(logAbandoned);
        return dataSource;
    }

From source file:com.alibaba.druid.benckmark.pool.CaseKylin_mysql.java

public void dbcp() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);//from  ww w.j ava 2  s .c o  m
    dataSource.setMaxIdle(maxIdle);
    dataSource.setMinIdle(minIdle);
    dataSource.setMaxWait(maxWait);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setTestOnBorrow(testWhileIdle);
    dataSource.setTestOnBorrow(testOnReturn);
    dataSource.setRemoveAbandoned(removeAbandoned);
    dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
    dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun);

    for (int i = 0; i < TEST_COUNT; ++i) {
        p0(dataSource, "dbcp", threadCount);
    }
    System.out.println();
}

From source file:com.alibaba.druid.benckmark.pool.CaseKylin_Oracle.java

public void dbcp() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);//from ww  w .  j  ava 2  s .  c om
    dataSource.setMaxIdle(maxIdle);
    dataSource.setMinIdle(minIdle);
    dataSource.setMaxWait(maxWait);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setDriverClassName(oracleDriverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setTestOnBorrow(testWhileIdle);
    dataSource.setTestOnBorrow(testOnReturn);
    dataSource.setRemoveAbandoned(removeAbandoned);
    dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
    dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    dataSource.setValidationQuery(validateQuery);

    for (int i = 0; i < TEST_COUNT; ++i) {
        p0(dataSource, "dbcp", threadCount);
    }
    System.out.println();
}

From source file:com.alibaba.otter.manager.biz.common.DataSourceCreator.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); // ???????

    // ??/*from   w  w  w.  ja  va  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;
}