Search in sources :

Example 1 with StackKeyedObjectPoolFactory

use of org.apache.commons.pool.impl.StackKeyedObjectPoolFactory 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)

Aggregations

CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Savepoint (java.sql.Savepoint)1 ConnectionFactory (org.apache.commons.dbcp.ConnectionFactory)1 DriverManagerConnectionFactory (org.apache.commons.dbcp.DriverManagerConnectionFactory)1 PoolableConnectionFactory (org.apache.commons.dbcp.PoolableConnectionFactory)1 PoolingDataSource (org.apache.commons.dbcp.PoolingDataSource)1 KeyedObjectPoolFactory (org.apache.commons.pool.KeyedObjectPoolFactory)1 GenericObjectPool (org.apache.commons.pool.impl.GenericObjectPool)1 StackKeyedObjectPoolFactory (org.apache.commons.pool.impl.StackKeyedObjectPoolFactory)1