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

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

Introduction

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

Prototype

public synchronized void setMaxWait(long maxWait) 

Source Link

Document

Sets the maxWait property.

Usage

From source file:com.oncecorp.visa3d.bridge.utility.JdbcUtils.java

/**
 * This method create and returns the client data source object.  In this class,
 * we use "Jakarta-commons-release" to create the data source object.
 * @param driverClass The JDBC driver class name.
 * @param jdbcURL The database URL.//from  w  ww .ja va2s.  c o m
 * @param jdbcUID The database user id.
 * @param jdbcPW The database password.
 * @param maxConns maximum connections
 * @param timeOut  connection timeout
 * @return The data Source object.
 */
public static DataSource createPoolingDataSoruce(String driverClass, String jdbcURL, String jdbcUID,
        String jdbcPW, int maxConns, int timeOut) {
    try {
        Class.forName(driverClass);
        BasicDataSource bds = new BasicDataSource();
        bds.setDriverClassName(driverClass);
        bds.setMaxActive(maxConns);
        bds.setMaxWait(timeOut);
        bds.setUrl(jdbcURL);
        bds.setUsername(jdbcUID);
        bds.setPassword(jdbcPW);

        System.out.println("Use dbcp pool with datasource = " + bds);
        return bds;
    } catch (Exception e) {
        System.out.println("Data Source creating failed. -- " + driverClass + ", " + jdbcURL + ", " + jdbcUID
                + ", " + jdbcPW + ", ==" + e.getMessage());
        return null;
    }
}

From source file:com.weibo.datasys.parser.sql.DBConnectionFactory.java

public static void init() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(ConfigFactory.getString("jdbc.driverClassName"));
    dataSource.setUrl(ConfigFactory.getString("jdbc.url"));
    dataSource.setUsername(ConfigFactory.getString("jdbc.username"));
    dataSource.setPassword(ConfigFactory.getString("jdbc.password"));

    dataSource.setInitialSize(ConfigFactory.getInt("jdbc.initialSize", 1));
    dataSource.setMinIdle(ConfigFactory.getInt("jdbc.minIdle", 2));
    dataSource.setMaxIdle(ConfigFactory.getInt("jdbc.maxIdle", 10));
    dataSource.setMaxWait(ConfigFactory.getInt("jdbc.maxWait", 1000));
    dataSource.setMaxActive(ConfigFactory.getInt("jdbc.maxActive", 2));
    dataSource.addConnectionProperty("autoReconnect", "true");
    // ??/*from  w  w  w.  jav a 2s  . c o m*/
    dataSource.setTestWhileIdle(true);
    // ?sql?
    dataSource.setValidationQuery("select 'test'");
    // ?
    dataSource.setValidationQueryTimeout(5000);
    // 
    dataSource.setTimeBetweenEvictionRunsMillis(3600000);
    // ??
    dataSource.setMinEvictableIdleTimeMillis(3600000);

    ds = dataSource;
}

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

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

    dataSource.setMaxActive(maxActive);/*from  w  w w .  j a  v  a2s  .c  o  m*/
    dataSource.setMaxIdle(maxIdle);
    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);

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

From source file:cn.vlabs.umt.common.datasource.DatabaseUtil.java

public DatabaseUtil(Config config) {
    BasicDataSource ds = new BasicDataSource();

    ds.setMaxActive(config.getInt("database.maxconn", 10));
    ds.setMaxIdle(config.getInt("database.maxidle", 3));
    ds.setMaxWait(100);

    ds.setUsername(config.getStringProp("database.username", null));
    ds.setPassword(config.getStringProp("database.password", null));
    ds.setDriverClassName(config.getStringProp("database.driver", null));
    ds.setUrl(config.getStringProp("database.conn-url", null));
    ds.setTimeBetweenEvictionRunsMillis(3600000);
    ds.setMinEvictableIdleTimeMillis(1200000);
    datasource = ds;/*from   ww w. j a  v  a 2  s . c o  m*/
}

From source file:com.pinterest.deployservice.db.DatabaseUtil.java

/**
 * Create a MySQL datasource./*from ww  w  .ja va2s  .  com*/
 *
 * @param url             the url of the DB.
 * @param user            the user name to connect to MySQL as.
 * @param passwd          the password for the corresponding MySQL user.
 * @param poolSize        the connection pool size string, in the format of
 *                        initialSize:maxActive:maxIdle:minIdle.
 * @param maxWaitInMillis the max wait time in milliseconds to get a connection from the pool.
 * @return a BasicDataSource for the target MySQL instance.
 */
public static BasicDataSource createDataSource(String driverClassName, String url, String user, String passwd,
        String poolSize, int maxWaitInMillis) {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(url);
    dataSource.setUsername(user);
    dataSource.setPassword(passwd);
    dataSource.setDefaultAutoCommit(true);
    dataSource.setDefaultReadOnly(false);

    // poolSize parsing, the poolsize string passed in the following format
    // initialSize:maxActive:maxIdle:minIdle
    String[] sizeStrs = poolSize.split(":");
    dataSource.setInitialSize(Integer.parseInt(sizeStrs[0]));
    dataSource.setMaxActive(Integer.parseInt(sizeStrs[1]));
    dataSource.setMaxIdle(Integer.parseInt(sizeStrs[2]));
    dataSource.setMinIdle(Integer.parseInt(sizeStrs[3]));

    dataSource.setValidationQuery("SELECT 1");
    dataSource.setTestOnBorrow(true);
    dataSource.setTestOnReturn(false);
    dataSource.setTestWhileIdle(true);
    dataSource.setMinEvictableIdleTimeMillis(5 * 60 * 1000);
    dataSource.setTimeBetweenEvictionRunsMillis(3 * 60 * 1000);
    // dataSource.setNumTestsPerEvictionRun(3);
    // max wait in milliseconds for a connection.
    dataSource.setMaxWait(maxWaitInMillis);

    // 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 db connection when creating DataSource, url = %s", url), e);
    } finally {
        DbUtils.closeQuietly(conn);
    }
    return dataSource;
}

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/*from ww w  .  j  ava  2s.c  om*/
            .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.weibo.datasys.common.db.DBManager.java

/**
 * /* w  w w . j av a2 s . c om*/
 * ?????
 * 
 * @param configData
 */
private static void initDataSource(CommonData configData) {
    String dsname = configData.getBaseField("dsname");

    BasicDataSource dataSource = new BasicDataSource();
    // ??
    dataSource.setDriverClassName(configData.getBaseField("driverClassName"));
    // ??
    dataSource.setUrl(configData.getBaseField("connectURL"));
    // ???
    dataSource.setUsername(configData.getBaseField("username"));
    dataSource.setPassword(configData.getBaseField("password"));
    // ?
    dataSource.setInitialSize(StringUtils.parseInt(configData.getBaseField("initialSize"), 1));
    // ?
    dataSource.setMinIdle(StringUtils.parseInt(configData.getBaseField("minIdle"), 1));
    // 
    dataSource.setMaxIdle(StringUtils.parseInt(configData.getBaseField("maxIdle"), 1));
    // 
    dataSource.setMaxActive(StringUtils.parseInt(configData.getBaseField("maxActive"), 1));
    // ?,?(ms)
    dataSource.setMaxWait(StringUtils.parseInt(configData.getBaseField("maxWait"), 1000));

    // ??
    dataSource.setTestWhileIdle(true);
    // ?sql?
    dataSource.setValidationQuery("select 'test'");
    // ?
    dataSource.setValidationQueryTimeout(5000);
    // 
    dataSource.setTimeBetweenEvictionRunsMillis(3600000);
    // ??
    dataSource.setMinEvictableIdleTimeMillis(3600000);

    dsMap.put(dsname, dataSource);

    logger.info("[InitDataSourceOK] - dsname={}", dsname);
}

From source file:jp.go.nict.langrid.serviceexecutor.db.ConnectionManager.java

private void initWithBasicDataSource(String driverClassName, String connectionUrl, String userName,
        String password, int maxActive, int maxIdle, int maxWait, int maxPSActive) {
    BasicDataSource bds = new BasicDataSource();
    bds.setDriverClassName(driverClassName);
    bds.setUrl(connectionUrl);// w  w w  . jav a 2 s. com
    bds.setUsername(userName);
    bds.setPassword(password);
    bds.setMaxActive(maxActive);
    bds.setMaxIdle(maxIdle);
    bds.setMaxWait(maxWait);
    if (maxPSActive != -1) {
        bds.setMaxOpenPreparedStatements(maxPSActive);
    }
    this.dataSource = bds;
}

From source file:com.openteach.diamond.repository.client.impl.database.DataSourceFactory.java

/**
 * //  w ww  .  jav a2s .c o  m
 * @return
 */
public DataSource newInstance() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDefaultAutoCommit(true);
    dataSource.setDriverClassName(certificate.getDriverClassName());
    dataSource.setMaxActive(certificate.getMaxActive());
    dataSource.setMaxIdle(certificate.getMaxIdle());
    dataSource.setMaxWait(certificate.getMaxWait());
    dataSource.setMinIdle(certificate.getMinIdle());
    dataSource.setUsername(certificate.getUsername());
    dataSource.setPassword(certificate.getPassword());
    dataSource.setUrl(certificate.getUrl());
    return dataSource;
}

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

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

    dataSource.setMaxActive(maxActive);/*w  ww  .j  ava  2 s .  c  o  m*/
    dataSource.setMaxIdle(maxIdle);
    dataSource.setMaxWait(maxWait);
    dataSource.setPoolPreparedStatements(preparedStatementCache);
    dataSource.setMaxOpenPreparedStatements(preparedStatementCacheSize);
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setConnectionProperties(properties);

    //        printAV_INFO(dataSource);

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