Search in sources :

Example 1 with PoolingDriver

use of org.apache.commons.dbcp2.PoolingDriver in project logging-log4j2 by apache.

the class PoolingDriverConnectionSource method stop.

@Override
public boolean stop(final long timeout, final TimeUnit timeUnit) {
    try {
        final PoolingDriver driver = getPoolingDriver();
        if (driver != null) {
            getLogger().debug("Driver {} closing DBCP pool '{}'", driver, poolName);
            driver.closePool(poolName);
        }
        return true;
    } catch (final Exception e) {
        getLogger().error("Exception stopping connection source for '{}' → '{}'", getConnectionString(), getActualConnectionString(), e);
        return false;
    }
}
Also used : PoolingDriver(org.apache.commons.dbcp2.PoolingDriver) SQLException(java.sql.SQLException)

Example 2 with PoolingDriver

use of org.apache.commons.dbcp2.PoolingDriver in project logging-log4j2 by apache.

the class PoolingDriverConnectionSource method setupDriver.

private void setupDriver(final String connectionString, final PoolableConnectionFactoryConfig poolableConnectionFactoryConfig) throws SQLException {
    // 
    // 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.
    // 
    final Property[] properties = getProperties();
    final char[] userName = getUserName();
    final char[] password = getPassword();
    final ConnectionFactory connectionFactory;
    if (properties != null && properties.length > 0) {
        if (userName != null || password != null) {
            throw new SQLException("Either set the userName and password, or set the Properties, but not both.");
        }
        connectionFactory = new DriverManagerConnectionFactory(connectionString, toProperties(properties));
    } else {
        connectionFactory = new DriverManagerConnectionFactory(connectionString, toString(userName), toString(password));
    }
    // 
    // Next, we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    // 
    final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    if (poolableConnectionFactoryConfig != null) {
        poolableConnectionFactoryConfig.init(poolableConnectionFactory);
    }
    // 
    // 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.
    // 
    @SuppressWarnings("resource") final ObjectPool<PoolableConnection> // This GenericObjectPool will be closed on shutdown
    connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);
    loadDriver(poolingDriverClassName);
    final PoolingDriver driver = getPoolingDriver();
    if (driver != null) {
        getLogger().debug("Registering DBCP pool '{}' with pooling driver {}: {}", poolName, driver, connectionPool);
        driver.registerPool(poolName, connectionPool);
    }
// 
// Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
// to access our pool of Connections.
// 
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) SQLException(java.sql.SQLException) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) PoolingDriver(org.apache.commons.dbcp2.PoolingDriver) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Property(org.apache.logging.log4j.core.config.Property) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 3 with PoolingDriver

use of org.apache.commons.dbcp2.PoolingDriver in project zeppelin by apache.

the class JDBCInterpreter method createConnectionPool.

private void createConnectionPool(String url, String user, String dbPrefix, Properties properties) throws SQLException, ClassNotFoundException {
    LOGGER.info("Creating connection pool for url: {}, user: {}, dbPrefix: {}, properties: {}", url, user, dbPrefix, properties);
    /* Remove properties that is not valid properties for presto/trino by checking driver key.
     * - Presto: com.facebook.presto.jdbc.PrestoDriver
     * - Trino(ex. PrestoSQL): io.trino.jdbc.TrinoDriver / io.prestosql.jdbc.PrestoDriver
     */
    String driverClass = properties.getProperty(DRIVER_KEY);
    if (driverClass != null && (driverClass.equals("com.facebook.presto.jdbc.PrestoDriver") || driverClass.equals("io.prestosql.jdbc.PrestoDriver") || driverClass.equals("io.trino.jdbc.TrinoDriver"))) {
        for (String key : properties.stringPropertyNames()) {
            if (!PRESTO_PROPERTIES.contains(key)) {
                properties.remove(key);
            }
        }
    }
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, properties);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    final String maxConnectionLifetime = StringUtils.defaultIfEmpty(getProperty("zeppelin.jdbc.maxConnLifetime"), "-1");
    poolableConnectionFactory.setMaxConnLifetimeMillis(Long.parseLong(maxConnectionLifetime));
    poolableConnectionFactory.setValidationQuery(PropertiesUtil.getString(properties, "validationQuery", "show databases"));
    ObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);
    this.configConnectionPool((GenericObjectPool) connectionPool, properties);
    poolableConnectionFactory.setPool(connectionPool);
    Class.forName(driverClass);
    PoolingDriver driver = new PoolingDriver();
    driver.registerPool(dbPrefix + user, connectionPool);
    getJDBCConfiguration(user).saveDBDriverPool(dbPrefix, driver);
}
Also used : DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolingDriver(org.apache.commons.dbcp2.PoolingDriver) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) ObjectPool(org.apache.commons.pool2.ObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Aggregations

PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)3 SQLException (java.sql.SQLException)2 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)2 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)2 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)2 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)2 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)1 ObjectPool (org.apache.commons.pool2.ObjectPool)1 Property (org.apache.logging.log4j.core.config.Property)1