Example usage for org.apache.commons.dbcp2 BasicDataSource setTestOnCreate

List of usage examples for org.apache.commons.dbcp2 BasicDataSource setTestOnCreate

Introduction

In this page you can find the example usage for org.apache.commons.dbcp2 BasicDataSource setTestOnCreate.

Prototype

public synchronized void setTestOnCreate(boolean testOnCreate) 

Source Link

Document

Sets the #testOnCreate property.

Usage

From source file:org.apache.jmeter.protocol.jdbc.config.DataSourceElement.java

private BasicDataSource initPool(String maxPool) {
    BasicDataSource dataSource = new BasicDataSource();

    if (log.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder(40);
        sb.append("MaxPool: ");
        sb.append(maxPool);/*w  w  w. j av  a  2s  .c  o  m*/
        sb.append(" Timeout: ");
        sb.append(getTimeout());
        sb.append(" TrimInt: ");
        sb.append(getTrimInterval());
        sb.append(" Auto-Commit: ");
        sb.append(isAutocommit());
        log.debug(sb.toString());
    }
    int poolSize = Integer.parseInt(maxPool);
    dataSource.setMinIdle(0);
    dataSource.setInitialSize(poolSize);
    dataSource.setMaxIdle(poolSize);
    dataSource.setMaxTotal(poolSize);
    dataSource.setMaxWaitMillis(Long.parseLong(getTimeout()));

    dataSource.setDefaultAutoCommit(Boolean.valueOf(isAutocommit()));

    if (log.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder(40);
        sb.append("KeepAlive: ");
        sb.append(isKeepAlive());
        sb.append(" Age: ");
        sb.append(getConnectionAge());
        sb.append(" CheckQuery: ");
        sb.append(getCheckQuery());
        log.debug(sb.toString());
    }
    dataSource.setTestOnBorrow(false);
    dataSource.setTestOnReturn(false);
    dataSource.setTestOnCreate(false);
    dataSource.setTestWhileIdle(false);

    if (isKeepAlive()) {
        dataSource.setTestWhileIdle(true);
        dataSource.setValidationQuery(getCheckQuery());
        dataSource.setSoftMinEvictableIdleTimeMillis(Long.parseLong(getConnectionAge()));
        dataSource.setTimeBetweenEvictionRunsMillis(Integer.parseInt(getTrimInterval()));
    }

    int transactionIsolation = DataSourceElementBeanInfo.getTransactionIsolationMode(getTransactionIsolation());
    if (transactionIsolation >= 0) {
        dataSource.setDefaultTransactionIsolation(transactionIsolation);
    }

    String _username = getUsername();
    if (log.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder(40);
        sb.append("Driver: ");
        sb.append(getDriver());
        sb.append(" DbUrl: ");
        sb.append(getDbUrl());
        sb.append(" User: ");
        sb.append(_username);
        log.debug(sb.toString());
    }
    dataSource.setDriverClassName(getDriver());
    dataSource.setUrl(getDbUrl());

    if (_username.length() > 0) {
        dataSource.setUsername(_username);
        dataSource.setPassword(getPassword());
    }

    // log is required to ensure errors are available
    //source.enableLogging(new LogKitLogger(log));
    if (log.isDebugEnabled()) {
        log.debug("PoolConfiguration:" + this.dataSource);
    }
    return dataSource;
}