Search in sources :

Example 11 with PoolableConnectionFactory

use of org.apache.commons.dbcp2.PoolableConnectionFactory in project cloudstack by apache.

the class TransactionLegacy method getDefaultDataSource.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static DataSource getDefaultDataSource(final String database) {
    final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://localhost:3306/" + database, "cloud", "cloud");
    final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    final GenericObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);
    return new PoolingDataSource(connectionPool);
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 12 with PoolableConnectionFactory

use of org.apache.commons.dbcp2.PoolableConnectionFactory in project tomcat by apache.

the class BasicManagedDataSource method createPoolableConnectionFactory.

/**
 * Creates the PoolableConnectionFactory and attaches it to the connection pool.
 *
 * @param driverConnectionFactory
 *            JDBC connection factory created by {@link #createConnectionFactory()}
 * @throws SQLException
 *             if an error occurs creating the PoolableConnectionFactory
 */
@Override
protected PoolableConnectionFactory createPoolableConnectionFactory(final ConnectionFactory driverConnectionFactory) throws SQLException {
    PoolableConnectionFactory connectionFactory = null;
    try {
        connectionFactory = new PoolableManagedConnectionFactory((XAConnectionFactory) driverConnectionFactory, getRegisteredJmxName());
        connectionFactory.setValidationQuery(getValidationQuery());
        connectionFactory.setValidationQueryTimeout(getValidationQueryTimeoutDuration());
        connectionFactory.setConnectionInitSql(getConnectionInitSqls());
        connectionFactory.setDefaultReadOnly(getDefaultReadOnly());
        connectionFactory.setDefaultAutoCommit(getDefaultAutoCommit());
        connectionFactory.setDefaultTransactionIsolation(getDefaultTransactionIsolation());
        connectionFactory.setDefaultCatalog(getDefaultCatalog());
        connectionFactory.setDefaultSchema(getDefaultSchema());
        connectionFactory.setCacheState(getCacheState());
        connectionFactory.setPoolStatements(isPoolPreparedStatements());
        connectionFactory.setClearStatementPoolOnReturn(isClearStatementPoolOnReturn());
        connectionFactory.setMaxOpenPreparedStatements(getMaxOpenPreparedStatements());
        connectionFactory.setMaxConn(getMaxConnDuration());
        connectionFactory.setRollbackOnReturn(getRollbackOnReturn());
        connectionFactory.setAutoCommitOnReturn(getAutoCommitOnReturn());
        connectionFactory.setDefaultQueryTimeout(getDefaultQueryTimeoutDuration());
        connectionFactory.setFastFailValidation(getFastFailValidation());
        connectionFactory.setDisconnectionSqlCodes(getDisconnectionSqlCodes());
        validateConnectionFactory(connectionFactory);
    } catch (final RuntimeException e) {
        throw e;
    } catch (final Exception e) {
        throw new SQLException("Cannot create PoolableConnectionFactory (" + e.getMessage() + ")", e);
    }
    return connectionFactory;
}
Also used : SQLException(java.sql.SQLException) PoolableConnectionFactory(org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory) SQLException(java.sql.SQLException)

Aggregations

PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)10 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)9 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)8 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)6 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)6 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)5 SQLException (java.sql.SQLException)4 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)4 Properties (java.util.Properties)3 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)2 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Driver (java.sql.Driver)1 Map (java.util.Map)1 TransactionManager (javax.transaction.TransactionManager)1 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)1 DataSourceConnectionFactory (org.apache.commons.dbcp2.DataSourceConnectionFactory)1 DriverConnectionFactory (org.apache.commons.dbcp2.DriverConnectionFactory)1 LocalXAConnectionFactory (org.apache.commons.dbcp2.managed.LocalXAConnectionFactory)1