Package org.apache.commons.dbcp

Examples of org.apache.commons.dbcp.PoolingDataSource


    final HashMap<String, String> attrs = new HashMap<String, String>();
    attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "org.postgresql.Driver" );
    attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql://localhost:5432/hibernate" );
    con.setAttributes( attrs );
    try {
      final PoolingDataSource poolingDataSource = PooledDatasourceHelper.setupPooledDataSource( con );
      fail( "Expecting the exception to be thrown" );
    } catch ( DBDatasourceServiceException ex ) {
      assertNotNull( ex );
    }
  }
View Full Code Here


    attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "" );
    attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql://localhost:5432/hibernate" );
    con.setAttributes( attrs );

    try {
      final PoolingDataSource poolingDataSource = PooledDatasourceHelper.setupPooledDataSource( con );
      fail( "Expecting the exception to be thrown" );
    } catch ( DBDatasourceServiceException ex ) {
      assertNotNull( ex );
    }
View Full Code Here

    attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "" );
    attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql://localhost:5432/hibernate" );
    con.setAttributes( attrs );

    try {
      final PoolingDataSource poolingDataSource = PooledDatasourceHelper.setupPooledDataSource( con );
      fail( "Expecting the exception to be thrown" );
    } catch ( DBDatasourceServiceException ex ) {
      assertNotNull( ex );
    }
View Full Code Here

public class PooledDatasourceHelper {

  public static PoolingDataSource setupPooledDataSource( IDatabaseConnection databaseConnection )
    throws DBDatasourceServiceException {
    PoolingDataSource poolingDataSource = null;
    String driverClass = null;
    String url = null;
    try {
      ICacheManager cacheManager = PentahoSystem.getCacheManager( null );
      IDatabaseDialectService databaseDialectService = PentahoSystem.get( IDatabaseDialectService.class );
      if ( databaseDialectService == null ) {
        throw new DBDatasourceServiceException( Messages.getInstance().getErrorString(
            "PooledDatasourceHelper.ERROR_0005_UNABLE_TO_POOL_DATASOURCE_NO_DIALECT_SERVICE",
            databaseConnection.getName() ) );
      }
      IDatabaseDialect dialect = databaseDialectService.getDialect( databaseConnection );
      if ( dialect == null || dialect.getDatabaseType() == null ) {
        throw new DBDatasourceServiceException( Messages.getInstance().getErrorString(
            "PooledDatasourceHelper.ERROR_0004_UNABLE_TO_POOL_DATASOURCE_NO_DIALECT", databaseConnection.getName() ) );
      }
      if ( databaseConnection.getDatabaseType().getShortName().equals( "GENERIC" ) ) { //$NON-NLS-1$
        driverClass = databaseConnection.getAttributes().get( GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS );
        if ( StringUtils.isEmpty( driverClass ) ) {
          throw new DBDatasourceServiceException( Messages.getInstance().getErrorString(
              "PooledDatasourceHelper.ERROR_0006_UNABLE_TO_POOL_DATASOURCE_NO_CLASSNAME", databaseConnection.getName() ) );
        }

      } else {
        driverClass = dialect.getNativeDriver();
        if ( StringUtils.isEmpty( driverClass ) ) {
          throw new DBDatasourceServiceException( Messages.getInstance().getErrorString(
              "PooledDatasourceHelper.ERROR_0007_UNABLE_TO_POOL_DATASOURCE_NO_DRIVER", databaseConnection.getName() ) );
        }
      }
      try {
        url = dialect.getURLWithExtraOptions( databaseConnection );
      } catch ( DatabaseDialectException e ) {
        url = null;
      }

      // Read default connection pooling parameter
      String maxdleConn = PentahoSystem.getSystemSetting( "dbcp-defaults/max-idle-conn", null ); //$NON-NLS-1$
      String minIdleConn = PentahoSystem.getSystemSetting( "dbcp-defaults/min-idle-conn", null ); //$NON-NLS-1$   
      String maxActConn = PentahoSystem.getSystemSetting( "dbcp-defaults/max-act-conn", null ); //$NON-NLS-1$
      String validQuery = null;
      String whenExhaustedAction = PentahoSystem.getSystemSetting( "dbcp-defaults/when-exhausted-action", null ); //$NON-NLS-1$
      String wait = PentahoSystem.getSystemSetting( "dbcp-defaults/wait", null ); //$NON-NLS-1$
      String testWhileIdleValue = PentahoSystem.getSystemSetting( "dbcp-defaults/test-while-idle", null ); //$NON-NLS-1$
      String testOnBorrowValue = PentahoSystem.getSystemSetting( "dbcp-defaults/test-on-borrow", null ); //$NON-NLS-1$
      String testOnReturnValue = PentahoSystem.getSystemSetting( "dbcp-defaults/test-on-return", null ); //$NON-NLS-1$

      // property initialization
      boolean testWhileIdle =
          !StringUtil.isEmpty( testWhileIdleValue ) ? Boolean.parseBoolean( testWhileIdleValue ) : false;
      boolean testOnBorrow =
          !StringUtil.isEmpty( testOnBorrowValue ) ? Boolean.parseBoolean( testOnBorrowValue ) : false;
      boolean testOnReturn =
          !StringUtil.isEmpty( testOnReturnValue ) ? Boolean.parseBoolean( testOnReturnValue ) : false;
      int maxActiveConnection = !StringUtil.isEmpty( maxActConn ) ? Integer.parseInt( maxActConn ) : -1;
      long waitTime = !StringUtil.isEmpty( wait ) ? Integer.parseInt( wait ) : -1;
      byte whenExhaustedActionType =
          !StringUtil.isEmpty( whenExhaustedAction ) ? Byte.parseByte( whenExhaustedAction )
              : GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
      int minIdleConnection = !StringUtil.isEmpty( minIdleConn ) ? Integer.parseInt( minIdleConn ) : -1;
      int maxIdleConnection = !StringUtil.isEmpty( maxdleConn ) ? Integer.parseInt( maxdleConn ) : -1;

      // setting properties according to user specifications
      Map<String, String> attributes = databaseConnection.getConnectionPoolingProperties();

      if ( attributes.containsKey( IDBDatasourceService.MAX_ACTIVE_KEY )
          && NumberUtils.isNumber( attributes.get( IDBDatasourceService.MAX_ACTIVE_KEY ) ) ) {
        maxActiveConnection = Integer.parseInt( attributes.get( IDBDatasourceService.MAX_ACTIVE_KEY ) );
      }
      if ( attributes.containsKey( IDBDatasourceService.MAX_WAIT_KEY )
          && NumberUtils.isNumber( attributes.get( IDBDatasourceService.MAX_WAIT_KEY ) ) ) {
        waitTime = Integer.parseInt( attributes.get( IDBDatasourceService.MAX_WAIT_KEY ) );
      }
      if ( attributes.containsKey( IDBDatasourceService.MIN_IDLE_KEY )
          && NumberUtils.isNumber( attributes.get( IDBDatasourceService.MIN_IDLE_KEY ) ) ) {
        minIdleConnection = Integer.parseInt( attributes.get( IDBDatasourceService.MIN_IDLE_KEY ) );
      }
      if ( attributes.containsKey( IDBDatasourceService.MAX_IDLE_KEY )
          && NumberUtils.isNumber( attributes.get( IDBDatasourceService.MAX_IDLE_KEY ) ) ) {
        maxIdleConnection = Integer.parseInt( attributes.get( IDBDatasourceService.MAX_IDLE_KEY ) );
      }
      if ( attributes.containsKey( IDBDatasourceService.QUERY_KEY ) ) {
        validQuery = attributes.get( IDBDatasourceService.QUERY_KEY );
      }
      if ( attributes.containsKey( IDBDatasourceService.TEST_ON_BORROW ) ) {
        testOnBorrow = Boolean.parseBoolean( attributes.get( IDBDatasourceService.TEST_ON_BORROW ) );
      }
      if ( attributes.containsKey( IDBDatasourceService.TEST_ON_RETURN ) ) {
        testOnReturn = Boolean.parseBoolean( attributes.get( IDBDatasourceService.TEST_ON_RETURN ) );
      }
      if ( attributes.containsKey( IDBDatasourceService.TEST_WHILE_IDLE ) ) {
        testWhileIdle = Boolean.parseBoolean( attributes.get( IDBDatasourceService.TEST_WHILE_IDLE ) );
      }

      poolingDataSource = new PoolingDataSource();
      Class.forName( driverClass );
      // As the name says, this is a generic pool; it returns basic Object-class objects.
      GenericObjectPool pool = new GenericObjectPool( null );

      // if removedAbandoned = true, then an AbandonedObjectPool object will take GenericObjectPool's place
      if ( attributes.containsKey( IDBDatasourceService.REMOVE_ABANDONED )
          && true == Boolean.parseBoolean( attributes.get( IDBDatasourceService.REMOVE_ABANDONED ) ) ) {

        AbandonedConfig config = new AbandonedConfig();
        config.setRemoveAbandoned( Boolean.parseBoolean( attributes.get( IDBDatasourceService.REMOVE_ABANDONED ) ) );

        if ( attributes.containsKey( IDBDatasourceService.LOG_ABANDONED ) ) {
          config.setLogAbandoned( Boolean.parseBoolean( attributes.get( IDBDatasourceService.LOG_ABANDONED ) ) );
        }

        if ( attributes.containsKey( IDBDatasourceService.REMOVE_ABANDONED_TIMEOUT )
            && NumberUtils.isNumber( attributes.get( IDBDatasourceService.REMOVE_ABANDONED_TIMEOUT ) ) ) {
          config.setRemoveAbandonedTimeout( Integer.parseInt( attributes
              .get( IDBDatasourceService.REMOVE_ABANDONED_TIMEOUT ) ) );
        }

        pool = new AbandonedObjectPool( null, config );
      }

      pool.setWhenExhaustedAction( whenExhaustedActionType );

      // Tuning the connection pool
      pool.setMaxActive( maxActiveConnection );
      pool.setMaxIdle( maxIdleConnection );
      pool.setMaxWait( waitTime );
      pool.setMinIdle( minIdleConnection );
      pool.setTestWhileIdle( testWhileIdle );
      pool.setTestOnReturn( testOnReturn );
      pool.setTestOnBorrow( testOnBorrow );
      pool.setTestWhileIdle( testWhileIdle );

      if ( attributes.containsKey( IDBDatasourceService.TIME_BETWEEN_EVICTION_RUNS_MILLIS )
          && NumberUtils.isNumber( attributes.get( IDBDatasourceService.TIME_BETWEEN_EVICTION_RUNS_MILLIS ) ) ) {
        pool.setTimeBetweenEvictionRunsMillis( Long.parseLong( attributes
            .get( IDBDatasourceService.TIME_BETWEEN_EVICTION_RUNS_MILLIS ) ) );
      }

      /*
       * ConnectionFactory creates connections on behalf of the pool. Here, we use the DriverManagerConnectionFactory
       * because that essentially uses DriverManager as the source of connections.
       */
      ConnectionFactory factory =
          new DriverManagerConnectionFactory( url, databaseConnection.getUsername(), databaseConnection.getPassword() );

      boolean defaultReadOnly =
          attributes.containsKey( IDBDatasourceService.DEFAULT_READ_ONLY ) ? Boolean.parseBoolean( attributes
              .get( IDBDatasourceService.TEST_WHILE_IDLE ) ) : false; // default to false

      boolean defaultAutoCommit =
          attributes.containsKey( IDBDatasourceService.DEFAULT_AUTO_COMMIT ) ? Boolean.parseBoolean( attributes
              .get( IDBDatasourceService.DEFAULT_AUTO_COMMIT ) ) : true; // default to true

      KeyedObjectPoolFactory kopf = null;

      if ( attributes.containsKey( IDBDatasourceService.POOL_PREPARED_STATEMENTS )
          && true == Boolean.parseBoolean( attributes.get( IDBDatasourceService.POOL_PREPARED_STATEMENTS ) ) ) {

        int maxOpenPreparedStatements = -1; // unlimited

        if ( attributes.containsKey( IDBDatasourceService.MAX_OPEN_PREPARED_STATEMENTS )
            && NumberUtils.isNumber( attributes.get( IDBDatasourceService.MAX_OPEN_PREPARED_STATEMENTS ) ) ) {

          maxOpenPreparedStatements =
              Integer.parseInt( attributes.get( IDBDatasourceService.MAX_OPEN_PREPARED_STATEMENTS ) );
        }

        kopf =
            new GenericKeyedObjectPoolFactory( null, pool.getMaxActive(), pool.getWhenExhaustedAction(), pool
                .getMaxWait(), pool.getMaxIdle(), maxOpenPreparedStatements );
      }

      /*
       * Puts pool-specific wrappers on factory connections. For clarification: "[PoolableConnection]Factory," not
       * "Poolable[ConnectionFactory]."
       */
      PoolableConnectionFactory pcf = new PoolableConnectionFactory( factory, // ConnectionFactory
          pool, // ObjectPool
          kopf, // KeyedObjectPoolFactory
          validQuery, // String (validation query)
          defaultReadOnly, // boolean (default to read-only?)
          defaultAutoCommit // boolean (default to auto-commit statements?)
          );

      if ( attributes.containsKey( IDBDatasourceService.DEFAULT_TRANSACTION_ISOLATION )
          && !IDBDatasourceService.TRANSACTION_ISOLATION_NONE_VALUE.equalsIgnoreCase( attributes
              .get( IDBDatasourceService.DEFAULT_TRANSACTION_ISOLATION ) ) ) {
        Isolation isolationLevel =
            Isolation.valueOf( attributes.get( IDBDatasourceService.DEFAULT_TRANSACTION_ISOLATION ) );

        if ( isolationLevel != null ) {
          pcf.setDefaultTransactionIsolation( isolationLevel.value() );
        }
      }

      if ( attributes.containsKey( IDBDatasourceService.DEFAULT_CATALOG ) ) {
        pcf.setDefaultCatalog( attributes.get( IDBDatasourceService.DEFAULT_CATALOG ) );
      }

      /*
       * initialize the pool to X connections
       */
      Logger.debug( PooledDatasourceHelper.class, "Pool defaults to " + maxActiveConnection + " max active/"
          + maxIdleConnection + "max idle" + "with " + waitTime + "wait time"//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
          + " idle connections." ); //$NON-NLS-1$

      for ( int i = 0; i < maxIdleConnection; ++i ) {
        pool.addObject();
      }
      Logger.debug( PooledDatasourceHelper.class, "Pool now has " + pool.getNumActive() + " active/"
          + pool.getNumIdle() + " idle connections." ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      /*
       * All of this is wrapped in a DataSource, which client code should already know how to handle (since it's the
       * same class of object they'd fetch via the container's JNDI tree
       */
      poolingDataSource.setPool( pool );

      if ( attributes.containsKey( IDBDatasourceService.ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED ) ) {
        poolingDataSource.setAccessToUnderlyingConnectionAllowed( Boolean.parseBoolean( attributes
            .get( IDBDatasourceService.ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED ) ) );
      }

      // store the pool, so we can get to it later
      cacheManager.putInRegionCache( IDBDatasourceService.JDBC_POOL, databaseConnection.getName(), pool );
View Full Code Here

    @Test
    public void testOperationCollection() throws SQLException {
        DataSourceConnectionFactory connFactory = new DataSourceConnectionFactory(dataSource);
        ObjectPool connPool = new GenericObjectPool();
        PoolableConnectionFactory poolFactory = new PoolableConnectionFactory(connFactory, connPool, null, null, false, true);
        PoolingDataSource poolDs = new PoolingDataSource(poolFactory.getPool());
        String sql = "select * from appointment where owner = 'Agim'";
        Connection c = poolDs.getConnection();
        try {
            PreparedStatement ps = c.prepareStatement(sql);
            try {
                System.out.println("Prepared statement=" + ps.getClass());

View Full Code Here

        connectURI, username, password);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
        connectionFactory, connectionPool, null, null, false, true);

    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

    return dataSource;
  }
View Full Code Here

            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 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 ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + usageHost + ":" + usagePort + "/" + usageDbName +
                    "?autoReconnect=" + usageAutoReconnect + (usageUrl != null ? "&" + usageUrl : ""), 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());

            // Configure awsapi db
            final String awsapiDbName = dbProps.getProperty("db.awsapi.name");
            final GenericObjectPool awsapiConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
                    usageMaxWait, usageMaxIdle);
            final ConnectionFactory awsapiConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + cloudHost + ":" + cloudPort + "/" + awsapiDbName +
                    "?autoReconnect=" + usageAutoReconnect, cloudUsername, cloudPassword);
            final PoolableConnectionFactory awsapiPoolableConnectionFactory = new PoolableConnectionFactory(awsapiConnectionFactory, awsapiConnectionPool,
                    new StackKeyedObjectPoolFactory(), null, false, false);

            // Data Source for awsapi
            s_awsapiDS = new PoolingDataSource(awsapiPoolableConnectionFactory.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 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 ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + simulatorHost + ":" + simulatorPort + "/" + simulatorDbName +
                        "?autoReconnect=" + simulatorAutoReconnect, 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");
View Full Code Here

        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());
    }
View Full Code Here

            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 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 ConnectionFactory usageConnectionFactory =
                new DriverManagerConnectionFactory("jdbc:mysql://" + usageHost + (s_dbHAEnabled ? "," + dbProps.getProperty("db.cloud.slaves") : "") + ":" + usagePort +
                    "/" + usageDbName + "?autoReconnect=" + usageAutoReconnect + (usageUrl != null ? "&" + usageUrl : "") +
                    (s_dbHAEnabled ? "&" + getDBHAParams("usage", dbProps) : "") + (s_dbHAEnabled ? "&loadBalanceStrategy=" + loadBalanceStrategy : ""), 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());

            // Configure awsapi db
            final String awsapiDbName = dbProps.getProperty("db.awsapi.name");
            final GenericObjectPool awsapiConnectionPool =
                new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, usageMaxWait, usageMaxIdle);
            final ConnectionFactory awsapiConnectionFactory =
                new DriverManagerConnectionFactory("jdbc:mysql://" + cloudHost + (s_dbHAEnabled ? "," + cloudSlaves : "") + ":" + cloudPort + "/" + awsapiDbName +
                    "?autoReconnect=" + cloudAutoReconnect + (s_dbHAEnabled ? "&" + cloudDbHAParams : "") +
                    (s_dbHAEnabled ? "&loadBalanceStrategy=" + loadBalanceStrategy : ""), cloudUsername, cloudPassword);
            final PoolableConnectionFactory awsapiPoolableConnectionFactory =
                new PoolableConnectionFactory(awsapiConnectionFactory, awsapiConnectionPool, new StackKeyedObjectPoolFactory(), null, false, false);

            // Data Source for awsapi
            s_awsapiDS = new PoolingDataSource(awsapiPoolableConnectionFactory.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 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 ConnectionFactory simulatorConnectionFactory =
                    new DriverManagerConnectionFactory("jdbc:mysql://" + simulatorHost + ":" + simulatorPort + "/" + simulatorDbName + "?autoReconnect=" +
                        simulatorAutoReconnect, 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");
View Full Code Here

    @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());
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.dbcp.PoolingDataSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.