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

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

Introduction

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

Prototype

public synchronized void setMaxActive(int maxActive) 

Source Link

Document

Sets the maximum number of active connections that can be allocated at the same time.

Usage

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);//from  w ww .j a  v a  2s.  c o  m
    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.alfaariss.oa.util.database.jdbc.DataSourceFactory.java

private static DataSource addOptionalSettings(IConfigurationManager configurationManager, Element eConfig,
        BasicDataSource dataSource) throws DatabaseException {
    try {/*w  ww.  j av a 2s  . co m*/
        String sMaxActive = configurationManager.getParam(eConfig, "maxactive");
        int iMaxActive = -1;
        if (sMaxActive != null) {
            try {
                iMaxActive = Integer.parseInt(sMaxActive);
            } catch (NumberFormatException e) {
                _logger.error("Wrong 'maxactive' item found in configuration: " + sMaxActive, e);
                throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ);
            }
        }
        dataSource.setMaxActive(iMaxActive);

        String sMaxIdle = configurationManager.getParam(eConfig, "maxidle");
        if (sMaxIdle != null) {
            int iMaxIdle = -1;
            try {
                iMaxIdle = Integer.parseInt(sMaxIdle);
            } catch (NumberFormatException e) {
                _logger.error("Wrong 'maxidle' item found in configuration: " + sMaxIdle, e);
                throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ);
            }
            dataSource.setMaxIdle(iMaxIdle);
        }

        String sMaxWait = configurationManager.getParam(eConfig, "maxwait");
        if (sMaxWait != null) {
            int iMaxWait = -1;
            try {
                iMaxWait = Integer.parseInt(sMaxWait);
            } catch (NumberFormatException e) {
                _logger.error("Wrong 'maxwait' item found in configuration: " + sMaxWait, e);
                throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ);
            }
            dataSource.setMaxWait(iMaxWait);
        }

        String sTestOnBorrow = configurationManager.getParam(eConfig, "testonborrow");
        if (sTestOnBorrow != null) {
            boolean bTestOnBorrow = false;
            if (sTestOnBorrow.equalsIgnoreCase("true"))
                bTestOnBorrow = true;
            else if (!sTestOnBorrow.equalsIgnoreCase("false")) {
                _logger.error("Wrong 'testonborrow' item found in configuration: " + sTestOnBorrow);
                throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ);
            }
            dataSource.setTestOnBorrow(bTestOnBorrow);
        }

        String sTestOnReturn = configurationManager.getParam(eConfig, "testonreturn");
        if (sTestOnReturn != null) {
            boolean bTestOnReturn = false;
            if (sTestOnReturn.equalsIgnoreCase("true"))
                bTestOnReturn = true;
            else if (!sTestOnReturn.equalsIgnoreCase("false")) {
                _logger.error("Wrong 'testonreturn' item found in configuration: " + sTestOnReturn);
                throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ);
            }
            dataSource.setTestOnReturn(bTestOnReturn);
        }

        String sTimeBetweenEvictionRunsMillis = configurationManager.getParam(eConfig,
                "timebetweenevictionrunsmillis");
        if (sTimeBetweenEvictionRunsMillis != null) {
            try {
                long lTimeBetweenEvictionRunsMillis = Long.parseLong(sTimeBetweenEvictionRunsMillis);
                dataSource.setTimeBetweenEvictionRunsMillis(lTimeBetweenEvictionRunsMillis);
            } catch (NumberFormatException e) {
                _logger.error("Wrong 'timebetweenevictionrunsmillis' item found in configuration: "
                        + sTimeBetweenEvictionRunsMillis, e);
                throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ);
            }
        }

        String sNumTestsPerEvictionRun = configurationManager.getParam(eConfig, "numtestsperevictionrun");
        if (sNumTestsPerEvictionRun != null) {
            int iNumTestsPerEvictionRun = -1;
            try {
                iNumTestsPerEvictionRun = Integer.parseInt(sNumTestsPerEvictionRun);
            } catch (NumberFormatException e) {
                _logger.error("Wrong 'numtestsperevictionrun' item found in configuration: "
                        + sNumTestsPerEvictionRun, e);
                throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ);
            }
            dataSource.setNumTestsPerEvictionRun(iNumTestsPerEvictionRun);
        }

        String sMinEvictableIdleTimeMillis = configurationManager.getParam(eConfig,
                "minevictableidletimemillis");
        if (sMinEvictableIdleTimeMillis != null) {
            try {
                long lMinEvictableIdleTimeMillis = Long.parseLong(sMinEvictableIdleTimeMillis);
                dataSource.setMinEvictableIdleTimeMillis(lMinEvictableIdleTimeMillis);
            } catch (NumberFormatException e) {
                _logger.error("Wrong 'minevictableidletimemillis' item found in configuration: "
                        + sMinEvictableIdleTimeMillis, e);
                throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ);
            }
        }

        String sTestWhileIdle = configurationManager.getParam(eConfig, "testwhileidle");
        if (sTestWhileIdle != null) {
            boolean bTestWhileIdle = false;
            if (sTestWhileIdle.equalsIgnoreCase("true"))
                bTestWhileIdle = true;
            else if (!sTestWhileIdle.equalsIgnoreCase("false")) {
                _logger.error("Wrong 'testwhileidle' item found in configuration: " + sTestWhileIdle);
                throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ);
            }
            dataSource.setTestWhileIdle(bTestWhileIdle);
        }

        String sValidationQuery = configurationManager.getParam(eConfig, "validationquery");
        if (sValidationQuery != null) {
            dataSource.setValidationQuery(sValidationQuery);
        }

    } catch (DatabaseException e) {
        throw e;
    } catch (Exception e) {
        _logger.fatal("Could not create datasource", e);
        throw new DatabaseException(SystemErrors.ERROR_INTERNAL);
    }
    return dataSource;
}

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  w w  w. j av  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.weibo.datasys.common.db.DBManager.java

/**
 * //from w  ww.  j  a v  a2s  .  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:com.alibaba.druid.benckmark.pool.Oracle_Case3.java

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

    dataSource.setMaxActive(maxActive);
    dataSource.setMaxIdle(maxIdle);//from   w  w  w .  j av  a 2  s  .c o m
    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 {//from   ww  w  .  j  a v  a 2 s . 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:blueprint.sdk.experimental.florist.db.ConnectionListener.java

public void contextInitialized(final ServletContextEvent arg0) {
    Properties poolProp = new Properties();
    try {// w w w.ja  va  2 s.  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:com.openteach.diamond.repository.client.impl.database.DataSourceFactory.java

/**
 * /*from   w ww .j av 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:general.conexion.Pool.java

public void inicializarDataSource() {
    BasicDataSource basic = new BasicDataSource();
    basic.setDriverClassName(driver);/*from  w  ww .j  a va2  s.co  m*/
    basic.setUsername(usuario);
    basic.setPassword(contrasena);
    basic.setUrl(url);
    basic.setMaxActive(5);
    this.dataSource = basic;
}

From source file:com.example.dbflute.spring.JdbcBeansJavaConfig.java

@Bean(name = { "dataSource" })
public DataSource createDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl(exampleDbUrl);// w  w w .j  a  v a2 s  . c o  m
    ds.setUsername("sa");
    ds.setPassword("");
    ds.setMaxActive(20);
    return ds;
}