Java Code Examples for org.apache.commons.dbcp.BasicDataSource#addConnectionProperty()

The following examples show how to use org.apache.commons.dbcp.BasicDataSource#addConnectionProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbcDataSourceFactory.java    From obevo with Apache License 2.0 6 votes vote down vote up
private static DataSource createFromJdbcUrl(Class<? extends Driver> driverClass, String url,
        Credential credential, int numThreads, ImmutableList<String> initSqls, Properties extraConnectionProperties) {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverClass.getName());
    // TODO validate non-null host name, notably for postgresl jdbc url
    dataSource.setUrl(url);
    dataSource.setUsername(credential.getUsername());
    dataSource.setPassword(credential.getPassword());

    // connection pool settings
    dataSource.setInitialSize(numThreads);
    dataSource.setMaxActive(numThreads);
    // keep the connections open if possible; only close them via the removeAbandonedTimeout feature
    dataSource.setMaxIdle(numThreads);
    dataSource.setMinIdle(0);
    dataSource.setRemoveAbandonedTimeout(300);

    dataSource.setConnectionInitSqls(initSqls.castToList());
    if (extraConnectionProperties != null) {
        for (String key : extraConnectionProperties.stringPropertyNames()) {
            dataSource.addConnectionProperty(key, extraConnectionProperties.getProperty(key));
        }
    }

    return dataSource;
}
 
Example 2
Source File: ConnectionPoolUtil.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private static void setCredentials( BasicDataSource ds, DatabaseMeta databaseMeta, String partitionId )
  throws KettleDatabaseException {

  String userName = databaseMeta.environmentSubstitute( databaseMeta.getUsername() );
  String password = databaseMeta.environmentSubstitute( databaseMeta.getPassword() );
  password = Encr.decryptPasswordOptionallyEncrypted( password );

  ds.addConnectionProperty( "user", Const.NVL( userName, "" ) );
  ds.addConnectionProperty( "password", Const.NVL( password, "" ) );
}
 
Example 3
Source File: PooledConnectionDBCP.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private static void setupDataSource(Isolation isolation) {
  ds = new BasicDataSource();
  Endpoint locatorEndPoint = (Endpoint) (NetworkServerHelper.getNetworkLocatorEndpoints()).get(0);
  String hostname = getHostNameFromEndpoint(locatorEndPoint);
  int port = getPortFromEndpoint(locatorEndPoint); 
  
  connProp.putAll(getExtraConnProp());
  
  try {
    ds.setDriverClassName(driver);
    ds.setUrl(protocol + hostname+ ":" + port);
    
    ds.setMaxActive(100);
    
    if (isolation == Isolation.NONE) {
    } else if (isolation == Isolation.READ_COMMITTED) {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
      ds.setDefaultAutoCommit(false);
    } else {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
      ds.setDefaultAutoCommit(false);
    }
    
    StringBuilder sb = new StringBuilder();
    
    for (Iterator<?> iter = connProp.entrySet().iterator(); iter.hasNext(); ) {
      Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();
      ds.addConnectionProperty(entry.getKey(), entry.getValue()); //add additional conn prop
      
      sb.append(entry.getKey() + " is set to " + entry.getValue() +"\n");
    }
         
    dsSet = true;
    Log.getLogWriter().info("basic source url is set as " + ds.getUrl());
    Log.getLogWriter().info("basic data source setting the following connection prop: " + sb.toString());
    
  } catch (Exception e) {
    throw new TestException("could not set data source" + TestHelper.getStackTrace(e));
  }
}
 
Example 4
Source File: PooledConnectionDBCP.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private static void setupDataSource(Isolation isolation) {
  ds = new BasicDataSource();
  Endpoint locatorEndPoint = (Endpoint) (NetworkServerHelper.getNetworkLocatorEndpoints()).get(0);
  String hostname = getHostNameFromEndpoint(locatorEndPoint);
  int port = getPortFromEndpoint(locatorEndPoint); 
  
  connProp.putAll(getExtraConnProp());
  
  try {
    ds.setDriverClassName(driver);
    ds.setUrl(protocol + hostname+ ":" + port);
    
    ds.setMaxActive(100);
    
    if (isolation == Isolation.NONE) {
    } else if (isolation == Isolation.READ_COMMITTED) {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
      ds.setDefaultAutoCommit(false);
    } else {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
      ds.setDefaultAutoCommit(false);
    }
    
    StringBuilder sb = new StringBuilder();
    
    for (Iterator<?> iter = connProp.entrySet().iterator(); iter.hasNext(); ) {
      Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();
      ds.addConnectionProperty(entry.getKey(), entry.getValue()); //add additional conn prop
      
      sb.append(entry.getKey() + " is set to " + entry.getValue() +"\n");
    }
         
    dsSet = true;
    Log.getLogWriter().info("basic source url is set as " + ds.getUrl());
    Log.getLogWriter().info("basic data source setting the following connection prop: " + sb.toString());
    
  } catch (Exception e) {
    throw new TestException("could not set data source" + TestHelper.getStackTrace(e));
  }
}