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

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

Introduction

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

Prototype

public void setMaxConnLifetimeMillis(long maxConnLifetimeMillis) 

Source Link

Document

Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation.

Usage

From source file:com.parallax.server.blocklyprop.db.utils.DataSourceSetup.java

public static PoolingDataSource connect(Configuration configuration) throws ClassNotFoundException {
    String driver = configuration.getString("database.driver");
    String url = configuration.getString("database.url");
    String username = configuration.getString("database.username");
    String password = configuration.getString("database.password");

    Class.forName(driver);//from  w w w .  j av  a 2s. com

    //
    // First, we'll create a ConnectionFactory that the
    // pool will use to create Connections.
    // We'll use the DriverManagerConnectionFactory,
    // using the connect string passed in the command line
    // arguments.
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, username, password);

    //
    // Next we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);
    poolableConnectionFactory.setValidationQuery("SELECT 1");
    poolableConnectionFactory.setMaxConnLifetimeMillis(5000);

    //
    // Now we'll need a ObjectPool that serves as the
    // actual pool of connections.
    //
    // We'll use a GenericObjectPool instance, although
    // any ObjectPool implementation will suffice.
    //
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource<PoolableConnection> dataSourceInstance = new PoolingDataSource<>(connectionPool);

    for (NeedsDataSource dataSourceUser : dataSourceUsers) {
        dataSourceUser.setDataSource(dataSourceInstance);
    }
    DataSourceSetup.dataSource = dataSourceInstance;
    return dataSourceInstance;
}

From source file:com.yahoo.athenz.common.server.db.DataSourceFactory.java

static PoolableDataSource create(ConnectionFactory connectionFactory) {

    // setup our pool config object

    GenericObjectPoolConfig config = setupPoolConfig();

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);/*from   w  w  w  .  ja v  a  2 s  . co  m*/

    // Set max lifetime of a connection in milli-secs, after which it will
    // always fail activation, passivation, and validation.
    // Value of -1 means infinite life time. The default value
    // defined in this class is 10 minutes.
    long connTtlMillis = retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_TTL, MAX_TTL_CONN_MS);
    poolableConnectionFactory.setMaxConnLifetimeMillis(connTtlMillis);
    if (LOG.isInfoEnabled()) {
        LOG.info("Setting Time-To-Live interval for live connections (" + connTtlMillis + ") milli-secs");
    }

    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
    poolableConnectionFactory.setPool(connectionPool);

    AthenzDataSource dataSource = new AthenzDataSource(connectionPool);
    return dataSource;
}

From source file:de.alexandria.cms.config.SpringConfigBackendDatabase.java

private PoolableConnectionFactory getPoolableConnectionFactory() {
    DataSourceConnectionFactory dataSourceConnectionFactory = getDataSourceConnectionFactory();
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
            dataSourceConnectionFactory, null);
    poolableConnectionFactory.setValidationQuery("SELECT 1");
    long maxConnLifetimeMillis = 600000; // ten minutes
    poolableConnectionFactory.setMaxConnLifetimeMillis(maxConnLifetimeMillis);
    return poolableConnectionFactory;
}

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
 *//*from  ww  w  .  ja  v  a 2 s  .  c o  m*/
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  w w  w . j av  a  2  s  . c  o  m
        dataSource = new PoolingDataSource<PoolableConnection>(pool);
    }

    return dataSource.getConnection();
}