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

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

Introduction

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

Prototype

public synchronized void setMaxIdle(int maxIdle) 

Source Link

Document

Sets the maximum number of connections that can remail idle in the pool.

Usage

From source file:com.weibo.datasys.common.db.DBManager.java

/**
 * /*w ww  .  ja  va 2s .  c o m*/
 * ?????
 * 
 * @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:com.alibaba.druid.pool.bonecp.TestLRU.java

public void f_test_dbcp() throws Exception {
    BasicDataSource ds = new BasicDataSource();
    ds.setUrl("jdbc:mock:test");
    ds.setMaxIdle(10);

    for (int i = 0; i < 10; ++i) {
        f(ds, 5);//from   w  w  w.j av a2s .c  o  m
        System.out.println("--------------------------------------------");
    }
}

From source file:com.alibaba.druid.pool.bonecp.TestPSCache.java

public void f_test_dbcp() throws Exception {
    BasicDataSource ds = new BasicDataSource();
    ds.setUrl("jdbc:mock:test");
    ds.setMaxIdle(10);
    ds.setPoolPreparedStatements(true);//from  ww  w . j av a 2 s . c o m
    ds.setMaxOpenPreparedStatements(10);

    for (int i = 0; i < 10; ++i) {
        f(ds, 5);
        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);/*from  ww w.  j  a  v a  2 s. co  m*/

    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 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 ww  . j  a  v a2 s.  com
    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:edu.tamu.tcat.oss.account.test.mock.MockDataSource.java

public void activate() {
    try {/* www .j a  v  a  2s. c o  m*/

        String url = props.getPropertyValue(PROP_URL, String.class);
        String user = props.getPropertyValue(PROP_USER, String.class);
        String pass = props.getPropertyValue(PROP_PASS, String.class);

        Objects.requireNonNull(url, "Database connection URL not supplied");
        Objects.requireNonNull(user, "Database username not supplied");
        Objects.requireNonNull(pass, "Database password not supplied");

        int maxActive = getIntValue(props, PROP_MAX_ACTIVE, 30);
        int maxIdle = getIntValue(props, PROP_MAX_IDLE, 3);
        int minIdle = getIntValue(props, PROP_MIN_IDLE, 0);
        int minEviction = getIntValue(props, PROP_MIN_EVICTION, 10 * 1000);
        int betweenEviction = getIntValue(props, PROP_BETWEEN_EVICTION, 100);

        PostgreSqlDataSourceFactory factory = new PostgreSqlDataSourceFactory();
        PostgreSqlPropertiesBuilder builder = factory.getPropertiesBuilder().create(url, user, pass);
        dataSource = factory.getDataSource(builder.getProperties());

        //HACK: should add this API to the properties builder instead of downcasting and overriding
        {
            BasicDataSource basic = (BasicDataSource) dataSource;

            basic.setMaxActive(maxActive);
            basic.setMaxIdle(maxIdle);
            basic.setMinIdle(minIdle);
            basic.setMinEvictableIdleTimeMillis(minEviction);
            basic.setTimeBetweenEvictionRunsMillis(betweenEviction);
        }

        this.executor = Executors.newSingleThreadExecutor();
    } catch (Exception e) {
        throw new IllegalStateException("Failed initializing data source", e);
    }
}

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

/**
 * //from w ww . j  a v a2  s .c om
 * @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.DBCPTest.java

public void test_max() throws Exception {
    Class.forName("com.alibaba.druid.mock.MockDriver");

    final BasicDataSource dataSource = new BasicDataSource();
    //        final DruidDataSource dataSource = new DruidDataSource();
    dataSource.setInitialSize(3);//from  ww w.  j  av a2s.com
    dataSource.setMaxActive(20);
    dataSource.setMaxIdle(20);
    dataSource.setDriverClassName("com.alibaba.druid.mock.MockDriver");
    dataSource.setUrl("jdbc:mock:xxx");

    final int THREAD_COUNT = 200;
    final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT);
    final CountDownLatch startLatch = new CountDownLatch(1);
    Thread[] threads = new Thread[THREAD_COUNT];
    for (int i = 0; i < THREAD_COUNT; ++i) {
        threads[i] = new Thread() {

            public void run() {
                try {
                    startLatch.await();
                    for (int i = 0; i < 1000; ++i) {
                        Connection conn = dataSource.getConnection();
                        Thread.sleep(1);
                        conn.close();
                    }
                } catch (Exception e) {
                } finally {
                    endLatch.countDown();
                }
            }
        };
        threads[i].start();
    }

    startLatch.countDown();

    endLatch.await();

    //        System.out.println(dataSource.getNumIdle());
    System.out.println(MockDriver.instance.getConnections().size());
    System.out.println(MockDriver.instance.getConnectionCloseCount());
}

From source file:blueprint.sdk.experimental.florist.db.ConnectionListener.java

public void contextInitialized(final ServletContextEvent arg0) {
    Properties poolProp = new Properties();
    try {/*  w  w w .j  av  a  2s  . c o m*/
        Context initContext = new InitialContext();

        // load pool properties file (from class path)
        poolProp.load(ConnectionListener.class.getResourceAsStream("/jdbc_pools.properties"));
        StringTokenizer stk = new StringTokenizer(poolProp.getProperty("PROP_LIST"));

        // process all properties files list in pool properties (from class
        // path)
        while (stk.hasMoreTokens()) {
            try {
                String propName = stk.nextToken();
                LOGGER.info(this, "loading jdbc properties - " + propName);

                Properties prop = new Properties();
                prop.load(ConnectionListener.class.getResourceAsStream(propName));

                DataSource dsr;
                if (prop.containsKey("JNDI_NAME")) {
                    // lookup DataSource from JNDI
                    // FIXME JNDI support is not tested yet. SPI or Factory
                    // is needed here.
                    LOGGER.warn(this, "JNDI DataSource support needs more hands on!");
                    dsr = (DataSource) initContext.lookup(prop.getProperty("JNDI_NAME"));
                } else {
                    // create new BasicDataSource
                    BasicDataSource bds = new BasicDataSource();
                    bds.setMaxActive(Integer.parseInt(prop.getProperty("MAX_ACTIVE")));
                    bds.setMaxIdle(Integer.parseInt(prop.getProperty("MAX_IDLE")));
                    bds.setMaxWait(Integer.parseInt(prop.getProperty("MAX_WAIT")));
                    bds.setInitialSize(Integer.parseInt(prop.getProperty("INITIAL")));
                    bds.setDriverClassName(prop.getProperty("CLASS_NAME"));
                    bds.setUrl(prop.getProperty("URL"));
                    bds.setUsername(prop.getProperty("USER"));
                    bds.setPassword(prop.getProperty("PASSWORD"));
                    bds.setValidationQuery(prop.getProperty("VALIDATION"));
                    dsr = bds;
                }
                boundDsrs.put(prop.getProperty("POOL_NAME"), dsr);
                initContext.bind(prop.getProperty("POOL_NAME"), dsr);
            } catch (RuntimeException e) {
                LOGGER.trace(e);
            }
        }
    } catch (IOException | NamingException e) {
        LOGGER.trace(e);
    }
}

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);/*from  w  w  w  .  j ava  2  s  .c  o m*/
    bds.setUsername(userName);
    bds.setPassword(password);
    bds.setMaxActive(maxActive);
    bds.setMaxIdle(maxIdle);
    bds.setMaxWait(maxWait);
    if (maxPSActive != -1) {
        bds.setMaxOpenPreparedStatements(maxPSActive);
    }
    this.dataSource = bds;
}