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

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

Introduction

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

Prototype

public synchronized void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) 

Source Link

Document

Sets the value of the #numTestsPerEvictionRun property.

Usage

From source file:org.apache.synapse.commons.datasource.factory.DataSourceFactory.java

/**
 * Factory method to create a DataSource based on provided information
 * which is encapsulated in the DataSourceInformation object.
 *
 * @param dataSourceInformation Information about DataSource
 * @return DataSource Instance if one can be created ,
 *         otherwise null or exception if provided details are not valid or enough to create
 *         a DataSource//from   w ww  .j a va2 s  .com
 */
public static DataSource createDataSource(DataSourceInformation dataSourceInformation) {

    String dsType = dataSourceInformation.getType();
    String driver = dataSourceInformation.getDriver();

    if (driver == null || "".equals(driver)) {
        handleException("Database driver class name cannot be found.");
    }

    String url = dataSourceInformation.getUrl();

    if (url == null || "".equals(url)) {
        handleException("Database connection URL cannot be found.");
    }

    String user = dataSourceInformation.getSecretInformation().getUser();
    String password = dataSourceInformation.getSecretInformation().getResolvedSecret();

    int defaultTransactionIsolation = dataSourceInformation.getDefaultTransactionIsolation();

    if (DataSourceInformation.BASIC_DATA_SOURCE.equals(dsType)) {

        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(driver);
        basicDataSource.setUrl(url);

        if (user != null && !"".equals(user)) {
            basicDataSource.setUsername(user);
        }

        if (password != null && !"".equals(password)) {
            basicDataSource.setPassword(password);
        }

        basicDataSource.setMaxActive(dataSourceInformation.getMaxActive());
        basicDataSource.setMaxIdle(dataSourceInformation.getMaxIdle());
        basicDataSource.setMaxWait(dataSourceInformation.getMaxWait());
        basicDataSource.setMinIdle(dataSourceInformation.getMinIdle());
        basicDataSource.setDefaultAutoCommit(dataSourceInformation.isDefaultAutoCommit());
        basicDataSource.setDefaultReadOnly(dataSourceInformation.isDefaultReadOnly());
        basicDataSource.setTestOnBorrow(dataSourceInformation.isTestOnBorrow());
        basicDataSource.setTestOnReturn(dataSourceInformation.isTestOnReturn());
        basicDataSource.setTestWhileIdle(dataSourceInformation.isTestWhileIdle());
        basicDataSource.setMinEvictableIdleTimeMillis(dataSourceInformation.getMinEvictableIdleTimeMillis());
        basicDataSource
                .setTimeBetweenEvictionRunsMillis(dataSourceInformation.getTimeBetweenEvictionRunsMillis());
        basicDataSource.setNumTestsPerEvictionRun(dataSourceInformation.getNumTestsPerEvictionRun());
        basicDataSource.setMaxOpenPreparedStatements(dataSourceInformation.getMaxOpenPreparedStatements());
        basicDataSource.setAccessToUnderlyingConnectionAllowed(
                dataSourceInformation.isAccessToUnderlyingConnectionAllowed());
        basicDataSource.setInitialSize(dataSourceInformation.getInitialSize());
        basicDataSource.setPoolPreparedStatements(dataSourceInformation.isPoolPreparedStatements());

        if (defaultTransactionIsolation != -1) {
            basicDataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
        }

        String defaultCatalog = dataSourceInformation.getDefaultCatalog();
        if (defaultCatalog != null && !"".equals(defaultCatalog)) {
            basicDataSource.setDefaultCatalog(defaultCatalog);
        }

        String validationQuery = dataSourceInformation.getValidationQuery();

        if (validationQuery != null && !"".equals(validationQuery)) {
            basicDataSource.setValidationQuery(validationQuery);
        }

        return basicDataSource;

    } else if (DataSourceInformation.PER_USER_POOL_DATA_SOURCE.equals(dsType)) {

        DriverAdapterCPDS adapterCPDS = new DriverAdapterCPDS();

        try {
            adapterCPDS.setDriver(driver);
        } catch (ClassNotFoundException e) {
            handleException("Error setting driver : " + driver + " in DriverAdapterCPDS", e);
        }

        adapterCPDS.setUrl(url);

        if (user != null && !"".equals(user)) {
            adapterCPDS.setUser(user);
        }

        if (password != null && !"".equals(password)) {
            adapterCPDS.setPassword(password);
        }

        adapterCPDS.setPoolPreparedStatements(dataSourceInformation.isPoolPreparedStatements());
        adapterCPDS.setMaxIdle(dataSourceInformation.getMaxIdle());

        PerUserPoolDataSource perUserPoolDataSource = new PerUserPoolDataSource();
        perUserPoolDataSource.setConnectionPoolDataSource(adapterCPDS);

        perUserPoolDataSource.setDefaultMaxActive(dataSourceInformation.getMaxActive());
        perUserPoolDataSource.setDefaultMaxIdle(dataSourceInformation.getMaxIdle());
        perUserPoolDataSource.setDefaultMaxWait((int) dataSourceInformation.getMaxWait());
        perUserPoolDataSource.setDefaultAutoCommit(dataSourceInformation.isDefaultAutoCommit());
        perUserPoolDataSource.setDefaultReadOnly(dataSourceInformation.isDefaultReadOnly());
        perUserPoolDataSource.setTestOnBorrow(dataSourceInformation.isTestOnBorrow());
        perUserPoolDataSource.setTestOnReturn(dataSourceInformation.isTestOnReturn());
        perUserPoolDataSource.setTestWhileIdle(dataSourceInformation.isTestWhileIdle());
        perUserPoolDataSource
                .setMinEvictableIdleTimeMillis((int) dataSourceInformation.getMinEvictableIdleTimeMillis());
        perUserPoolDataSource.setTimeBetweenEvictionRunsMillis(
                (int) dataSourceInformation.getTimeBetweenEvictionRunsMillis());
        perUserPoolDataSource.setNumTestsPerEvictionRun(dataSourceInformation.getNumTestsPerEvictionRun());

        if (defaultTransactionIsolation != -1) {
            perUserPoolDataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
        }

        String validationQuery = dataSourceInformation.getValidationQuery();

        if (validationQuery != null && !"".equals(validationQuery)) {
            perUserPoolDataSource.setValidationQuery(validationQuery);
        }

        return perUserPoolDataSource;

    } else {
        handleException("Unsupported DataSource : " + dsType);
    }
    return null;
}

From source file:org.geosde.core.jdbc.JDBCDataStoreFactory.java

/**
 * DataSource access allowing SQL use: intended to allow client code to query available schemas.
 * <p>/*from  w w  w  .ja  v  a 2 s  .  co m*/
 * This DataSource is the clients responsibility to close() when they are finished using it.
 * </p> 
 * @param params Map of connection parameter.
 * @return DataSource for SQL use
 * @throws IOException
 */
public BasicDataSource createDataSource(Map params) throws IOException {
    //create a datasource
    BasicDataSource dataSource = new BasicDataSource();

    // driver
    dataSource.setDriverClassName(getDriverClassName());

    // url
    dataSource.setUrl(getJDBCUrl(params));

    // username (on embedded dbs it can be optional)
    String user = (String) USER.lookUp(params);
    if (user != null) {
        dataSource.setUsername(user);
    }

    // password
    String passwd = (String) PASSWD.lookUp(params);
    if (passwd != null) {
        dataSource.setPassword(passwd);
    }

    // max wait
    Integer maxWait = (Integer) MAXWAIT.lookUp(params);
    if (maxWait != null && maxWait != -1) {
        dataSource.setMaxWait(maxWait * 1000);
    }

    // connection pooling options
    Integer minConn = (Integer) MINCONN.lookUp(params);
    if (minConn != null) {
        dataSource.setMinIdle(minConn);
    }

    Integer maxConn = (Integer) MAXCONN.lookUp(params);
    if (maxConn != null) {
        dataSource.setMaxActive(maxConn);
    }

    Boolean validate = (Boolean) VALIDATECONN.lookUp(params);
    if (validate != null && validate && getValidationQuery() != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(getValidationQuery());
    }

    Boolean testWhileIdle = (Boolean) TEST_WHILE_IDLE.lookUp(params);
    if (testWhileIdle != null) {
        dataSource.setTestWhileIdle(testWhileIdle);
    }

    Integer timeBetweenEvictorRuns = (Integer) TIME_BETWEEN_EVICTOR_RUNS.lookUp(params);
    if (timeBetweenEvictorRuns != null && timeBetweenEvictorRuns > 0) {
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictorRuns * 1000l);
    }

    Integer minEvictableTime = (Integer) MIN_EVICTABLE_TIME.lookUp(params);
    if (minEvictableTime != null) {
        dataSource.setMinEvictableIdleTimeMillis(minEvictableTime * 1000l);
    }

    Integer evictorTestsPerRun = (Integer) EVICTOR_TESTS_PER_RUN.lookUp(params);
    if (evictorTestsPerRun != null) {
        dataSource.setNumTestsPerEvictionRun(evictorTestsPerRun);
    }

    // some datastores might need this
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    return dataSource;
}

From source file:org.pinus4j.cluster.impl.AppDBClusterImpl.java

@Override
public void buildDataSource(DBInfo dbConnInfo) throws LoadConfigException {
    AppDBInfo appDbConnInfo = (AppDBInfo) dbConnInfo;

    LOG.info(dbConnInfo.toString());/*from  w  w w.  jav  a  2 s.c o  m*/

    try {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(enumDb.getDriverClass());
        ds.setUsername(appDbConnInfo.getUsername());
        ds.setPassword(appDbConnInfo.getPassword());
        ds.setUrl(appDbConnInfo.getUrl());

        // ?
        Map<String, Object> dbConnPoolInfo = appDbConnInfo.getConnPoolInfo();
        ds.setValidationQuery("SELECT 1");
        ds.setMaxActive((Integer) dbConnPoolInfo.get(Const.PROP_MAXACTIVE));
        ds.setMinIdle((Integer) dbConnPoolInfo.get(Const.PROP_MINIDLE));
        ds.setMaxIdle((Integer) dbConnPoolInfo.get(Const.PROP_MAXIDLE));
        ds.setInitialSize((Integer) dbConnPoolInfo.get(Const.PROP_INITIALSIZE));
        ds.setRemoveAbandoned((Boolean) dbConnPoolInfo.get(Const.PROP_REMOVEABANDONED));
        ds.setRemoveAbandonedTimeout((Integer) dbConnPoolInfo.get(Const.PROP_REMOVEABANDONEDTIMEOUT));
        ds.setMaxWait((Integer) dbConnPoolInfo.get(Const.PROP_MAXWAIT));
        ds.setTimeBetweenEvictionRunsMillis(
                (Integer) dbConnPoolInfo.get(Const.PROP_TIMEBETWEENEVICTIONRUNSMILLIS));
        ds.setNumTestsPerEvictionRun((Integer) dbConnPoolInfo.get(Const.PROP_NUMTESTSPEREVICTIONRUN));
        ds.setMinEvictableIdleTimeMillis((Integer) dbConnPoolInfo.get(Const.PROP_MINEVICTABLEIDLETIMEMILLIS));

        dbConnInfo.setDatasource(ds);
    } catch (Exception e) {
        throw new LoadConfigException(e);
    }
}