Search in sources :

Example 1 with DriverManagerConnectionFactory

use of org.apache.commons.dbcp.DriverManagerConnectionFactory in project hive by apache.

the class TxnHandler method setupJdbcConnectionPool.

private static synchronized void setupJdbcConnectionPool(HiveConf conf) throws SQLException {
    if (connPool != null)
        return;
    String driverUrl = HiveConf.getVar(conf, HiveConf.ConfVars.METASTORECONNECTURLKEY);
    String user = getMetastoreJdbcUser(conf);
    String passwd = getMetastoreJdbcPasswd(conf);
    String connectionPooler = conf.getVar(HiveConf.ConfVars.METASTORE_CONNECTION_POOLING_TYPE).toLowerCase();
    if ("bonecp".equals(connectionPooler)) {
        BoneCPConfig config = new BoneCPConfig();
        config.setJdbcUrl(driverUrl);
        //if we are waiting for connection for 60s, something is really wrong
        //better raise an error than hang forever
        config.setConnectionTimeoutInMs(60000);
        config.setMaxConnectionsPerPartition(10);
        config.setPartitionCount(1);
        config.setUser(user);
        config.setPassword(passwd);
        connPool = new BoneCPDataSource(config);
        // Enable retries to work around BONECP bug.
        doRetryOnConnPool = true;
    } else if ("dbcp".equals(connectionPooler)) {
        ObjectPool objectPool = new GenericObjectPool();
        ConnectionFactory connFactory = new DriverManagerConnectionFactory(driverUrl, user, passwd);
        // This doesn't get used, but it's still necessary, see
        // http://svn.apache.org/viewvc/commons/proper/dbcp/branches/DBCP_1_4_x_BRANCH/doc/ManualPoolingDataSourceExample.java?view=markup
        PoolableConnectionFactory poolConnFactory = new PoolableConnectionFactory(connFactory, objectPool, null, null, false, true);
        connPool = new PoolingDataSource(objectPool);
    } else if ("hikaricp".equals(connectionPooler)) {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(driverUrl);
        config.setUsername(user);
        config.setPassword(passwd);
        connPool = new HikariDataSource(config);
    } else if ("none".equals(connectionPooler)) {
        LOG.info("Choosing not to pool JDBC connections");
        connPool = new NoPoolConnectionPool(conf);
    } else {
        throw new RuntimeException("Unknown JDBC connection pooling " + connectionPooler);
    }
}
Also used : PoolingDataSource(org.apache.commons.dbcp.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp.DriverManagerConnectionFactory) HikariDataSource(com.zaxxer.hikari.HikariDataSource) BoneCPDataSource(com.jolbox.bonecp.BoneCPDataSource) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool) HikariConfig(com.zaxxer.hikari.HikariConfig) PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory) ConnectionFactory(org.apache.commons.dbcp.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp.DriverManagerConnectionFactory) BoneCPConfig(com.jolbox.bonecp.BoneCPConfig) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool) ObjectPool(org.apache.commons.pool.ObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory)

Example 2 with DriverManagerConnectionFactory

use of org.apache.commons.dbcp.DriverManagerConnectionFactory in project cloudstack by apache.

the class TransactionLegacy method initDataSource.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void initDataSource(Properties dbProps) {
    try {
        if (dbProps.size() == 0)
            return;
        s_dbHAEnabled = Boolean.valueOf(dbProps.getProperty("db.ha.enabled"));
        s_logger.info("Is Data Base High Availiability enabled? Ans : " + s_dbHAEnabled);
        String loadBalanceStrategy = dbProps.getProperty("db.ha.loadBalanceStrategy");
        // FIXME:  If params are missing...default them????
        final int cloudMaxActive = Integer.parseInt(dbProps.getProperty("db.cloud.maxActive"));
        final int cloudMaxIdle = Integer.parseInt(dbProps.getProperty("db.cloud.maxIdle"));
        final long cloudMaxWait = Long.parseLong(dbProps.getProperty("db.cloud.maxWait"));
        final String cloudUsername = dbProps.getProperty("db.cloud.username");
        final String cloudPassword = dbProps.getProperty("db.cloud.password");
        final String cloudHost = dbProps.getProperty("db.cloud.host");
        final String cloudDriver = dbProps.getProperty("db.cloud.driver");
        final int cloudPort = Integer.parseInt(dbProps.getProperty("db.cloud.port"));
        final String cloudDbName = dbProps.getProperty("db.cloud.name");
        final boolean cloudAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.cloud.autoReconnect"));
        final String cloudValidationQuery = dbProps.getProperty("db.cloud.validationQuery");
        final String cloudIsolationLevel = dbProps.getProperty("db.cloud.isolation.level");
        int isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
        if (cloudIsolationLevel == null) {
            isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
        } else if (cloudIsolationLevel.equalsIgnoreCase("readcommitted")) {
            isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
        } else if (cloudIsolationLevel.equalsIgnoreCase("repeatableread")) {
            isolationLevel = Connection.TRANSACTION_REPEATABLE_READ;
        } else if (cloudIsolationLevel.equalsIgnoreCase("serializable")) {
            isolationLevel = Connection.TRANSACTION_SERIALIZABLE;
        } else if (cloudIsolationLevel.equalsIgnoreCase("readuncommitted")) {
            isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
        } else {
            s_logger.warn("Unknown isolation level " + cloudIsolationLevel + ".  Using read uncommitted");
        }
        final boolean cloudTestOnBorrow = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testOnBorrow"));
        final boolean cloudTestWhileIdle = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testWhileIdle"));
        final long cloudTimeBtwEvictionRunsMillis = Long.parseLong(dbProps.getProperty("db.cloud.timeBetweenEvictionRunsMillis"));
        final long cloudMinEvcitableIdleTimeMillis = Long.parseLong(dbProps.getProperty("db.cloud.minEvictableIdleTimeMillis"));
        final boolean cloudPoolPreparedStatements = Boolean.parseBoolean(dbProps.getProperty("db.cloud.poolPreparedStatements"));
        final String url = dbProps.getProperty("db.cloud.url.params");
        String cloudDbHAParams = null;
        String cloudSlaves = null;
        if (s_dbHAEnabled) {
            cloudDbHAParams = getDBHAParams("cloud", dbProps);
            cloudSlaves = dbProps.getProperty("db.cloud.slaves");
            s_logger.info("The slaves configured for Cloud Data base is/are : " + cloudSlaves);
        }
        final boolean useSSL = Boolean.parseBoolean(dbProps.getProperty("db.cloud.useSSL"));
        if (useSSL) {
            System.setProperty("javax.net.ssl.keyStore", dbProps.getProperty("db.cloud.keyStore"));
            System.setProperty("javax.net.ssl.keyStorePassword", dbProps.getProperty("db.cloud.keyStorePassword"));
            System.setProperty("javax.net.ssl.trustStore", dbProps.getProperty("db.cloud.trustStore"));
            System.setProperty("javax.net.ssl.trustStorePassword", dbProps.getProperty("db.cloud.trustStorePassword"));
        }
        final GenericObjectPool cloudConnectionPool = new GenericObjectPool(null, cloudMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, cloudMaxWait, cloudMaxIdle, cloudTestOnBorrow, false, cloudTimeBtwEvictionRunsMillis, 1, cloudMinEvcitableIdleTimeMillis, cloudTestWhileIdle);
        final String cloudConnectionUri = cloudDriver + "://" + cloudHost + (s_dbHAEnabled ? "," + cloudSlaves : "") + ":" + cloudPort + "/" + cloudDbName + "?autoReconnect=" + cloudAutoReconnect + (url != null ? "&" + url : "") + (useSSL ? "&useSSL=true" : "") + (s_dbHAEnabled ? "&" + cloudDbHAParams : "") + (s_dbHAEnabled ? "&loadBalanceStrategy=" + loadBalanceStrategy : "");
        DriverLoader.loadDriver(cloudDriver);
        final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory(cloudConnectionUri, cloudUsername, cloudPassword);
        final KeyedObjectPoolFactory poolableObjFactory = (cloudPoolPreparedStatements ? new StackKeyedObjectPoolFactory() : null);
        final PoolableConnectionFactory cloudPoolableConnectionFactory = new PoolableConnectionFactory(cloudConnectionFactory, cloudConnectionPool, poolableObjFactory, cloudValidationQuery, false, false, isolationLevel);
        // Default Data Source for CloudStack
        s_ds = new PoolingDataSource(cloudPoolableConnectionFactory.getPool());
        // Configure the usage db
        final int usageMaxActive = Integer.parseInt(dbProps.getProperty("db.usage.maxActive"));
        final int usageMaxIdle = Integer.parseInt(dbProps.getProperty("db.usage.maxIdle"));
        final long usageMaxWait = Long.parseLong(dbProps.getProperty("db.usage.maxWait"));
        final String usageUsername = dbProps.getProperty("db.usage.username");
        final String usagePassword = dbProps.getProperty("db.usage.password");
        final String usageHost = dbProps.getProperty("db.usage.host");
        final String usageDriver = dbProps.getProperty("db.usage.driver");
        final int usagePort = Integer.parseInt(dbProps.getProperty("db.usage.port"));
        final String usageDbName = dbProps.getProperty("db.usage.name");
        final boolean usageAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.usage.autoReconnect"));
        final String usageUrl = dbProps.getProperty("db.usage.url.params");
        final GenericObjectPool usageConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, usageMaxWait, usageMaxIdle);
        final String usageConnectionUri = usageDriver + "://" + usageHost + (s_dbHAEnabled ? "," + dbProps.getProperty("db.cloud.slaves") : "") + ":" + usagePort + "/" + usageDbName + "?autoReconnect=" + usageAutoReconnect + (usageUrl != null ? "&" + usageUrl : "") + (s_dbHAEnabled ? "&" + getDBHAParams("usage", dbProps) : "") + (s_dbHAEnabled ? "&loadBalanceStrategy=" + loadBalanceStrategy : "");
        DriverLoader.loadDriver(usageDriver);
        final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory(usageConnectionUri, usageUsername, usagePassword);
        final PoolableConnectionFactory usagePoolableConnectionFactory = new PoolableConnectionFactory(usageConnectionFactory, usageConnectionPool, new StackKeyedObjectPoolFactory(), null, false, false);
        // Data Source for usage server
        s_usageDS = new PoolingDataSource(usagePoolableConnectionFactory.getPool());
        try {
            // Configure the simulator db
            final int simulatorMaxActive = Integer.parseInt(dbProps.getProperty("db.simulator.maxActive"));
            final int simulatorMaxIdle = Integer.parseInt(dbProps.getProperty("db.simulator.maxIdle"));
            final long simulatorMaxWait = Long.parseLong(dbProps.getProperty("db.simulator.maxWait"));
            final String simulatorUsername = dbProps.getProperty("db.simulator.username");
            final String simulatorPassword = dbProps.getProperty("db.simulator.password");
            final String simulatorHost = dbProps.getProperty("db.simulator.host");
            final String simulatorDriver = dbProps.getProperty("db.simulator.driver");
            final int simulatorPort = Integer.parseInt(dbProps.getProperty("db.simulator.port"));
            final String simulatorDbName = dbProps.getProperty("db.simulator.name");
            final boolean simulatorAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.simulator.autoReconnect"));
            final GenericObjectPool simulatorConnectionPool = new GenericObjectPool(null, simulatorMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, simulatorMaxWait, simulatorMaxIdle);
            final String simulatorConnectionUri = simulatorDriver + "://" + simulatorHost + ":" + simulatorPort + "/" + simulatorDbName + "?autoReconnect=" + simulatorAutoReconnect;
            DriverLoader.loadDriver(simulatorDriver);
            final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory(simulatorConnectionUri, simulatorUsername, simulatorPassword);
            final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory(simulatorConnectionFactory, simulatorConnectionPool, new StackKeyedObjectPoolFactory(), null, false, false);
            s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool());
        } catch (Exception e) {
            s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS");
        }
    } catch (final Exception e) {
        s_ds = getDefaultDataSource("cloud");
        s_usageDS = getDefaultDataSource("cloud_usage");
        s_simulatorDS = getDefaultDataSource("cloud_simulator");
        s_logger.warn("Unable to load db configuration, using defaults with 5 connections. Falling back on assumed datasource on localhost:3306 using username:password=cloud:cloud. Please check your configuration", e);
    }
}
Also used : PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory) ConnectionFactory(org.apache.commons.dbcp.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp.DriverManagerConnectionFactory) PoolingDataSource(org.apache.commons.dbcp.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp.DriverManagerConnectionFactory) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool) StackKeyedObjectPoolFactory(org.apache.commons.pool.impl.StackKeyedObjectPoolFactory) Savepoint(java.sql.Savepoint) StackKeyedObjectPoolFactory(org.apache.commons.pool.impl.StackKeyedObjectPoolFactory) KeyedObjectPoolFactory(org.apache.commons.pool.KeyedObjectPoolFactory) PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 3 with DriverManagerConnectionFactory

use of org.apache.commons.dbcp.DriverManagerConnectionFactory in project cloudstack by apache.

the class TransactionLegacy method getDefaultDataSource.

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

Aggregations

ConnectionFactory (org.apache.commons.dbcp.ConnectionFactory)3 DriverManagerConnectionFactory (org.apache.commons.dbcp.DriverManagerConnectionFactory)3 PoolableConnectionFactory (org.apache.commons.dbcp.PoolableConnectionFactory)3 PoolingDataSource (org.apache.commons.dbcp.PoolingDataSource)3 GenericObjectPool (org.apache.commons.pool.impl.GenericObjectPool)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 BoneCPConfig (com.jolbox.bonecp.BoneCPConfig)1 BoneCPDataSource (com.jolbox.bonecp.BoneCPDataSource)1 HikariConfig (com.zaxxer.hikari.HikariConfig)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Savepoint (java.sql.Savepoint)1 KeyedObjectPoolFactory (org.apache.commons.pool.KeyedObjectPoolFactory)1 ObjectPool (org.apache.commons.pool.ObjectPool)1 StackKeyedObjectPoolFactory (org.apache.commons.pool.impl.StackKeyedObjectPoolFactory)1