Java Code Examples for org.apache.commons.dbcp2.BasicDataSource#setConnectionProperties()

The following examples show how to use org.apache.commons.dbcp2.BasicDataSource#setConnectionProperties() . 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: ConnectionManager.java    From gocd with Apache License 2.0 6 votes vote down vote up
private BasicDataSource createDataSource() {
    final DbProperties dbProperties = getDbProperties();
    BasicDataSource basicDataSource = new BasicDataSource();

    if (isBlank(dbProperties.url())) {
        return DefaultH2DataSource.defaultH2DataSource(basicDataSource, dbProperties);
    }

    basicDataSource.setDriverClassName(dbProperties.driver());
    basicDataSource.setUrl(dbProperties.url());
    basicDataSource.setUsername(dbProperties.user());
    basicDataSource.setPassword(dbProperties.password());

    basicDataSource.setMaxTotal(dbProperties.maxTotal());
    basicDataSource.setMaxIdle(dbProperties.maxIdle());
    basicDataSource.setConnectionProperties(dbProperties.connectionPropertiesAsString());
    return basicDataSource;
}
 
Example 2
Source File: JdbcIO.java    From beam with Apache License 2.0 5 votes vote down vote up
DataSource buildDatasource() {
  if (getDataSource() == null) {
    BasicDataSource basicDataSource = new BasicDataSource();
    if (getDriverClassName() != null) {
      basicDataSource.setDriverClassName(getDriverClassName().get());
    }
    if (getUrl() != null) {
      basicDataSource.setUrl(getUrl().get());
    }
    if (getUsername() != null) {
      basicDataSource.setUsername(getUsername().get());
    }
    if (getPassword() != null) {
      basicDataSource.setPassword(getPassword().get());
    }
    if (getConnectionProperties() != null && getConnectionProperties().get() != null) {
      basicDataSource.setConnectionProperties(getConnectionProperties().get());
    }
    if (getConnectionInitSqls() != null
        && getConnectionInitSqls().get() != null
        && !getConnectionInitSqls().get().isEmpty()) {
      basicDataSource.setConnectionInitSqls(getConnectionInitSqls().get());
    }

    return basicDataSource;
  }
  return getDataSource();
}
 
Example 3
Source File: DynamicJdbcIO.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
DataSource buildDatasource() {
  BasicDataSource basicDataSource = new BasicDataSource();
  if (getDriverClassName() != null) {
    if (getDriverClassName().get() == null) {
      throw new RuntimeException("Driver class name is required.");
    }
    basicDataSource.setDriverClassName(getDriverClassName().get());
  }
  if (getUrl() != null) {
    if (getUrl().get() == null) {
      throw new RuntimeException("Connection url is required.");
    }
    basicDataSource.setUrl(getUrl().get());
  }
  if (getUsername() != null) {
    basicDataSource.setUsername(getUsername().get());
  }
  if (getPassword() != null) {
    basicDataSource.setPassword(getPassword().get());
  }
  if (getConnectionProperties() != null && getConnectionProperties().get() != null) {
    basicDataSource.setConnectionProperties(getConnectionProperties().get());
  }

  /**
   * Since the jdbc connection connection class might have dependencies that are not available
   * to the template, we will localize the user provided dependencies from GCS and create a new
   * {@link URLClassLoader} that is added to the {@link
   * BasicDataSource#setDriverClassLoader(ClassLoader)}
   */
  if (getDriverJars() != null) {
    ClassLoader urlClassLoader = getNewClassLoader(getDriverJars());
    basicDataSource.setDriverClassLoader(urlClassLoader);
  }

  // wrapping the datasource as a pooling datasource
  DataSourceConnectionFactory connectionFactory =
      new DataSourceConnectionFactory(basicDataSource);
  PoolableConnectionFactory poolableConnectionFactory =
      new PoolableConnectionFactory(connectionFactory, null);
  GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
  poolConfig.setMaxTotal(1);
  poolConfig.setMinIdle(0);
  poolConfig.setMinEvictableIdleTimeMillis(10000);
  poolConfig.setSoftMinEvictableIdleTimeMillis(30000);
  GenericObjectPool connectionPool =
      new GenericObjectPool(poolableConnectionFactory, poolConfig);
  poolableConnectionFactory.setPool(connectionPool);
  poolableConnectionFactory.setDefaultAutoCommit(false);
  poolableConnectionFactory.setDefaultReadOnly(false);
  PoolingDataSource poolingDataSource = new PoolingDataSource(connectionPool);
  return poolingDataSource;
}