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:org.wso2.carbon.registry.core.test.performance.BasicPerformanceTest.java

public void setUp() {
    //String connURL = "jdbc:log4jdbc:derby:target/REG1_DB";
    //String connURL = "jdbc:derby:target/REG1_DB";
    String connURL = "jdbc:mysql://localhost:3306/registry1";
    BasicDataSource ds = new BasicDataSource();
    //ds.setUrl(connURL + ";");
    //ds.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl(connURL);//from   www. j a  va2  s .  co m
    ds.setUsername("root");
    ds.setPassword("password");

    //        ds.setMaxWait(1000*60*2);

    ds.setMaxActive(150);
    ds.setMaxIdle(1000 * 60 * 2);
    ds.setMinIdle(5);
    //ds.setDriverClassName("net.sf.log4jdbc.DriverSpy");

    //DerbyDatabaseCreator creator = new DerbyDatabaseCreator(ds);
    DatabaseCreator creator = new DatabaseCreator(ds);
    try {
        creator.createRegistryDatabase();
    } catch (Exception e) {
        fail("Failed to create database. Caused by: " + e.getMessage());
    }
    //String fileName = "target/db/registry";
    //File file = new File(fileName);

    //if (! file.exists()) {
    //creator.createDefaultDatabaseTables();
    //}

    //        UserRealm realm = new DefaultRealm();
    //        DefaultRealmConfig config = (DefaultRealmConfig) realm.getBootstrapRealmConfiguration();
    //        config.setConnectionURL(connURL);
    //        realm.init(config);
    //        UserRealm registryRealm = new UserRealm(realm);
    //
    //        InputStream configStream =
    //            Thread.currentThread().getContextClassLoader().getResourceAsStream("registry.xml");
    //        RegistryContext regContext = new RegistryContext(configStream, registryRealm);
    //        embeddedRegistryService = new EmbeddedRegistryService(regContext);
    //        adminRegistry = embeddedRegistryService.getUserRegistry(
    //            RegistryConstants.ADMIN_USER, RegistryConstants.ADMIN_PASSWORD);
    System.out.println("~~~~~setup method done~~~~~");
}

From source file:qa.qcri.nadeef.core.util.sql.DBConnectionPool.java

/**
 * Create a connection pool.//from   w  ww .jav a  2 s  .co m
 * @param dbconfig input DB config.
 * @return connection pool instance.
 */
public BasicDataSource createConnectionPool(DBConfig dbconfig) {
    tracer.verbose("Creating connection pool for " + dbconfig.getUrl());
    BasicDataSource result;
    result = new BasicDataSource();
    result.setUrl(dbconfig.getUrl());
    result.setDriverClassName(SQLDialectTools.getDriverName(dbconfig.getDialect()));

    String username = dbconfig.getUserName();
    if (!Strings.isNullOrEmpty(username)) {
        result.setUsername(username);
    }

    String password = dbconfig.getPassword();
    if (!Strings.isNullOrEmpty(password)) {
        result.setPassword(password);
    }

    result.setMaxActive(MAX_ACTIVE);
    result.setMaxIdle(MAX_ACTIVE * 3);
    result.setDefaultAutoCommit(false);
    return result;
}

From source file:qa.qcri.nadeef.core.utils.sql.DBConnectionPool.java

/**
 * Create a connection pool./*www. j  a  va  2s .co m*/
 * @param dbconfig input DB config.
 * @return connection pool instance.
 */
public BasicDataSource createConnectionPool(DBConfig dbconfig) {
    tracer.fine("Creating connection pool for " + dbconfig.getUrl());
    BasicDataSource result;
    result = new BasicDataSource();
    result.setUrl(dbconfig.getUrl());
    result.setDriverClassName(SQLDialectTools.getDriverName(dbconfig.getDialect()));

    String username = dbconfig.getUserName();
    if (!Strings.isNullOrEmpty(username)) {
        result.setUsername(username);
    }

    String password = dbconfig.getPassword();
    if (!Strings.isNullOrEmpty(password)) {
        result.setPassword(password);
    }

    result.setMaxActive(MAX_ACTIVE);
    result.setMaxIdle(MAX_ACTIVE * 3);
    result.setDefaultAutoCommit(false);
    return result;
}

From source file:se.unlogic.hierarchy.core.utils.DBCPUtils.java

public static BasicDataSource createConnectionPool(DataSourceDescriptor dataSourceDescriptor) {

    BasicDataSource basicDataSource = new BasicDataSource();

    basicDataSource.setDriverClassName(dataSourceDescriptor.getDriver());
    basicDataSource.setUsername(dataSourceDescriptor.getUsername());
    basicDataSource.setPassword(dataSourceDescriptor.getPassword());
    basicDataSource.setUrl(dataSourceDescriptor.getUrl());
    basicDataSource.setDefaultCatalog(dataSourceDescriptor.getDefaultCatalog());
    basicDataSource.setLogAbandoned(dataSourceDescriptor.logAbandoned());
    basicDataSource.setRemoveAbandoned(dataSourceDescriptor.removeAbandoned());

    if (dataSourceDescriptor.getRemoveTimeout() != null) {
        basicDataSource.setRemoveAbandonedTimeout(dataSourceDescriptor.getRemoveTimeout());
    }//from  w  w w  .  ja va2  s .  c o m

    basicDataSource.setTestOnBorrow(dataSourceDescriptor.testOnBorrow());
    basicDataSource.setValidationQuery(dataSourceDescriptor.getValidationQuery());
    basicDataSource.setMaxWait(dataSourceDescriptor.getMaxWait());
    basicDataSource.setMaxActive(dataSourceDescriptor.getMaxActive());
    basicDataSource.setMaxIdle(dataSourceDescriptor.getMaxIdle());
    basicDataSource.setMinIdle(dataSourceDescriptor.getMinIdle());

    return basicDataSource;
}

From source file:test.com.agiletec.ConfigTestUtils.java

private void createDatasource(String dsNameControlKey, SimpleNamingContextBuilder builder,
        Properties testConfig) {/*from   w ww. ja v a2  s  . co m*/
    String beanName = testConfig.getProperty("jdbc." + dsNameControlKey + ".beanName");
    try {
        String className = testConfig.getProperty("jdbc." + dsNameControlKey + ".driverClassName");
        String url = testConfig.getProperty("jdbc." + dsNameControlKey + ".url");
        String username = testConfig.getProperty("jdbc." + dsNameControlKey + ".username");
        String password = testConfig.getProperty("jdbc." + dsNameControlKey + ".password");
        Class.forName(className);
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setMaxActive(8);
        ds.setMaxIdle(4);
        builder.bind("java:comp/env/jdbc/" + beanName, ds);
    } catch (Throwable t) {
        throw new RuntimeException("Error on creation datasource '" + beanName + "'", t);
    }
}

From source file:xbird.util.jdbc.datasource.DbcpDataSourceProvider.java

public DataSource setupDataSource(String connectURI) {
    // creates DataSource
    BasicDataSource ds = new BasicDataSource();

    // for debugging.
    if (Settings.isLoggingEnabled) {
        ds.setAccessToUnderlyingConnectionAllowed(true);
    }//from w w  w . ja v a  2 s.  c  o m

    ds.setDriverClassName(DriverClassNameResolver.resolve(Settings.get("xbird.db.kind")));

    // sets up DataSource
    ds.setUrl(connectURI);
    final String dbuser = Settings.get("xbird.db.user");
    final String dbpasswd = Settings.get("xbird.db.passwd");
    if (dbuser != null && dbuser.length() != 0) {
        ds.setUsername(dbuser);
        ds.setPassword(dbpasswd);
    }

    // addtinal settings.
    final String maxactive = Settings.get("xbird.db.pool.maxactive");
    if (maxactive != null)
        ds.setMaxActive(Integer.parseInt(maxactive));

    final String maxidle = Settings.get("xbird.db.pool.maxidle");
    if (maxidle != null)
        ds.setMaxIdle(Integer.parseInt(maxidle));

    final String maxwait = Settings.get("xbird.db.pool.maxwait");
    ds.setMaxWait(maxwait == null ? DEFAULT_MAXWAIT : Integer.parseInt(maxwait));

    ds.setDefaultAutoCommit(true);
    //ds.setDefaultReadOnly(false);

    final String initialsize = Settings.get("xbird.db.pool.initialsize");
    ds.setInitialSize(initialsize == null ? DEFAULT_INITIAL_POLLSIZE : Integer.parseInt(initialsize));

    // sets up for PreparedStatements.
    ds.setPoolPreparedStatements(true);

    final String maxOpenPreparedStatements = Settings.get("xbird.db.pool.statement.cache_size");
    ds.setMaxOpenPreparedStatements(maxOpenPreparedStatements == null ? MAX_OPEN_PREPARED_STATEMENTS
            : Integer.parseInt(maxOpenPreparedStatements));

    return ds;
}