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

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

Introduction

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

Prototype

public synchronized int getMinIdle() 

Source Link

Document

Returns the minimum number of idle connections in the pool

Usage

From source file:net.risesoft.soa.asf.web.controller.SystemController.java

private List<SysInfo> getDBInfo() {
    List list = new ArrayList();
    String group = "4. ??";
    try {/*from   w  w  w .ja v  a  2 s. c  o  m*/
        Connection conn = this.basicDataSource.getConnection();
        try {
            DatabaseMetaData dbmd = conn.getMetaData();
            list.add(new SysInfo("DatabaseProductName", dbmd.getDatabaseProductName(), group));
            list.add(new SysInfo("DatabaseProductVersion", dbmd.getDatabaseProductVersion(), group));
            list.add(new SysInfo("DatabaseMajorVersion", Integer.valueOf(dbmd.getDatabaseMajorVersion()),
                    group));
            list.add(new SysInfo("DatabaseMinorVersion", Integer.valueOf(dbmd.getDatabaseMinorVersion()),
                    group));
            list.add(new SysInfo("DriverName", dbmd.getDriverName(), group));
            list.add(new SysInfo("DriverVersion", dbmd.getDriverVersion(), group));
            list.add(new SysInfo("DriverMajorVersion", Integer.valueOf(dbmd.getDriverMajorVersion()), group));
            list.add(new SysInfo("DriverMinorVersion", Integer.valueOf(dbmd.getDriverMinorVersion()), group));
        } finally {
            conn.close();
        }
        group = "5. ?";
        BasicDataSource bds = this.basicDataSource;
        list.add(new SysInfo("InitialSize", Integer.valueOf(bds.getInitialSize()), group));
        list.add(new SysInfo("MaxActive", Integer.valueOf(bds.getMaxActive()), group));
        list.add(new SysInfo("MaxIdle", Integer.valueOf(bds.getMaxIdle()), group));
        list.add(new SysInfo("MinIdle", Integer.valueOf(bds.getMinIdle()), group));
        list.add(new SysInfo("MaxWait", Long.valueOf(bds.getMaxWait()), group));
        list.add(new SysInfo("NumActive", Integer.valueOf(bds.getNumActive()), group));
        list.add(new SysInfo("NumIdle", Integer.valueOf(bds.getNumIdle()), group));
        list.add(new SysInfo("DriverClass", bds.getDriverClassName(), group));
        list.add(new SysInfo("URL", bds.getUrl(), group));
        list.add(new SysInfo("Username", bds.getUsername(), group));
        list.add(new SysInfo("Password", "******", group));
    } catch (Exception ex) {
        log.warn("???: " + ex.getMessage(), ex);
    }
    return list;
}

From source file:fr.cnes.sitools.datasource.jdbc.DataSourceMonitoringResource.java

/**
 * Trace informations for a sitoolsDataSource
 * //from www  . ja v  a2  s.  com
 * @param sitoolsDataSource
 *          SitoolsDataSource
 * @param messages
 *          ArrayList<String> messages
 */
private void traceStatus(SitoolsSQLDataSource sitoolsDataSource, ArrayList<String> messages) {
    DataSource concreteDs = sitoolsDataSource.getDs();
    if ((concreteDs != null) && (concreteDs instanceof BasicDataSource)) {
        BasicDataSource bds = (BasicDataSource) concreteDs;
        messages.add("--------------------------------------------------");
        messages.add("Url: " + bds.getUrl());
        messages.add("User: " + bds.getUsername());
        messages.add("DefaultCatalog: " + bds.getDefaultCatalog());
        messages.add("InitialSize: " + bds.getInitialSize());
        messages.add("NumActive: " + bds.getNumActive());
        messages.add("MaxActive: " + bds.getMaxActive());
        messages.add("MaxIdl: " + bds.getMaxIdle());
        messages.add("MinIdl: " + bds.getMinIdle());
        messages.add("NumIdle: " + bds.getNumIdle());
    }

}

From source file:com.google.gerrit.server.schema.DataSourceProvider.java

private DataSource open(final SitePaths site, final Config cfg, final Context context,
        final DataSourceType dst) {
    ConfigSection dbs = new ConfigSection(cfg, "database");
    String driver = dbs.optional("driver");
    if (Strings.isNullOrEmpty(driver)) {
        driver = dst.getDriver();/*  w w w .  j  a va 2 s .c om*/
    }

    String url = dbs.optional("url");
    if (Strings.isNullOrEmpty(url)) {
        url = dst.getUrl();
    }

    String username = dbs.optional("username");
    String password = dbs.optional("password");

    boolean usePool;
    if (context == Context.SINGLE_USER) {
        usePool = false;
    } else {
        usePool = cfg.getBoolean("database", "connectionpool", dst.usePool());
    }

    if (usePool) {
        final BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        if (username != null && !username.isEmpty()) {
            ds.setUsername(username);
        }
        if (password != null && !password.isEmpty()) {
            ds.setPassword(password);
        }
        ds.setMaxActive(cfg.getInt("database", "poollimit", 8));
        ds.setMinIdle(cfg.getInt("database", "poolminidle", 4));
        ds.setMaxIdle(cfg.getInt("database", "poolmaxidle", 4));
        ds.setMaxWait(ConfigUtil.getTimeUnit(cfg, "database", null, "poolmaxwait",
                MILLISECONDS.convert(30, SECONDS), MILLISECONDS));
        ds.setInitialSize(ds.getMinIdle());
        return ds;

    } else {
        // Don't use the connection pool.
        //
        try {
            final Properties p = new Properties();
            p.setProperty("driver", driver);
            p.setProperty("url", url);
            if (username != null) {
                p.setProperty("user", username);
            }
            if (password != null) {
                p.setProperty("password", password);
            }
            return new SimpleDataSource(p);
        } catch (SQLException se) {
            throw new ProvisionException("Database unavailable", se);
        }
    }
}

From source file:com.googlesource.gerrit.plugins.verifystatus.server.schema.CiDataSourceProvider.java

private DataSource open(Context context, CiDataSourceType dst) {
    // ConfigSection dbs = new ConfigSection(cfg, "database");
    String driver = config.getString("driver");
    if (Strings.isNullOrEmpty(driver)) {
        driver = dst.getDriver();// ww  w  .j  av a2s . c om
    }

    String url = config.getString("dbUrl");
    if (Strings.isNullOrEmpty(url)) {
        url = dst.getUrl();
    }

    String username = config.getString("username");
    String password = config.getString("password");
    String interceptor = config.getString("dataSourceInterceptorClass");

    boolean usePool;
    if (context == Context.SINGLE_USER) {
        usePool = false;
    } else {
        usePool = config.getBoolean("connectionpool", dst.usePool());
    }

    if (usePool) {
        final BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        if (username != null && !username.isEmpty()) {
            ds.setUsername(username);
        }
        if (password != null && !password.isEmpty()) {
            ds.setPassword(password);
        }
        ds.setMaxActive(config.getInt("poollimit", DEFAULT_POOL_LIMIT));
        ds.setMinIdle(config.getInt("poolminidle", 4));
        ds.setMaxIdle(config.getInt("poolmaxidle", 4));
        String valueString = config.getString("poolmaxwait");
        if (Strings.isNullOrEmpty(valueString)) {
            ds.setMaxWait(MILLISECONDS.convert(30, SECONDS));
        } else {
            ds.setMaxWait(ConfigUtil.getTimeUnit(valueString, MILLISECONDS.convert(30, SECONDS), MILLISECONDS));
        }
        ds.setInitialSize(ds.getMinIdle());
        exportPoolMetrics(ds);
        return intercept(interceptor, ds);
    }
    // Don't use the connection pool.
    try {
        Properties p = new Properties();
        p.setProperty("driver", driver);
        p.setProperty("url", url);
        if (username != null) {
            p.setProperty("user", username);
        }
        if (password != null) {
            p.setProperty("password", password);
        }
        return intercept(interceptor, new SimpleDataSource(p));
    } catch (SQLException se) {
        throw new ProvisionException("Database unavailable", se);
    }
}

From source file:com.googlesource.gerrit.plugins.ci.server.schema.CiDataSourceProvider.java

private DataSource open(Context context, CiDataSourceType dst) {
    //ConfigSection dbs = new ConfigSection(cfg, "database");
    String driver = config.getString("driver");
    if (Strings.isNullOrEmpty(driver)) {
        driver = dst.getDriver();/*from w w  w .j  a va  2  s. c om*/
    }

    String url = config.getString("dbUrl");
    if (Strings.isNullOrEmpty(url)) {
        url = dst.getUrl();
    }

    String username = config.getString("username");
    String password = config.getString("password");
    String interceptor = config.getString("dataSourceInterceptorClass");

    boolean usePool;
    if (context == Context.SINGLE_USER) {
        usePool = false;
    } else {
        usePool = config.getBoolean("connectionpool", dst.usePool());
    }

    if (usePool) {
        final BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        if (username != null && !username.isEmpty()) {
            ds.setUsername(username);
        }
        if (password != null && !password.isEmpty()) {
            ds.setPassword(password);
        }
        ds.setMaxActive(config.getInt("poollimit", DEFAULT_POOL_LIMIT));
        ds.setMinIdle(config.getInt("poolminidle", 4));
        ds.setMaxIdle(config.getInt("poolmaxidle", 4));
        String valueString = config.getString("poolmaxwait");
        if (Strings.isNullOrEmpty(valueString)) {
            ds.setMaxWait(MILLISECONDS.convert(30, SECONDS));
        } else {
            ds.setMaxWait(ConfigUtil.getTimeUnit(valueString, MILLISECONDS.convert(30, SECONDS), MILLISECONDS));
        }
        ds.setInitialSize(ds.getMinIdle());
        exportPoolMetrics(ds);
        return intercept(interceptor, ds);
    } else {
        // Don't use the connection pool.
        //
        try {
            Properties p = new Properties();
            p.setProperty("driver", driver);
            p.setProperty("url", url);
            if (username != null) {
                p.setProperty("user", username);
            }
            if (password != null) {
                p.setProperty("password", password);
            }
            return intercept(interceptor, new SimpleDataSource(p));
        } catch (SQLException se) {
            throw new ProvisionException("Database unavailable", se);
        }
    }
}

From source file:org.alfresco.repo.tenant.TenantBasicDataSource.java

public TenantBasicDataSource(BasicDataSource bds, String tenantUrl, int tenantMaxActive) throws SQLException {
    // tenant-specific
    this.setUrl(tenantUrl);
    this.setMaxActive(tenantMaxActive == -1 ? bds.getMaxActive() : tenantMaxActive);

    // defaults/overrides - see also 'baseDefaultDataSource' (core-services-context.xml + repository.properties)

    this.setDriverClassName(bds.getDriverClassName());
    this.setUsername(bds.getUsername());
    this.setPassword(bds.getPassword());

    this.setInitialSize(bds.getInitialSize());
    this.setMinIdle(bds.getMinIdle());
    this.setMaxIdle(bds.getMaxIdle());
    this.setDefaultAutoCommit(bds.getDefaultAutoCommit());
    this.setDefaultTransactionIsolation(bds.getDefaultTransactionIsolation());
    this.setMaxWait(bds.getMaxWait());
    this.setValidationQuery(bds.getValidationQuery());
    this.setTimeBetweenEvictionRunsMillis(bds.getTimeBetweenEvictionRunsMillis());
    this.setMinEvictableIdleTimeMillis(bds.getMinEvictableIdleTimeMillis());
    this.setNumTestsPerEvictionRun(bds.getNumTestsPerEvictionRun());
    this.setTestOnBorrow(bds.getTestOnBorrow());
    this.setTestOnReturn(bds.getTestOnReturn());
    this.setTestWhileIdle(bds.getTestWhileIdle());
    this.setRemoveAbandoned(bds.getRemoveAbandoned());
    this.setRemoveAbandonedTimeout(bds.getRemoveAbandonedTimeout());
    this.setPoolPreparedStatements(bds.isPoolPreparedStatements());
    this.setMaxOpenPreparedStatements(bds.getMaxOpenPreparedStatements());
    this.setLogAbandoned(bds.getLogAbandoned());
}

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

@Test
public void testGetDataSource() throws Exception {

    String baseUrl = getClass().getPackage().getName().replace('.', '/');
    URL url = getClass().getClassLoader().getResource(baseUrl + "/");
    assertNotNull(url);//from ww w.j  av  a2 s  .  co  m

    DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
    nodeDescriptor.setConfigurationSource(new URLResource(url));
    nodeDescriptor.setParameters("testDBCP.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.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);//from  www . ja v  a 2s .  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.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  ww  w .  j a va 2 s.  c  o  m*/
        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());
}