Example usage for org.apache.commons.dbcp.datasources SharedPoolDataSource getMinEvictableIdleTimeMillis

List of usage examples for org.apache.commons.dbcp.datasources SharedPoolDataSource getMinEvictableIdleTimeMillis

Introduction

In this page you can find the example usage for org.apache.commons.dbcp.datasources SharedPoolDataSource getMinEvictableIdleTimeMillis.

Prototype

public int getMinEvictableIdleTimeMillis() 

Source Link

Document

Returns the minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any).

Usage

From source file:com.smartmarmot.orabbix.Configurator.java

private DBConn getConnection(String dbName) throws Exception {
    try {//from  w ww  .j  av  a2s  . co  m
        verifyConfig();

        SmartLogger.logThis(Level.DEBUG, "getConnection for database " + dbName);
        String url = "";
        try {
            url = new String(_props.getProperty(dbName + "." + Constants.CONN_URL));
        } catch (Exception ex) {
            SmartLogger.logThis(Level.ERROR, "Error on Configurator getConnection while getting " + dbName + "."
                    + Constants.CONN_URL + " " + ex.getMessage());
        }

        String uname = "";
        try {
            uname = new String(_props.getProperty(dbName + "." + Constants.CONN_USERNAME));
        } catch (Exception ex) {
            try {
                SmartLogger.logThis(Level.DEBUG, "Error on Configurator getConnection while getting " + dbName
                        + "." + Constants.CONN_USERNAME + " " + ex.getMessage());

                uname = new String(_props.getProperty(Constants.CONN_DEFAULT_USERNAME));
            } catch (Exception ex1) {
                SmartLogger.logThis(Level.ERROR, "Error on Configurator getConnection while getting "
                        + Constants.CONN_DEFAULT_USERNAME + " " + ex1.getMessage());
            }
        }
        String password = "";
        try {
            password = new String(_props.getProperty(dbName + "." + Constants.CONN_PASSWORD));
        } catch (Exception ex) {
            try {
                SmartLogger.logThis(Level.DEBUG, "Error on Configurator getConnection while getting " + dbName
                        + "." + Constants.CONN_PASSWORD + " " + ex.getMessage());
                password = new String(_props.getProperty(Constants.CONN_DEFAULT_PASSWORD));
            } catch (Exception ex1) {
                SmartLogger.logThis(Level.ERROR, "Error on Configurator getConnection while getting " + dbName
                        + "." + Constants.CONN_PASSWORD + " " + ex.getMessage());
            }
        }
        DriverAdapterCPDS cpds = new DriverAdapterCPDS();
        cpds.setDriver(Constants.ORACLE_DRIVER);
        cpds.setUrl(url.toString());
        cpds.setUser(uname.toString());
        cpds.setPassword(password.toString());
        SharedPoolDataSource tds = new SharedPoolDataSource();
        tds.setConnectionPoolDataSource(cpds);
        // tds.setMaxActive(5);
        Integer maxActive = new Integer(5);
        try {
            maxActive = new Integer(_props.getProperty(dbName + "." + Constants.CONN_MAX_ACTIVE));
        } catch (Exception ex) {
            SmartLogger.logThis(Level.DEBUG,
                    "Note: " + dbName + "." + Constants.CONN_MAX_ACTIVE + " " + ex.getMessage());
            try {
                maxActive = new Integer(
                        _props.getProperty(Constants.DATABASES_LIST + "." + Constants.CONN_MAX_ACTIVE));
            } catch (Exception e) {
                SmartLogger.logThis(Level.WARN, "Note: " + Constants.DATABASES_LIST + "."
                        + Constants.CONN_MAX_ACTIVE + " " + e.getMessage());
                SmartLogger.logThis(Level.WARN, "Warning I will use default value " + maxActive);
            }
        }
        tds.setMaxActive(maxActive.intValue());
        Integer maxWait = new Integer(100);
        try {
            maxWait = new Integer(_props.getProperty(dbName + "." + Constants.CONN_MAX_WAIT));
        } catch (Exception ex) {
            SmartLogger.logThis(Level.DEBUG,
                    "Note: " + dbName + "." + Constants.CONN_MAX_WAIT + " " + ex.getMessage());
            try {
                maxWait = new Integer(
                        _props.getProperty(Constants.DATABASES_LIST + "." + Constants.CONN_MAX_WAIT));
            } catch (Exception e) {
                SmartLogger.logThis(Level.WARN, "Note: " + Constants.DATABASES_LIST + "."
                        + Constants.CONN_MAX_WAIT + " " + e.getMessage());
                SmartLogger.logThis(Level.WARN, "Warning I will use default value " + maxWait);
            }
        }
        tds.setMaxWait(maxWait.intValue());
        Integer maxIdle = new Integer(1);
        try {
            maxIdle = new Integer(_props.getProperty(dbName + "." + Constants.CONN_MAX_IDLE));
        } catch (Exception ex) {
            SmartLogger.logThis(Level.DEBUG,
                    "Note: " + dbName + "." + Constants.CONN_MAX_IDLE + " " + ex.getMessage());
            try {
                maxIdle = new Integer(
                        _props.getProperty(Constants.DATABASES_LIST + "." + Constants.CONN_MAX_IDLE));
            } catch (Exception e) {
                SmartLogger.logThis(Level.WARN, "Note: " + Constants.DATABASES_LIST + "."
                        + Constants.CONN_MAX_IDLE + " " + e.getMessage());
                SmartLogger.logThis(Level.WARN, "Warning I will use default value " + maxIdle);
            }
        }
        tds.setMaxIdle(maxIdle.intValue());

        SmartLogger.logThis(Level.INFO, "DB Pool created: " + tds);
        SmartLogger.logThis(Level.INFO, "URL=" + url.toString());
        SmartLogger.logThis(Level.INFO, "maxPoolSize=" + tds.getMaxActive());
        SmartLogger.logThis(Level.INFO, "maxIdleSize=" + tds.getMaxIdle());
        SmartLogger.logThis(Level.INFO, "maxIdleTime=" + tds.getMinEvictableIdleTimeMillis() + "ms");
        SmartLogger.logThis(Level.INFO, "poolTimeout=" + tds.getMaxWait());
        SmartLogger.logThis(Level.INFO,
                "timeBetweenEvictionRunsMillis=" + tds.getTimeBetweenEvictionRunsMillis());
        SmartLogger.logThis(Level.INFO, "numTestsPerEvictionRun=" + tds.getNumTestsPerEvictionRun());

        tds.setValidationQuery(Constants.ORACLE_VALIDATION_QUERY);
        Connection con = null;
        con = tds.getConnection();
        PreparedStatement p_stmt = null;
        p_stmt = con.prepareStatement(Constants.ORACLE_WHOAMI_QUERY);
        ResultSet rs = null;
        rs = p_stmt.executeQuery();
        String tempStr = new String("");
        ResultSetMetaData rsmd = rs.getMetaData();
        int numColumns = rsmd.getColumnCount();
        while (rs.next()) {
            for (int r = 1; r < numColumns + 1; r++) {
                tempStr = tempStr + rs.getObject(r).toString().trim();
            }
        }
        SmartLogger.logThis(Level.INFO, "Connected as " + tempStr);

        con.close();
        con = null;
        con = tds.getConnection();
        p_stmt = con.prepareStatement(Constants.ORACLE_DBNAME_QUERY);
        rs = p_stmt.executeQuery();
        rsmd = rs.getMetaData();
        numColumns = rsmd.getColumnCount();
        tempStr = "";
        while (rs.next()) {
            for (int r = 1; r < numColumns + 1; r++) {
                tempStr = tempStr + rs.getObject(r).toString().trim();
            }
        }
        SmartLogger.logThis(Level.INFO, "--------- on Database -> " + tempStr);
        con.close();
        con = null;
        DBConn mydbconn = new DBConn(tds, dbName.toString());
        return mydbconn;

    } catch (Exception ex) {
        SmartLogger.logThis(Level.ERROR,
                "Error on Configurator for database " + dbName + " -->" + ex.getMessage());
        return null;
    }
}

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

@Test
public void get() throws Throwable {
    final boolean autoCommit = true;
    final int loginTimeout = 10;
    final boolean defaultReadOnly = true;
    final int defaultTransactionIsolation = Connection.TRANSACTION_READ_COMMITTED;
    final String description = "test_description";
    final int minEvictableIdleTimeMillis = 30;
    final int numTestsPerEvictionRun = 40;
    final boolean rollbackAfterValidation = true;
    final boolean testOnBorrow = true;
    final boolean testOnReturn = true;
    final boolean testWhileIdle = true;
    final int timeBetweenEvictionRunsMillis = 50;
    final String validationQuery = "SELECT 1";
    final int maxActive = 60;
    final int maxIdle = 70;
    final int maxWait = 80;
    Injector injector = Guice.createInjector(new AbstractModule() {
        @Override//  w w w  .  j  a va  2s.  co m
        protected void configure() {
            bind(ConnectionPoolDataSource.class).toInstance(connectionPoolDataSource);
            bindConstant().annotatedWith(Names.named("JDBC.autoCommit")).to(autoCommit);
            bindConstant().annotatedWith(Names.named("JDBC.loginTimeout")).to(loginTimeout);
            bindConstant().annotatedWith(Names.named("DBCP.defaultReadOnly")).to(defaultReadOnly);
            bindConstant().annotatedWith(Names.named("DBCP.defaultTransactionIsolation"))
                    .to(defaultTransactionIsolation);
            bindConstant().annotatedWith(Names.named("DBCP.description")).to(description);
            bindConstant().annotatedWith(Names.named("DBCP.minEvictableIdleTimeMillis"))
                    .to(minEvictableIdleTimeMillis);
            bindConstant().annotatedWith(Names.named("DBCP.numTestsPerEvictionRun")).to(numTestsPerEvictionRun);
            bindConstant().annotatedWith(Names.named("DBCP.rollbackAfterValidation"))
                    .to(rollbackAfterValidation);
            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);
            bindConstant().annotatedWith(Names.named("DBCP.maxActive")).to(maxActive);
            bindConstant().annotatedWith(Names.named("DBCP.maxIdle")).to(maxIdle);
            bindConstant().annotatedWith(Names.named("DBCP.maxWait")).to(maxWait);
        }
    });
    SharedPoolDataSourceProvider provider = injector.getInstance(SharedPoolDataSourceProvider.class);

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

    assertEquals(connectionPoolDataSource, dataSource.getConnectionPoolDataSource());
    assertEquals(autoCommit, dataSource.isDefaultAutoCommit());
    assertEquals(defaultReadOnly, dataSource.isDefaultReadOnly());
    assertEquals(defaultTransactionIsolation, dataSource.getDefaultTransactionIsolation());
    assertEquals(description, dataSource.getDescription());
    assertEquals(loginTimeout, dataSource.getLoginTimeout());
    assertEquals(minEvictableIdleTimeMillis, dataSource.getMinEvictableIdleTimeMillis());
    assertEquals(numTestsPerEvictionRun, dataSource.getNumTestsPerEvictionRun());
    assertEquals(rollbackAfterValidation, dataSource.isRollbackAfterValidation());
    assertEquals(testOnBorrow, dataSource.isTestOnBorrow());
    assertEquals(testOnReturn, dataSource.isTestOnReturn());
    assertEquals(testWhileIdle, dataSource.isTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, dataSource.getTimeBetweenEvictionRunsMillis());
    assertEquals(validationQuery, dataSource.getValidationQuery());
    assertEquals(maxActive, dataSource.getMaxActive());
    assertEquals(maxIdle, dataSource.getMaxIdle());
    assertEquals(maxWait, dataSource.getMaxWait());
}

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

@Test
public void get_OtherValues() throws Throwable {
    final boolean autoCommit = false;
    final int loginTimeout = 11;
    final boolean defaultReadOnly = false;
    final int defaultTransactionIsolation = Connection.TRANSACTION_REPEATABLE_READ;
    final String description = "test_description2";
    final int minEvictableIdleTimeMillis = 31;
    final int numTestsPerEvictionRun = 41;
    final boolean rollbackAfterValidation = false;
    final boolean testOnBorrow = false;
    final boolean testOnReturn = false;
    final boolean testWhileIdle = false;
    final int timeBetweenEvictionRunsMillis = 51;
    final String validationQuery = "SELECT 2";
    final int maxActive = 61;
    final int maxIdle = 71;
    final int maxWait = 81;
    Injector injector = Guice.createInjector(new AbstractModule() {
        @Override/* ww w  .  ja v a  2  s  .c om*/
        protected void configure() {
            bind(ConnectionPoolDataSource.class).toInstance(connectionPoolDataSource);
            bindConstant().annotatedWith(Names.named("JDBC.autoCommit")).to(autoCommit);
            bindConstant().annotatedWith(Names.named("JDBC.loginTimeout")).to(loginTimeout);
            bindConstant().annotatedWith(Names.named("DBCP.defaultReadOnly")).to(defaultReadOnly);
            bindConstant().annotatedWith(Names.named("DBCP.defaultTransactionIsolation"))
                    .to(defaultTransactionIsolation);
            bindConstant().annotatedWith(Names.named("DBCP.description")).to(description);
            bindConstant().annotatedWith(Names.named("DBCP.minEvictableIdleTimeMillis"))
                    .to(minEvictableIdleTimeMillis);
            bindConstant().annotatedWith(Names.named("DBCP.numTestsPerEvictionRun")).to(numTestsPerEvictionRun);
            bindConstant().annotatedWith(Names.named("DBCP.rollbackAfterValidation"))
                    .to(rollbackAfterValidation);
            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);
            bindConstant().annotatedWith(Names.named("DBCP.maxActive")).to(maxActive);
            bindConstant().annotatedWith(Names.named("DBCP.maxIdle")).to(maxIdle);
            bindConstant().annotatedWith(Names.named("DBCP.maxWait")).to(maxWait);
        }
    });
    SharedPoolDataSourceProvider provider = injector.getInstance(SharedPoolDataSourceProvider.class);

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

    assertEquals(connectionPoolDataSource, dataSource.getConnectionPoolDataSource());
    assertEquals(autoCommit, dataSource.isDefaultAutoCommit());
    assertEquals(defaultReadOnly, dataSource.isDefaultReadOnly());
    assertEquals(defaultTransactionIsolation, dataSource.getDefaultTransactionIsolation());
    assertEquals(description, dataSource.getDescription());
    assertEquals(loginTimeout, dataSource.getLoginTimeout());
    assertEquals(minEvictableIdleTimeMillis, dataSource.getMinEvictableIdleTimeMillis());
    assertEquals(numTestsPerEvictionRun, dataSource.getNumTestsPerEvictionRun());
    assertEquals(rollbackAfterValidation, dataSource.isRollbackAfterValidation());
    assertEquals(testOnBorrow, dataSource.isTestOnBorrow());
    assertEquals(testOnReturn, dataSource.isTestOnReturn());
    assertEquals(testWhileIdle, dataSource.isTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, dataSource.getTimeBetweenEvictionRunsMillis());
    assertEquals(validationQuery, dataSource.getValidationQuery());
    assertEquals(maxActive, dataSource.getMaxActive());
    assertEquals(maxIdle, dataSource.getMaxIdle());
    assertEquals(maxWait, dataSource.getMaxWait());
}