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:org.shelloid.vpt.rms.util.Platform.java

private BasicDataSource configDbPool() {
    BasicDataSource ds = new BasicDataSource();
    ds.setTestOnBorrow(true);//from  w w  w. j  a  va 2 s  . c o  m
    ds.setValidationQuery("SELECT 1");
    ds.setDriverClassName(get(Configurations.ConfigParams.JDBC_DRIVER));
    ds.setUrl(get(Configurations.ConfigParams.JDBC_URL));
    ds.setUsername(get(Configurations.ConfigParams.JDBC_USERNAME));
    ds.setPassword(get(Configurations.ConfigParams.JDBC_PASSWORD));
    ds.setMaxActive(Integer.parseInt(get(Configurations.ConfigParams.JDBC_MAX_ACTIVE)));
    ds.setMaxIdle(Integer.parseInt(get(Configurations.ConfigParams.JDBC_MIN_IDLE)));
    return ds;
}

From source file:org.snaker.engine.access.jdbc.JdbcHelper.java

/**
 * dataSourcedbcp??//  w ww  .j  a v a 2 s .  c  o m
 */
private static void initialize() {
    String driver = ConfigHelper.getProperty("jdbc.driver");
    String url = ConfigHelper.getProperty("jdbc.url");
    String username = ConfigHelper.getProperty("jdbc.username");
    String password = ConfigHelper.getProperty("jdbc.password");
    int maxActive = ConfigHelper.getNumerProperty("jdbc.max.active");
    int maxIdle = ConfigHelper.getNumerProperty("jdbc.max.idle");
    AssertHelper.notNull(driver);
    AssertHelper.notNull(url);
    AssertHelper.notNull(username);
    AssertHelper.notNull(password);
    //?DBCP??
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(driver);
    ds.setUrl(url);
    ds.setUsername(username);
    ds.setPassword(password);
    if (maxActive != 0) {
        ds.setMaxActive(maxActive);
    }
    if (maxIdle != 0) {
        ds.setMaxIdle(maxIdle);
    }
    dataSource = ds;
}

From source file:org.springframework.boot.autoconfigure.jdbc.metadata.CommonsDbcpDataSourcePoolMetadataTests.java

private CommonsDbcpDataSourcePoolMetadata createDataSourceMetadata(int minSize, int maxSize) {
    BasicDataSource dataSource = createDataSource();
    dataSource.setMinIdle(minSize);//from w w w  . ja  va 2 s  . c  o  m
    dataSource.setMaxActive(maxSize);
    return new CommonsDbcpDataSourcePoolMetadata(dataSource);
}

From source file:org.tdmx.lib.control.datasource.DynamicDataSource.java

private synchronized void createDataSource(DatabaseConnectionInfo dci) throws SQLException {
    // race condition avoidance
    if (connectionDataSourceMap.get(dci) != null) {
        return;//from  w  w w  .ja va  2s. c o m
    }
    BasicDataSource bds = new BasicDataSource();
    bds.setUrl(dci.getUrl());
    bds.setDriverClassName(dci.getDriverClassname());
    bds.setUsername(dci.getUsername());
    bds.setPassword(dci.getPassword());
    bds.setMaxActive(100);
    bds.setMinIdle(0);
    bds.setInitialSize(1);
    bds.setMinEvictableIdleTimeMillis(60000);
    bds.setLogWriter(logWriter);
    connectionDataSourceMap.put(dci, bds);
}

From source file:org.wsm.database.tools.util.MysqlBaseLoadTestConnectionManager.java

public BasicDataSource getBasicDataSourceSetup() {
    BasicDataSource bds = super.setupBasicDataSourcePooling();
    bds.setMaxActive(150);
    bds.setMaxIdle(8);/*from   w  w  w.  ja  va  2 s  . c  om*/
    bds.setMaxWait(3000);
    return bds;
}

From source file:org.wso2.carbon.registry.core.jdbc.utils.RegistryDataSource.java

public RegistryDataSource(DataBaseConfiguration config) {

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setUrl(config.getDbUrl());
    basicDataSource.setDriverClassName(config.getDriverName());
    basicDataSource.setUsername(config.getUserName());
    basicDataSource.setPassword(config.getResolvedPassword());

    if (config.getMaxActive() != null) {
        basicDataSource.setMaxActive(Integer.parseInt(config.getMaxActive()));
    } else {//from  www . j a  v a  2  s.c  o m
        basicDataSource.setMaxActive(DEFAULT_MAX_ACTIVE);
    }

    if (config.getMaxWait() != null) {
        basicDataSource.setMaxWait(Integer.parseInt(config.getMaxWait()));
    } else {
        basicDataSource.setMaxWait(DEFAULT_MAX_WAIT);
    }

    if (config.getMaxIdle() != null) {
        basicDataSource.setMaxIdle(Integer.parseInt(config.getMaxIdle()));
    }

    if (config.getMinIdle() != null) {
        basicDataSource.setMinIdle(Integer.parseInt(config.getMinIdle()));
    } else {
        basicDataSource.setMinIdle(DEFAULT_MIN_IDLE);
    }

    this.dataSource = basicDataSource;
}

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  w  w  w.  j a  v a 2s. c  om*/
    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:org.xbib.io.jdbc.SQLConnectionFactory.java

/**
 * Get connection//w w w .j ava2  s . com
 *
 * @param uri
 *
 * @return an SQL connection
 *
 * @throws java.io.IOException
 */
@Override
public Connection<SQLSession> getConnection(final URI uri) throws IOException {
    this.properties = URIUtil.getPropertiesFromURI(uri);
    Context context = null;
    DataSource ds = null;
    for (String name : new String[] { "jdbc/" + properties.getProperty("host"),
            "java:comp/env/" + properties.getProperty("scheme") + ":" + properties.getProperty("host") + ":"
                    + properties.getProperty("port") }) {
        this.jndiName = name;
        try {
            context = new InitialContext();
            Object o = context.lookup(jndiName);
            if (o instanceof DataSource) {
                logger.info("DataSource ''{}'' found in naming context", jndiName);
                ds = (DataSource) o;
                break;
            } else {
                logger.warn("JNDI object {} not a DataSource class: {} - ignored", jndiName, o.getClass());
            }
        } catch (NameNotFoundException e) {
            logger.warn("DataSource ''{}'' not found in context", jndiName);
        } catch (NamingException e) {
            logger.warn(e.getMessage(), e);
        }
    }
    try {
        if (ds == null) {
            BasicDataSource bsource = new BasicDataSource();
            bsource.setDriverClassName(properties.getProperty("driverClassName"));
            String url = properties.getProperty("jdbcScheme") + properties.getProperty("host") + ":"
                    + properties.getProperty("port")
                    + ("jdbc:oracle:thin:@".equals(properties.getProperty("jdbcScheme")) ? ":" : "/")
                    + properties.getProperty("cluster");
            bsource.setUrl(url);
            if (properties.containsKey("username")) {
                bsource.setUsername(properties.getProperty("username"));
            }
            if (properties.containsKey("password")) {
                bsource.setPassword(properties.getProperty("password"));
            }
            if (properties.containsKey("n")) {
                bsource.setInitialSize(Integer.parseInt(properties.getProperty("n")));
                bsource.setMaxActive(Integer.parseInt(properties.getProperty("n")));
            }
            // Other BasicDataSource settings, not used yet:
            //  setAccessToUnderlyingConnectionAllowed(boolean allow)
            //  setDefaultAutoCommit(boolean defaultAutoCommit)
            //  setDefaultCatalog(String defaultCatalog)
            //  setDefaultReadOnly(boolean defaultReadOnly)
            //  setDefaultTransactionIsolation(int defaultTransactionIsolation)
            //  setLogAbandoned(boolean logAbandoned)
            //  setLoginTimeout(int loginTimeout)
            //  setLogWriter(PrintWriter logWriter)
            //  setMaxIdle(int maxIdle)
            //  setMaxOpenPreparedStatements(int maxOpenStatements)
            //  setMaxWait(long maxWait)
            //  setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
            //  setMinIdle(int minIdle)
            //  setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
            //  setPoolPreparedStatements(boolean poolingStatements)
            //  setTestOnBorrow(boolean testOnBorrow)
            //  setTestOnReturn(boolean testOnReturn)
            //  setTestWhileIdle(boolean testWhileIdle)
            //  setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
            //  setValidationQuery(String validationQuery)
            context.bind(jndiName, bsource);
            ds = (DataSource) bsource;
        }
    } catch (NamingException e) {
        throw new IOException(e.getMessage());
    }
    try {
        ds.getConnection().setAutoCommit("false".equals(properties.getProperty("autoCommit")) ? false : true);
    } catch (SQLException e) {
        throw new IOException(e.getMessage());
    }
    return new SQLConnection(ds);
}

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

/**
 * Create a connection pool./*from  w  w w  .ja va 2  s.c  o 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.//from w  ww. ja v  a  2  s .c  om
 * @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;
}