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

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

Introduction

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

Prototype

public synchronized int getMaxActive() 

Source Link

Document

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

Usage

From source file:org.apache.cayenne.configuration.server.DBCPDataSourceFactoryTest.java

@Test
public void testGetDataSource_LegacyConfig() throws Exception {

    String baseUrl = getClass().getPackage().getName().replace('.', '/');
    URL url = getClass().getClassLoader().getResource(baseUrl + "/");
    assertNotNull(url);// w  ww .ja v a 2  s  .  co  m

    DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
    nodeDescriptor.setConfigurationSource(new URLResource(url));
    nodeDescriptor.setParameters("testDBCP_legacy.properties");

    DBCPDataSourceFactory factory = new DBCPDataSourceFactory();
    DataSource dataSource = factory.getDataSource(nodeDescriptor);
    assertNotNull(dataSource);

    assertTrue(dataSource instanceof BasicDataSource);
    BasicDataSource basicDataSource = (BasicDataSource) dataSource;
    assertEquals("com.example.jdbc.Driver", basicDataSource.getDriverClassName());
    assertEquals("jdbc:somedb://localhost/cayenne", basicDataSource.getUrl());
    assertEquals("john", basicDataSource.getUsername());
    assertEquals("secret", basicDataSource.getPassword());
    assertEquals(20, basicDataSource.getMaxActive());
    assertEquals(5, basicDataSource.getMinIdle());
    assertEquals(8, basicDataSource.getMaxIdle());
    assertEquals(10000, basicDataSource.getMaxWait());
    assertEquals("select 1 from xyz;", basicDataSource.getValidationQuery());
}

From source file:org.apache.jackrabbit.core.util.db.ConnectionFactoryTest.java

private void assertPoolDefaults(BasicDataSource ds, String validationQuery, int maxCons) {
    assertEquals(maxCons, ds.getMaxActive());
    assertEquals(validationQuery, ds.getValidationQuery());
    assertTrue(ds.getDefaultAutoCommit());
    assertFalse(ds.getTestOnBorrow());/*www . j  a v a 2  s  .  c o  m*/
    assertTrue(ds.getTestWhileIdle());
    assertEquals(600000, ds.getTimeBetweenEvictionRunsMillis());
    assertTrue(ds.isPoolPreparedStatements());
    assertEquals(-1, ds.getMaxOpenPreparedStatements());
}

From source file:org.fao.geonet.api.site.SiteInformation.java

/**
 * Compute information about database.//  ww w.  j a  va2  s .  c  o  m
 */
private void loadDatabaseInfo(ServiceContext context) throws SQLException {
    String dbURL = null;

    Connection connection = null;
    try {
        connection = context.getBean(DataSource.class).getConnection();
        dbURL = connection.getMetaData().getURL();
        databaseProperties.put("db.openattempt", "Database Opened Successfully");

        if (connection instanceof BasicDataSource) {
            BasicDataSource basicDataSource = (BasicDataSource) connection;
            try {
                databaseProperties.put("db.numactive", "" + basicDataSource.getNumActive());
                databaseProperties.put("db.numidle", "" + basicDataSource.getNumIdle());
                databaseProperties.put("db.maxactive", "" + basicDataSource.getMaxActive());
            } catch (Exception e) {
                databaseProperties.put("db.statserror",
                        "Failed to get stats on database connections. Error is: " + e.getMessage());
            }
        }

    } catch (Exception e) {
        databaseProperties.put("db.openattempt",
                "Failed to open database connection, Check config.xml db file configuration. Error" + " is: "
                        + e.getMessage());
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
    databaseProperties.put("db.url", dbURL);
}

From source file:org.gvsig.fmap.dal.store.jdbc.JDBCResource.java

private void debugPoolStatus(String src) {
    if (logger.isDebugEnabled() && dataSource instanceof BasicDataSource) {
        BasicDataSource ds = (BasicDataSource) dataSource;
        logger.debug(src + "  actives:" + ds.getNumActive() + "(" + ds.getMaxActive() + ") idle:"
                + ds.getNumIdle() + "(" + ds.getMaxIdle() + ")");
    }/* ww w. java 2 s.  co  m*/

}

From source file:org.kuali.rice.web.health.DatabaseConnectionPoolMetricSet.java

private void installDBCPMetrics(final BasicDataSource dataSource, Map<String, Metric> metrics) {
    metrics.put(namePrefix + ACTIVE, new Gauge<Integer>() {
        @Override//from   w w w.j av a 2 s .  com
        public Integer getValue() {
            return dataSource.getNumActive();
        }
    });
    metrics.put(namePrefix + MIN, new Gauge<Integer>() {
        @Override
        public Integer getValue() {
            return dataSource.getMinIdle();
        }
    });
    metrics.put(namePrefix + MAX, new Gauge<Integer>() {
        @Override
        public Integer getValue() {
            return dataSource.getMaxActive();
        }
    });
    metrics.put(namePrefix + USAGE, new RatioGauge() {
        @Override
        protected Ratio getRatio() {
            return Ratio.of(dataSource.getNumActive(), dataSource.getMaxActive());
        }
    });
}

From source file:org.kuali.rice.web.health.DatabaseConnectionPoolMetricSetTest.java

@Test
public void testGetMetrics_DBCP() throws Exception {
    BasicDataSource dataSource = mock(BasicDataSource.class);
    when(dataSource.getNumActive()).thenReturn(10);
    when(dataSource.getMinIdle()).thenReturn(5);
    when(dataSource.getMaxActive()).thenReturn(20);

    DatabaseConnectionPoolMetricSet metricSet = new DatabaseConnectionPoolMetricSet("dbcp:", dataSource);
    Map<String, Metric> metrics = metricSet.getMetrics();
    assertEquals(10, ((Gauge) metrics.get("dbcp:pool.active")).getValue());
    assertEquals(5, ((Gauge) metrics.get("dbcp:pool.min")).getValue());
    assertEquals(20, ((Gauge) metrics.get("dbcp:pool.max")).getValue());
    assertEquals(0.5, ((Gauge) metrics.get("dbcp:pool.usage")).getValue());
}

From source file:org.mybatis.guice.datasource.dbcp.BasicDataSourceProviderTest.java

@Test
public void get() throws Throwable {
    final String driver = "org.mybatis.guice.TestDriver";
    final String url = "jdbc:h2:mem:testdb";
    final String username = "test_user";
    final String password = "test_password";
    final boolean autoCommit = true;
    final Properties driverProperties = new Properties();
    driverProperties.put("my_property", "true");
    final boolean accessToUnderlyingConnectionAllowed = true;
    final String defaultCatalog = "test_catalog";
    final boolean defaultReadOnly = true;
    final int defaultTransactionIsolation = 20;
    final int initialSize = 30;
    final int maxActive = 40;
    final int maxIdle = 50;
    final int maxOpenPreparedStatements = 60;
    final long maxWait = 70;
    final int minIdle = 80;
    final int numTestsPerEvictionRun = 90;
    final boolean poolPreparedStatements = true;
    final boolean testOnBorrow = true;
    final boolean testOnReturn = true;
    final boolean testWhileIdle = true;
    final int timeBetweenEvictionRunsMillis = 100;
    final String validationQuery = "SELECT 1";
    Injector injector = Guice.createInjector(new AbstractModule() {
        @Override/*from  ww w  .ja  va  2 s .  c  om*/
        protected void configure() {
            bind(ClassLoader.class).annotatedWith(Names.named("JDBC.driverClassLoader"))
                    .toInstance(driverClassLoader);
            bindConstant().annotatedWith(Names.named("JDBC.driver")).to(driver);
            bindConstant().annotatedWith(Names.named("JDBC.url")).to(url);
            bindConstant().annotatedWith(Names.named("JDBC.username")).to(username);
            bindConstant().annotatedWith(Names.named("JDBC.password")).to(password);
            bindConstant().annotatedWith(Names.named("JDBC.autoCommit")).to(autoCommit);
            bind(Properties.class).annotatedWith(Names.named("JDBC.driverProperties"))
                    .toInstance(driverProperties);
            bindConstant().annotatedWith(Names.named("DBCP.accessToUnderlyingConnectionAllowed"))
                    .to(accessToUnderlyingConnectionAllowed);
            bindConstant().annotatedWith(Names.named("DBCP.defaultCatalog")).to(defaultCatalog);
            bindConstant().annotatedWith(Names.named("DBCP.defaultReadOnly")).to(defaultReadOnly);
            bindConstant().annotatedWith(Names.named("DBCP.defaultTransactionIsolation"))
                    .to(defaultTransactionIsolation);
            bindConstant().annotatedWith(Names.named("DBCP.initialSize")).to(initialSize);
            bindConstant().annotatedWith(Names.named("DBCP.maxActive")).to(maxActive);
            bindConstant().annotatedWith(Names.named("DBCP.maxIdle")).to(maxIdle);
            bindConstant().annotatedWith(Names.named("DBCP.maxOpenPreparedStatements"))
                    .to(maxOpenPreparedStatements);
            bindConstant().annotatedWith(Names.named("DBCP.maxWait")).to(maxWait);
            bindConstant().annotatedWith(Names.named("DBCP.minIdle")).to(minIdle);
            bindConstant().annotatedWith(Names.named("DBCP.numTestsPerEvictionRun")).to(numTestsPerEvictionRun);
            bindConstant().annotatedWith(Names.named("DBCP.poolPreparedStatements")).to(poolPreparedStatements);
            bindConstant().annotatedWith(Names.named("DBCP.testOnBorrow")).to(testOnBorrow);
            bindConstant().annotatedWith(Names.named("DBCP.testOnReturn")).to(testOnReturn);
            bindConstant().annotatedWith(Names.named("DBCP.testWhileIdle")).to(testWhileIdle);
            bindConstant().annotatedWith(Names.named("DBCP.timeBetweenEvictionRunsMillis"))
                    .to(timeBetweenEvictionRunsMillis);
            bindConstant().annotatedWith(Names.named("DBCP.validationQuery")).to(validationQuery);
        }
    });
    BasicDataSourceProvider provider = injector.getInstance(BasicDataSourceProvider.class);

    BasicDataSource dataSource = (BasicDataSource) provider.get();

    assertEquals(driverClassLoader, dataSource.getDriverClassLoader());
    assertEquals(driver, dataSource.getDriverClassName());
    assertEquals(url, dataSource.getUrl());
    assertEquals(username, dataSource.getUsername());
    assertEquals(password, dataSource.getPassword());
    assertEquals(autoCommit, dataSource.getDefaultAutoCommit());
    // Cannot test driver properties.
    assertEquals(accessToUnderlyingConnectionAllowed, dataSource.isAccessToUnderlyingConnectionAllowed());
    assertEquals(defaultCatalog, dataSource.getDefaultCatalog());
    assertEquals(defaultReadOnly, dataSource.getDefaultReadOnly());
    assertEquals(defaultTransactionIsolation, dataSource.getDefaultTransactionIsolation());
    assertEquals(initialSize, dataSource.getInitialSize());
    assertEquals(maxActive, dataSource.getMaxActive());
    assertEquals(maxIdle, dataSource.getMaxIdle());
    assertEquals(maxOpenPreparedStatements, dataSource.getMaxOpenPreparedStatements());
    assertEquals(maxWait, dataSource.getMaxWait());
    assertEquals(minIdle, dataSource.getMinIdle());
    assertEquals(numTestsPerEvictionRun, dataSource.getNumTestsPerEvictionRun());
    assertEquals(poolPreparedStatements, dataSource.isPoolPreparedStatements());
    assertEquals(testOnBorrow, dataSource.getTestOnBorrow());
    assertEquals(testOnReturn, dataSource.getTestOnReturn());
    assertEquals(testWhileIdle, dataSource.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, dataSource.getTimeBetweenEvictionRunsMillis());
    assertEquals(validationQuery, dataSource.getValidationQuery());
}

From source file:org.mybatis.guice.datasource.dbcp.BasicDataSourceProviderTest.java

@Test
public void get_OtherValues() throws Throwable {
    final String driver = "org.mybatis.guice.TestDriver2";
    final String url = "jdbc:h2:mem:testdb2";
    final String username = "test_user2";
    final String password = "test_password2";
    final boolean autoCommit = false;
    final Properties driverProperties = new Properties();
    driverProperties.put("my_property", "false");
    final boolean accessToUnderlyingConnectionAllowed = false;
    final String defaultCatalog = "test_catalog2";
    final boolean defaultReadOnly = false;
    final int defaultTransactionIsolation = 21;
    final int initialSize = 31;
    final int maxActive = 41;
    final int maxIdle = 51;
    final int maxOpenPreparedStatements = 61;
    final long maxWait = 71;
    final int minIdle = 81;
    final int numTestsPerEvictionRun = 91;
    final boolean poolPreparedStatements = false;
    final boolean testOnBorrow = false;
    final boolean testOnReturn = false;
    final boolean testWhileIdle = false;
    final int timeBetweenEvictionRunsMillis = 101;
    final String validationQuery = "SELECT 2";
    Injector injector = Guice.createInjector(new AbstractModule() {
        @Override/*from  w w  w  . ja v  a 2 s . c o  m*/
        protected void configure() {
            bind(ClassLoader.class).annotatedWith(Names.named("JDBC.driverClassLoader"))
                    .toInstance(driverClassLoader);
            bindConstant().annotatedWith(Names.named("JDBC.driver")).to(driver);
            bindConstant().annotatedWith(Names.named("JDBC.url")).to(url);
            bindConstant().annotatedWith(Names.named("JDBC.username")).to(username);
            bindConstant().annotatedWith(Names.named("JDBC.password")).to(password);
            bindConstant().annotatedWith(Names.named("JDBC.autoCommit")).to(autoCommit);
            bind(Properties.class).annotatedWith(Names.named("JDBC.driverProperties"))
                    .toInstance(driverProperties);
            bindConstant().annotatedWith(Names.named("DBCP.accessToUnderlyingConnectionAllowed"))
                    .to(accessToUnderlyingConnectionAllowed);
            bindConstant().annotatedWith(Names.named("DBCP.defaultCatalog")).to(defaultCatalog);
            bindConstant().annotatedWith(Names.named("DBCP.defaultReadOnly")).to(defaultReadOnly);
            bindConstant().annotatedWith(Names.named("DBCP.defaultTransactionIsolation"))
                    .to(defaultTransactionIsolation);
            bindConstant().annotatedWith(Names.named("DBCP.initialSize")).to(initialSize);
            bindConstant().annotatedWith(Names.named("DBCP.maxActive")).to(maxActive);
            bindConstant().annotatedWith(Names.named("DBCP.maxIdle")).to(maxIdle);
            bindConstant().annotatedWith(Names.named("DBCP.maxOpenPreparedStatements"))
                    .to(maxOpenPreparedStatements);
            bindConstant().annotatedWith(Names.named("DBCP.maxWait")).to(maxWait);
            bindConstant().annotatedWith(Names.named("DBCP.minIdle")).to(minIdle);
            bindConstant().annotatedWith(Names.named("DBCP.numTestsPerEvictionRun")).to(numTestsPerEvictionRun);
            bindConstant().annotatedWith(Names.named("DBCP.poolPreparedStatements")).to(poolPreparedStatements);
            bindConstant().annotatedWith(Names.named("DBCP.testOnBorrow")).to(testOnBorrow);
            bindConstant().annotatedWith(Names.named("DBCP.testOnReturn")).to(testOnReturn);
            bindConstant().annotatedWith(Names.named("DBCP.testWhileIdle")).to(testWhileIdle);
            bindConstant().annotatedWith(Names.named("DBCP.timeBetweenEvictionRunsMillis"))
                    .to(timeBetweenEvictionRunsMillis);
            bindConstant().annotatedWith(Names.named("DBCP.validationQuery")).to(validationQuery);
        }
    });
    BasicDataSourceProvider provider = injector.getInstance(BasicDataSourceProvider.class);

    BasicDataSource dataSource = (BasicDataSource) provider.get();

    assertEquals(driverClassLoader, dataSource.getDriverClassLoader());
    assertEquals(driver, dataSource.getDriverClassName());
    assertEquals(url, dataSource.getUrl());
    assertEquals(username, dataSource.getUsername());
    assertEquals(password, dataSource.getPassword());
    assertEquals(autoCommit, dataSource.getDefaultAutoCommit());
    // Cannot test driver properties.
    assertEquals(accessToUnderlyingConnectionAllowed, dataSource.isAccessToUnderlyingConnectionAllowed());
    assertEquals(defaultCatalog, dataSource.getDefaultCatalog());
    assertEquals(defaultReadOnly, dataSource.getDefaultReadOnly());
    assertEquals(defaultTransactionIsolation, dataSource.getDefaultTransactionIsolation());
    assertEquals(initialSize, dataSource.getInitialSize());
    assertEquals(maxActive, dataSource.getMaxActive());
    assertEquals(maxIdle, dataSource.getMaxIdle());
    assertEquals(maxOpenPreparedStatements, dataSource.getMaxOpenPreparedStatements());
    assertEquals(maxWait, dataSource.getMaxWait());
    assertEquals(minIdle, dataSource.getMinIdle());
    assertEquals(numTestsPerEvictionRun, dataSource.getNumTestsPerEvictionRun());
    assertEquals(poolPreparedStatements, dataSource.isPoolPreparedStatements());
    assertEquals(testOnBorrow, dataSource.getTestOnBorrow());
    assertEquals(testOnReturn, dataSource.getTestOnReturn());
    assertEquals(testWhileIdle, dataSource.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, dataSource.getTimeBetweenEvictionRunsMillis());
    assertEquals(validationQuery, dataSource.getValidationQuery());
}

From source file:org.openkoala.koala.monitor.support.DbcpDataSourceCollector.java

public synchronized JdbcPoolStatusVo currentPoolStatus() {
    JdbcPoolStatusVo poolStatus = new JdbcPoolStatusVo();
    try {//from   w  w w.j  a  va2s.co  m
        BasicDataSource realDS = (BasicDataSource) dataSoure;

        poolStatus.setDriverName(realDS.getDriverClassName());
        //         poolStatus.setStartTime();
        poolStatus.setProvider(dataSoure.getClass().getName());
        poolStatus.setInitConnectionCount(realDS.getInitialSize());
        poolStatus.setMaxConnectionCount(realDS.getMaxActive());
        poolStatus.setMaxActiveTime(realDS.getMaxWait());

        poolStatus.setMaxOpenStatements(realDS.getMaxOpenPreparedStatements());
        poolStatus.setSnapshotTime(new Date());
        poolStatus.setIdleConnectionCount(realDS.getNumIdle());
        poolStatus.setActiveConnectionCount(realDS.getNumActive());
    } catch (Exception e) {
        poolStatus.setErrorTip("??");
    }

    return poolStatus;
}

From source file:psiprobe.beans.DbcpDatasourceAccessor.java

@Override
public DataSourceInfo getInfo(Object resource) throws Exception {
    DataSourceInfo dataSourceInfo = null;
    if (canMap(resource)) {
        BasicDataSource source = (BasicDataSource) resource;
        dataSourceInfo = new DataSourceInfo();
        dataSourceInfo.setBusyConnections(source.getNumActive());
        dataSourceInfo.setEstablishedConnections(source.getNumIdle() + source.getNumActive());
        dataSourceInfo.setMaxConnections(source.getMaxActive());
        dataSourceInfo.setJdbcUrl(source.getUrl());
        dataSourceInfo.setUsername(source.getUsername());
        dataSourceInfo.setResettable(false);
        dataSourceInfo.setType("commons-dbcp");
    }//from   ww  w. j  a  v a 2 s. c  o m
    return dataSourceInfo;
}