Example usage for org.apache.commons.dbcp2 PoolableConnectionFactory setValidationQueryTimeout

List of usage examples for org.apache.commons.dbcp2 PoolableConnectionFactory setValidationQueryTimeout

Introduction

In this page you can find the example usage for org.apache.commons.dbcp2 PoolableConnectionFactory setValidationQueryTimeout.

Prototype

public void setValidationQueryTimeout(int timeout) 

Source Link

Document

Sets the validation query timeout, the amount of time, in seconds, that connection validation will wait for a response from the database when executing a validation query.

Usage

From source file:JDBCPool.dbcp.demo.sourcecode.BasicDataSource.java

/**
 * Creates the PoolableConnectionFactory and attaches it to the connection pool.  This method only exists
 * so subclasses can replace the default implementation.
 *
 * @param driverConnectionFactory JDBC connection factory
 * @throws SQLException if an error occurs creating the PoolableConnectionFactory
 *///w  w  w  .j  a v a  2 s  . c om
protected PoolableConnectionFactory createPoolableConnectionFactory(ConnectionFactory driverConnectionFactory)
        throws SQLException {
    PoolableConnectionFactory connectionFactory = null;
    try {
        connectionFactory = new PoolableConnectionFactory(driverConnectionFactory, registeredJmxName);
        connectionFactory.setValidationQuery(validationQuery);
        connectionFactory.setValidationQueryTimeout(validationQueryTimeout);
        connectionFactory.setConnectionInitSql(connectionInitSqls); //?Connection?SQL?
        connectionFactory.setDefaultReadOnly(defaultReadOnly);
        connectionFactory.setDefaultAutoCommit(defaultAutoCommit);
        connectionFactory.setDefaultTransactionIsolation(defaultTransactionIsolation);
        connectionFactory.setDefaultCatalog(defaultCatalog);
        connectionFactory.setCacheState(cacheState);
        connectionFactory.setPoolStatements(poolPreparedStatements);
        connectionFactory.setMaxOpenPrepatedStatements(maxOpenPreparedStatements);
        connectionFactory.setMaxConnLifetimeMillis(maxConnLifetimeMillis);
        connectionFactory.setRollbackOnReturn(getRollbackOnReturn());
        connectionFactory.setEnableAutoCommitOnReturn(getEnableAutoCommitOnReturn());
        connectionFactory.setDefaultQueryTimeout(getDefaultQueryTimeout());
        validateConnectionFactory(connectionFactory); //??
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new SQLException("Cannot create PoolableConnectionFactory (" + e.getMessage() + ")", e);
    }
    return connectionFactory;
}

From source file:org.aludratest.cloud.impl.app.LogDatabase.java

private Connection getConnection() throws SQLException {
    if (dataSource == null) {
        EmbeddedDataSource ds = new EmbeddedDataSource();
        ds.setDatabaseName("acm");
        ConnectionFactory connectionFactory = new DataSourceConnectionFactory(ds);
        PoolableConnectionFactory objFactory = new PoolableConnectionFactory(connectionFactory, null);
        objFactory.setValidationQuery("VALUES 1");
        objFactory.setDefaultAutoCommit(true);
        // max 10 minutes lifetime
        objFactory.setMaxConnLifetimeMillis(1000l * 60 * 10);
        // must be fast, because is local
        objFactory.setValidationQueryTimeout(5);

        GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(objFactory);
        pool.setMaxIdle(2);//from   ww  w  .  ja  va2 s . c o m
        dataSource = new PoolingDataSource<PoolableConnection>(pool);
    }

    return dataSource.getConnection();
}