Search in sources :

Example 1 with DriverManagerConnectionFactory

use of org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory in project athenz by yahoo.

the class DataSourceFactory method create.

public static PoolableDataSource create(String url, Properties mysqlConnectionProperties) {
    String driver = null;
    try {
        if (url.indexOf(":mysql:") > 0) {
            driver = "com.mysql.jdbc.Driver";
            Class.forName(driver);
            ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, mysqlConnectionProperties);
            return create(connectionFactory);
        } else {
            throw new RuntimeException("Cannot figure out how to instantiate this data source: " + url);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Cannot load driver class: " + driver);
    } catch (Exception exc) {
        throw new RuntimeException("Failed to create database source(" + url + ") with driver(" + driver + ")", exc);
    }
}
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)

Example 2 with DriverManagerConnectionFactory

use of org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory in project cloudstack by apache.

the class TransactionLegacy method createDataSource.

/**
 * Creates a data source
 */
private static DataSource createDataSource(String uri, String username, String password, Integer maxActive, Integer maxIdle, Long maxWait, Long timeBtwnEvictionRuns, Long minEvictableIdleTime, Boolean testWhileIdle, Boolean testOnBorrow, String validationQuery, Integer isolationLevel) {
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(uri, username, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    GenericObjectPoolConfig config = createPoolConfig(maxActive, maxIdle, maxWait, timeBtwnEvictionRuns, minEvictableIdleTime, testWhileIdle, testOnBorrow);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
    poolableConnectionFactory.setPool(connectionPool);
    if (validationQuery != null) {
        poolableConnectionFactory.setValidationQuery(validationQuery);
    }
    if (isolationLevel != null) {
        poolableConnectionFactory.setDefaultTransactionIsolation(isolationLevel);
    }
    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) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 3 with DriverManagerConnectionFactory

use of org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory in project datanucleus-rdbms by datanucleus.

the class DBCP2BuiltinConnectionPoolFactory method createConnectionPool.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.datasource.ConnectionPoolFactory#createConnectionPool(org.datanucleus.store.StoreManager)
     */
public ConnectionPool createConnectionPool(StoreManager storeMgr) {
    // Load the database driver
    String dbDriver = storeMgr.getConnectionDriverName();
    if (!StringUtils.isWhitespace(dbDriver)) {
        loadDriver(dbDriver, storeMgr.getNucleusContext().getClassLoaderResolver(null));
    }
    String dbURL = storeMgr.getConnectionURL();
    PoolingDataSource ds = null;
    GenericObjectPool<PoolableConnection> connectionPool;
    try {
        // Create a factory to be used by the pool to create the connections
        Properties dbProps = getPropertiesForDriver(storeMgr);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dbURL, dbProps);
        // Wrap the connections and statements with pooled variants
        PoolableConnectionFactory poolableCF = null;
        poolableCF = new PoolableConnectionFactory(connectionFactory, null);
        String testSQL = null;
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TEST_SQL)) {
            testSQL = storeMgr.getStringProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TEST_SQL);
            poolableCF.setValidationQuery(testSQL);
        }
        // Create the actual pool of connections, and apply any properties
        connectionPool = new GenericObjectPool(poolableCF);
        poolableCF.setPool(connectionPool);
        if (testSQL != null) {
            connectionPool.setTestOnBorrow(true);
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_IDLE)) {
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_IDLE);
            if (value > 0) {
                connectionPool.setMaxIdle(value);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_IDLE)) {
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_IDLE);
            if (value > 0) {
                connectionPool.setMinIdle(value);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_ACTIVE)) {
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_ACTIVE);
            if (value > 0) {
                connectionPool.setMaxTotal(value);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_WAIT)) {
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_WAIT);
            if (value > 0) {
                connectionPool.setMaxWaitMillis(value);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TIME_BETWEEN_EVICTOR_RUNS_MILLIS)) {
            // how often should the evictor run (if ever, default is -1 = off)
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TIME_BETWEEN_EVICTOR_RUNS_MILLIS);
            if (value > 0) {
                connectionPool.setTimeBetweenEvictionRunsMillis(value);
                // in each eviction run, evict at least a quarter of "maxIdle" connections
                int maxIdle = connectionPool.getMaxIdle();
                int numTestsPerEvictionRun = (int) Math.ceil((double) maxIdle / 4);
                connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS)) {
            // how long may a connection sit idle in the pool before it may be evicted
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS);
            if (value > 0) {
                connectionPool.setMinEvictableIdleTimeMillis(value);
            }
        }
        // Create the datasource
        ds = new org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource(connectionPool);
    } catch (Exception e) {
        throw new DatastorePoolException("DBCP2", dbDriver, dbURL, e);
    }
    return new DBCPConnectionPool(ds, connectionPool);
}
Also used : PoolingDataSource(org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource) DriverManagerConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory) Properties(java.util.Properties) GenericObjectPool(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPool) DriverManagerConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory) PoolableConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnectionFactory) ConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.ConnectionFactory) PoolableConnection(org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnection) PoolingDataSource(org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource) PoolableConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnectionFactory)

Example 4 with DriverManagerConnectionFactory

use of org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory 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 5 with DriverManagerConnectionFactory

use of org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory 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

ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)7 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)7 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)7 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)5 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)3 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)3 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)2 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)2 File (java.io.File)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 ObjectPool (org.apache.commons.pool2.ObjectPool)1 Property (org.apache.logging.log4j.core.config.Property)1 ConnectionFactory (org.datanucleus.store.rdbms.datasource.dbcp2.ConnectionFactory)1 DriverManagerConnectionFactory (org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory)1 PoolableConnection (org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnection)1 PoolableConnectionFactory (org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnectionFactory)1 PoolingDataSource (org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource)1 GenericObjectPool (org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPool)1