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

The following examples show how to use org.apache.commons.dbcp2.BasicDataSource#setMaxTotal() . 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: DBCPPool.java    From MtgDesktopCompanion with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void init(String url, String user, String pass, boolean enable) {

	logger.debug("init connection to " + url + ", Pooling="+enable);
	dataSource =  new BasicDataSource();
       dataSource.setUrl(url);
       dataSource.setUsername(user);
       dataSource.setPassword(pass);

       props.entrySet().forEach(ks->{
       	try {
			BeanUtils.setProperty(dataSource, ks.getKey().toString(), ks.getValue());
		} catch (Exception e) {
			logger.error(e);
		} 
       });
       
       if(!enable) {
		  dataSource.setMinIdle(1);
          dataSource.setMaxIdle(1);
          dataSource.setInitialSize(0);
          dataSource.setMaxTotal(1);
	  }
 	}
 
Example 2
Source File: ConnectionUtils.java    From FROST-Server with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static ConnectionSource setupBasicDataSource(Settings settings) {
    LOGGER.info("Setting up BasicDataSource for database connections.");
    String driver = settings.get(TAG_DB_DRIVER, ConnectionUtils.class);
    if (driver.isEmpty()) {
        throw new IllegalArgumentException("Property '" + TAG_DB_DRIVER + "' must be non-empty");
    }
    try {
        Class.forName(driver);
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl(settings.get(TAG_DB_URL, ConnectionUtils.class));
        ds.setUsername(settings.get(TAG_DB_USERNAME, ConnectionUtils.class));
        ds.setPassword(settings.get(TAG_DB_PASSWRD, ConnectionUtils.class));
        ds.setMaxIdle(settings.getInt(TAG_DB_MAXIDLE, ds.getMaxIdle()));
        ds.setMaxTotal(settings.getInt(TAG_DB_MAXCONN, ds.getMaxTotal()));
        ds.setMinIdle(settings.getInt(TAG_DB_MINIDLE, ds.getMinIdle()));
        return new ConnectionSourceBasicDataSource(ds);
    } catch (ClassNotFoundException exc) {
        throw new IllegalArgumentException(exc);
    }
}
 
Example 3
Source File: DataSourceManager.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
private static void createPoolIfAbsent(IDataSource dataSource) {
	Assert.assertNotNull(dataSource, "Missing input datasource");
	Assert.assertNotNull(dataSource.getJdbcPoolConfiguration(), "Connection pool information is not provided");

	logger.debug("Creating connection pool for datasource " + dataSource.getLabel());
	BasicDataSource pool = new BasicDataSource();
	pool.setDriverClassName(dataSource.getDriver());
	pool.setUrl(dataSource.getUrlConnection());
	pool.setUsername(dataSource.getUser());
	pool.setPassword(dataSource.getPwd());
	pool.setMaxTotal(dataSource.getJdbcPoolConfiguration().getMaxTotal());
	pool.setMaxWaitMillis(dataSource.getJdbcPoolConfiguration().getMaxWait());
	pool.setRemoveAbandonedOnBorrow(dataSource.getJdbcPoolConfiguration().getRemoveAbandonedOnBorrow());
	pool.setRemoveAbandonedOnMaintenance(dataSource.getJdbcPoolConfiguration().getRemoveAbandonedOnMaintenance());
	pool.setRemoveAbandonedTimeout(dataSource.getJdbcPoolConfiguration().getAbandonedTimeout());
	pool.setLogAbandoned(dataSource.getJdbcPoolConfiguration().getLogAbandoned());
	pool.setTestOnReturn(dataSource.getJdbcPoolConfiguration().getTestOnReturn());
	pool.setTestWhileIdle(dataSource.getJdbcPoolConfiguration().getTestWhileIdle());
	pool.setTimeBetweenEvictionRunsMillis(dataSource.getJdbcPoolConfiguration().getTimeBetweenEvictionRuns());
	pool.setMinEvictableIdleTimeMillis(dataSource.getJdbcPoolConfiguration().getMinEvictableIdleTimeMillis());
	pool.setValidationQuery(dataSource.getJdbcPoolConfiguration().getValidationQuery());

	dataSources.put(dataSource, pool);
}
 
Example 4
Source File: ConfigTestUtils.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void createDatasource(String dsNameControlKey, SimpleNamingContextBuilder builder, Properties testConfig) {
    String beanName = testConfig.getProperty("jdbc." + dsNameControlKey + ".beanName");
    try {
        String className = testConfig.getProperty("jdbc." + dsNameControlKey + ".driverClassName");
        String url = testConfig.getProperty("jdbc." + dsNameControlKey + ".url");
        String username = testConfig.getProperty("jdbc." + dsNameControlKey + ".username");
        String password = testConfig.getProperty("jdbc." + dsNameControlKey + ".password");
        Class.forName(className);
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setMaxTotal(12);
        ds.setMaxIdle(4);
        ds.setDriverClassName(className);
        builder.bind("java:comp/env/jdbc/" + beanName, ds);
    } catch (Throwable t) {
        throw new RuntimeException("Error on creation datasource '" + beanName + "'", t);
    }
}
 
Example 5
Source File: ResourceFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
private static void internal_dbcp2() throws Exception {

		for (Entry<String, DataServer> entry : Config.nodes().dataServers().entrySet()) {

			BasicDataSource dataSource = new BasicDataSource();

			String url = "jdbc:h2:tcp://" + entry.getKey() + ":" + entry.getValue().getTcpPort() + "/X;JMX="
					+ (entry.getValue().getJmxEnable() ? "TRUE" : "FALSE") + ";CACHE_SIZE="
					+ (entry.getValue().getCacheSize() * 1024);
			dataSource.setDriverClassName(SlicePropertiesBuilder.driver_h2);
			dataSource.setUrl(url);
			dataSource.setInitialSize(0);
			dataSource.setMinIdle(0);
			dataSource.setMaxTotal(50);
			dataSource.setMaxIdle(50);
			dataSource.setUsername("sa");
			dataSource.setTestOnCreate(false);
			dataSource.setTestWhileIdle(false);
			dataSource.setTestOnReturn(false);
			dataSource.setTestOnBorrow(false);
			dataSource.setPassword(Config.token().getPassword());
			String name = Config.nodes().dataServers().name(entry.getValue());
			new Resource(Config.RESOURCE_JDBC_PREFIX + name, dataSource);

		}
	}
 
Example 6
Source File: ResourceFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
private static void external_dbcp2() throws Exception {
	for (ExternalDataSource ds : Config.externalDataSources()) {
		BasicDataSource dataSource = new BasicDataSource();
		dataSource.setDriverClassName(ds.getDriverClassName());
		dataSource.setUrl(ds.getUrl());
		dataSource.setInitialSize(0);
		dataSource.setMinIdle(0);
		dataSource.setMaxTotal(ds.getMaxTotal());
		dataSource.setMaxIdle(ds.getMaxTotal());
		dataSource.setTestOnCreate(false);
		dataSource.setTestWhileIdle(false);
		dataSource.setTestOnReturn(false);
		dataSource.setTestOnBorrow(false);
		dataSource.setUsername(ds.getUsername());
		dataSource.setPassword(ds.getPassword());
		String name = Config.externalDataSources().name(ds);
		new Resource(Config.RESOURCE_JDBC_PREFIX + name, dataSource);
	}
}
 
Example 7
Source File: AbstractMysqlHandler.java    From adt with Apache License 2.0 6 votes vote down vote up
public AbstractMysqlHandler() throws Exception{
        shardCount = Integer.parseInt(System.getProperty(PROP_SHARD_CNT));
        for(int i=0; i<shardCount; i++){
            final BasicDataSource ds = new BasicDataSource();
            ds.setMaxTotal(1024);
            ds.setMaxIdle(1024);
            ds.setMinIdle(0);
            ds.setUrl(System.getProperty(String.format(PROP_SHARD_URL, i)));
            ds.setUsername(System.getProperty(String.format(PROP_SHARD_USERNAME, i)));
            ds.setPassword(System.getProperty(String.format(PROP_SHARD_PASSWORD, i)));
            dataSourceList.add(ds);
        }
        
//        final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
//        scriptEngine = scriptEngineManager.getEngineByName(SCRIPT_ENGINE_NAME);
//        scriptEngine.eval(System.getProperty(PROP_SCRIPT));
        
    }
 
Example 8
Source File: SinkToMySQL.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
private static Connection getConnection(BasicDataSource dataSource) {
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    //注意,替换成自己本地的 mysql 数据库地址和用户名、密码
    dataSource.setUrl("jdbc:mysql://localhost:3306/test");
    dataSource.setUsername("root");
    dataSource.setPassword("root123456");
    //设置连接池的一些参数
    dataSource.setInitialSize(10);
    dataSource.setMaxTotal(50);
    dataSource.setMinIdle(2);

    Connection con = null;
    try {
        con = dataSource.getConnection();
        log.info("创建连接池:{}", con);
    } catch (Exception e) {
        log.error("-----------mysql get connection has exception , msg = {}", e.getMessage());
    }
    return con;
}
 
Example 9
Source File: DBCPDataSourceServiceImporter.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
protected BasicDataSource setupDataSourcePool( DataSourceConfiguration config )
        throws Exception
{
    BasicDataSource pool = new BasicDataSource();

    Class.forName( config.driver().get() );
    pool.setDriverClassName( config.driver().get() );
    pool.setUrl( config.url().get() );

    if ( !config.username().get().equals( "" ) ) {
        pool.setUsername( config.username().get() );
        pool.setPassword( config.password().get() );
    }

    if ( config.minPoolSize().get() != null ) {
        pool.setMinIdle( config.minPoolSize().get() );
    }
    if ( config.maxPoolSize().get() != null ) {
        pool.setMaxTotal( config.maxPoolSize().get() );
    }
    if ( config.loginTimeoutSeconds().get() != null ) {
        pool.setLoginTimeout( config.loginTimeoutSeconds().get() );
    }
    if ( config.maxConnectionAgeSeconds().get() != null ) {
        pool.setMinEvictableIdleTimeMillis( config.maxConnectionAgeSeconds().get() * 1000 );
    }
    if ( config.validationQuery().get() != null ) {
        pool.setValidationQuery( config.validationQuery().get() );
    }

    return pool;
}
 
Example 10
Source File: DataSourceFactory.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
public DataSource build() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl(resetBaseDir(options.url));
    dataSource.setUsername(options.username);
    dataSource.setPassword(options.password);
    dataSource.setMaxTotal(options.pool.max);
    dataSource.setMinIdle(options.pool.min);
    return dataSource;
}
 
Example 11
Source File: FhirServerConfigCommon.java    From hapi-fhir-jpaserver-starter with Apache License 2.0 5 votes vote down vote up
/**
 * The following bean configures the database connection. The 'url' property value of "jdbc:derby:directory:jpaserver_derby_files;create=true" indicates that the server should save resources in a
 * directory called "jpaserver_derby_files".
 * <p>
 * A URL to a remote database could also be placed here, along with login credentials and other properties supported by BasicDataSource.
 */
@Bean(destroyMethod = "close")
public BasicDataSource dataSource() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
  BasicDataSource retVal = new BasicDataSource();
  Driver driver = (Driver) Class.forName(HapiProperties.getDataSourceDriver()).getConstructor().newInstance();
  retVal.setDriver(driver);
  retVal.setUrl(HapiProperties.getDataSourceUrl());
  retVal.setUsername(HapiProperties.getDataSourceUsername());
  retVal.setPassword(HapiProperties.getDataSourcePassword());
  retVal.setMaxTotal(HapiProperties.getDataSourceMaxPoolSize());
  return retVal;
}
 
Example 12
Source File: CompatCreator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static DataSource getDataSource(ConnectionPoolDataSource cpds, String driver, String url, String username, String password) throws SQLException {

    Preconditions.checkNotNull(driver);

    if (cpds != null) {
      // Configuration supplied a driver-specific CPDS.
      return newSharedDataSource(cpds);
    }

    // Use the ConnectionPoolDataSource supplied by Apache DBCP.
    final BasicDataSource source = new BasicDataSource();
    // unbounded number of connections since we could have hundreds of queries running many threads simultaneously.
    source.setMaxTotal(Integer.MAX_VALUE);
    source.setTestOnBorrow(true);
    source.setValidationQueryTimeout(1);
    source.setDriverClassName(driver);
    source.setUrl(url);

    if (username != null) {
      source.setUsername(username);
    }

    if (password != null) {
      source.setPassword(password);
    }
    return source;
  }
 
Example 13
Source File: PgConnectionHelper.java    From pg-index-health with Apache License 2.0 5 votes vote down vote up
private static void setCommonProperties(@Nonnull final BasicDataSource dataSource,
                                        @Nonnull final String userName,
                                        @Nonnull final String password) {
    dataSource.setDriverClassName("org.postgresql.Driver");
    dataSource.setUsername(userName);
    dataSource.setPassword(password);
    dataSource.setValidationQuery("select 1");
    dataSource.setMaxTotal(1);
    dataSource.setMaxIdle(1);
    dataSource.setMaxOpenPreparedStatements(1);
}
 
Example 14
Source File: AbstractYamlDataSourceTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
protected static DataSource createDataSource(final String dsName) {
    BasicDataSource result = new BasicDataSource();
    result.setDriverClassName(org.h2.Driver.class.getName());
    result.setUrl(String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL", dsName));
    result.setUsername("sa");
    result.setMaxTotal(100);
    return result;
}
 
Example 15
Source File: DataSourceInfo.java    From yawp with MIT License 5 votes vote down vote up
public DataSource buildDatasource() {
    BasicDataSource ds = new BasicDataSource();

    ds.setDriverClassName(driverClassName);
    ds.setUrl(url);
    ds.setMaxTotal(50);

    return ds;
}
 
Example 16
Source File: DataSourceUtil.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private static DataSource createDBCP(final DatabaseType databaseType, final String dataSourceName) {
    BasicDataSource result = new BasicDataSource();
    DatabaseEnvironment databaseEnvironment = IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().get(databaseType);
    result.setDriverClassName(databaseEnvironment.getDriverClassName());
    result.setUrl(null == dataSourceName ? databaseEnvironment.getURL() : databaseEnvironment.getURL(dataSourceName));
    result.setUsername(databaseEnvironment.getUsername());
    result.setPassword(databaseEnvironment.getPassword());
    result.setMaxTotal(2);
    result.setValidationQuery("SELECT 1");
    if ("Oracle".equals(databaseType.getName())) {
        result.setConnectionInitSqls(Collections.singleton("ALTER SESSION SET CURRENT_SCHEMA = " + dataSourceName));
    }
    return result;
}
 
Example 17
Source File: TestDataSource.java    From cuba with Apache License 2.0 5 votes vote down vote up
public TestDataSource(String connUrl, String user, String password) {
    dataSource = new BasicDataSource();
    dataSource.setUrl(connUrl);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setMaxTotal(20);
}
 
Example 18
Source File: Dbcp2DataSourcePool.java    From Zebra with Apache License 2.0 4 votes vote down vote up
@Override
public DataSource build(DataSourceConfig config, boolean withDefaultValue) {
	BasicDataSource dbcp2DataSource = new BasicDataSource();

	dbcp2DataSource.setUrl(config.getJdbcUrl());
	dbcp2DataSource.setUsername(config.getUsername());
	dbcp2DataSource.setPassword(config.getPassword());
	dbcp2DataSource.setDriverClassName(StringUtils.isNotBlank(config.getDriverClass()) ? config.getDriverClass()
	      : JdbcDriverClassHelper.getDriverClassNameByJdbcUrl(config.getJdbcUrl()));

	if (withDefaultValue) {
		dbcp2DataSource.setInitialSize(getIntProperty(config, "initialPoolSize", 5));
		dbcp2DataSource.setMaxTotal(getIntProperty(config, "maxPoolSize", 30));
		dbcp2DataSource.setMinIdle(getIntProperty(config, "minPoolSize", 5));
		dbcp2DataSource.setMaxIdle(getIntProperty(config, "maxPoolSize", 20));
		dbcp2DataSource.setMaxWaitMillis(getIntProperty(config, "checkoutTimeout", 1000));
		dbcp2DataSource.setValidationQuery(getStringProperty(config, "preferredTestQuery", "SELECT 1"));
		dbcp2DataSource.setMinEvictableIdleTimeMillis(getIntProperty(config, "minEvictableIdleTimeMillis", 1800000));// 30min
		dbcp2DataSource
		      .setTimeBetweenEvictionRunsMillis(getIntProperty(config, "timeBetweenEvictionRunsMillis", 30000)); // 30s
		dbcp2DataSource.setRemoveAbandonedTimeout(getIntProperty(config, "removeAbandonedTimeout", 300)); // 30s
		dbcp2DataSource.setNumTestsPerEvictionRun(getIntProperty(config, "numTestsPerEvictionRun", 6)); // 30s
		dbcp2DataSource.setValidationQueryTimeout(getIntProperty(config, "validationQueryTimeout", 0));
		if (StringUtils.isNotBlank(getStringProperty(config, "connectionInitSql", null))) {
			List<String> initSqls = new ArrayList<String>();
			initSqls.add(getStringProperty(config, "connectionInitSql", null));
			dbcp2DataSource.setConnectionInitSqls(initSqls);
		}

		dbcp2DataSource.setTestWhileIdle(true);
		dbcp2DataSource.setTestOnBorrow(false);
		dbcp2DataSource.setTestOnReturn(false);
		dbcp2DataSource.setRemoveAbandonedOnBorrow(true);
		dbcp2DataSource.setRemoveAbandonedOnMaintenance(true);
	} else {
		try {
			PropertiesInit<BasicDataSource> propertiesInit = new PropertiesInit<BasicDataSource>(dbcp2DataSource);
			propertiesInit.initPoolProperties(config);
		} catch (Exception e) {
			throw new ZebraConfigException(String.format("dbcp2 dataSource [%s] created error : ", config.getId()), e);
		}
	}

	this.pool = dbcp2DataSource;
	LOGGER.info(String.format("New dataSource [%s] created.", config.getId()));

	return this.pool;
}
 
Example 19
Source File: HadoopDBCPConnectionPool.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Configures connection pool by creating an instance of the
 * {@link BasicDataSource} based on configuration provided with
 * {@link ConfigurationContext}.
 *
 * This operation makes no guarantees that the actual connection could be
 * made since the underlying system may still go off-line during normal
 * operation of the connection pool.
 *
 * @param context
 *            the configuration context
 * @throws InitializationException
 *             if unable to create a database connection
 */
@OnEnabled
public void onEnabled(final ConfigurationContext context) throws IOException {
    // Get Configuration instance from specified resources
    final String configFiles = context.getProperty(HADOOP_CONFIGURATION_RESOURCES).evaluateAttributeExpressions().getValue();
    final Configuration hadoopConfig = getConfigurationFromFiles(configFiles);

    // Add any dynamic properties to the HBase Configuration
    for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        final PropertyDescriptor descriptor = entry.getKey();
        if (descriptor.isDynamic()) {
            hadoopConfig.set(descriptor.getName(), context.getProperty(descriptor).evaluateAttributeExpressions().getValue());
        }
    }

    // If security is enabled then determine how to authenticate based on the various principal/keytab/password options
    if (SecurityUtil.isSecurityEnabled(hadoopConfig)) {
        final String explicitPrincipal = context.getProperty(kerberosProperties.getKerberosPrincipal()).evaluateAttributeExpressions().getValue();
        final String explicitKeytab = context.getProperty(kerberosProperties.getKerberosKeytab()).evaluateAttributeExpressions().getValue();
        final String explicitPassword = context.getProperty(kerberosProperties.getKerberosPassword()).getValue();
        final KerberosCredentialsService credentialsService = context.getProperty(KERBEROS_CREDENTIALS_SERVICE).asControllerService(KerberosCredentialsService.class);

        final String resolvedPrincipal;
        final String resolvedKeytab;
        if (credentialsService != null) {
            resolvedPrincipal = credentialsService.getPrincipal();
            resolvedKeytab = credentialsService.getKeytab();
        } else {
            resolvedPrincipal = explicitPrincipal;
            resolvedKeytab = explicitKeytab;
        }

        if (resolvedKeytab != null) {
            kerberosUser = new KerberosKeytabUser(resolvedPrincipal, resolvedKeytab);
            getLogger().info("Security Enabled, logging in as principal {} with keytab {}", new Object[] {resolvedPrincipal, resolvedKeytab});
        } else if (explicitPassword != null) {
            kerberosUser = new KerberosPasswordUser(resolvedPrincipal, explicitPassword);
            getLogger().info("Security Enabled, logging in as principal {} with password", new Object[] {resolvedPrincipal});
        } else {
            throw new IOException("Unable to authenticate with Kerberos, no keytab or password was provided");
        }

        ugi = SecurityUtil.getUgiForKerberosUser(hadoopConfig, kerberosUser);
        getLogger().info("Successfully logged in as principal " + resolvedPrincipal);
    } else {
        getLogger().info("Simple Authentication");
    }

    // Initialize the DataSource...
    final String dbUrl = context.getProperty(DATABASE_URL).evaluateAttributeExpressions().getValue();
    final String driverName = context.getProperty(DB_DRIVERNAME).evaluateAttributeExpressions().getValue();
    final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue();
    final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue();
    final Integer maxTotal = context.getProperty(MAX_TOTAL_CONNECTIONS).evaluateAttributeExpressions().asInteger();
    final String validationQuery = context.getProperty(VALIDATION_QUERY).evaluateAttributeExpressions().getValue();
    final Long maxWaitMillis = extractMillisWithInfinite(context.getProperty(MAX_WAIT_TIME).evaluateAttributeExpressions());
    final Integer minIdle = context.getProperty(MIN_IDLE).evaluateAttributeExpressions().asInteger();
    final Integer maxIdle = context.getProperty(MAX_IDLE).evaluateAttributeExpressions().asInteger();
    final Long maxConnLifetimeMillis = extractMillisWithInfinite(context.getProperty(MAX_CONN_LIFETIME).evaluateAttributeExpressions());
    final Long timeBetweenEvictionRunsMillis = extractMillisWithInfinite(context.getProperty(EVICTION_RUN_PERIOD).evaluateAttributeExpressions());
    final Long minEvictableIdleTimeMillis = extractMillisWithInfinite(context.getProperty(MIN_EVICTABLE_IDLE_TIME).evaluateAttributeExpressions());
    final Long softMinEvictableIdleTimeMillis = extractMillisWithInfinite(context.getProperty(SOFT_MIN_EVICTABLE_IDLE_TIME).evaluateAttributeExpressions());

    dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverName);
    dataSource.setDriverClassLoader(this.getClass().getClassLoader());
    dataSource.setUrl(dbUrl);
    dataSource.setUsername(user);
    dataSource.setPassword(passw);
    dataSource.setMaxWaitMillis(maxWaitMillis);
    dataSource.setMaxTotal(maxTotal);
    dataSource.setMinIdle(minIdle);
    dataSource.setMaxIdle(maxIdle);
    dataSource.setMaxConnLifetimeMillis(maxConnLifetimeMillis);
    dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    dataSource.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);

    if (StringUtils.isEmpty(validationQuery)) {
        dataSource.setValidationQuery(validationQuery);
        dataSource.setTestOnBorrow(true);
    }
}
 
Example 20
Source File: DBCPConnectionPool.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Configures connection pool by creating an instance of the
 * {@link BasicDataSource} based on configuration provided with
 * {@link ConfigurationContext}.
 *
 * This operation makes no guarantees that the actual connection could be
 * made since the underlying system may still go off-line during normal
 * operation of the connection pool.
 *
 * @param context
 *            the configuration context
 * @throws InitializationException
 *             if unable to create a database connection
 */
@OnEnabled
public void onConfigured(final ConfigurationContext context) throws InitializationException {

    final String drv = context.getProperty(DB_DRIVERNAME).evaluateAttributeExpressions().getValue();
    final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue();
    final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue();
    final Integer maxTotal = context.getProperty(MAX_TOTAL_CONNECTIONS).evaluateAttributeExpressions().asInteger();
    final String validationQuery = context.getProperty(VALIDATION_QUERY).evaluateAttributeExpressions().getValue();
    final Long maxWaitMillis = extractMillisWithInfinite(context.getProperty(MAX_WAIT_TIME).evaluateAttributeExpressions());
    final Integer minIdle = context.getProperty(MIN_IDLE).evaluateAttributeExpressions().asInteger();
    final Integer maxIdle = context.getProperty(MAX_IDLE).evaluateAttributeExpressions().asInteger();
    final Long maxConnLifetimeMillis = extractMillisWithInfinite(context.getProperty(MAX_CONN_LIFETIME).evaluateAttributeExpressions());
    final Long timeBetweenEvictionRunsMillis = extractMillisWithInfinite(context.getProperty(EVICTION_RUN_PERIOD).evaluateAttributeExpressions());
    final Long minEvictableIdleTimeMillis = extractMillisWithInfinite(context.getProperty(MIN_EVICTABLE_IDLE_TIME).evaluateAttributeExpressions());
    final Long softMinEvictableIdleTimeMillis = extractMillisWithInfinite(context.getProperty(SOFT_MIN_EVICTABLE_IDLE_TIME).evaluateAttributeExpressions());
    final KerberosCredentialsService kerberosCredentialsService = context.getProperty(KERBEROS_CREDENTIALS_SERVICE).asControllerService(KerberosCredentialsService.class);
    final String kerberosPrincipal = context.getProperty(KERBEROS_PRINCIPAL).evaluateAttributeExpressions().getValue();
    final String kerberosPassword = context.getProperty(KERBEROS_PASSWORD).getValue();

    if (kerberosCredentialsService != null) {
        kerberosUser = new KerberosKeytabUser(kerberosCredentialsService.getPrincipal(), kerberosCredentialsService.getKeytab());
    } else if (!StringUtils.isBlank(kerberosPrincipal) && !StringUtils.isBlank(kerberosPassword)) {
        kerberosUser = new KerberosPasswordUser(kerberosPrincipal, kerberosPassword);
    }

    if (kerberosUser != null) {
        try {
            kerberosUser.login();
        } catch (LoginException e) {
            throw new InitializationException("Unable to authenticate Kerberos principal", e);
        }
    }

    dataSource = new BasicDataSource();
    dataSource.setDriverClassName(drv);

    // Optional driver URL, when exist, this URL will be used to locate driver jar file location
    final String urlString = context.getProperty(DB_DRIVER_LOCATION).evaluateAttributeExpressions().getValue();
    dataSource.setDriverClassLoader(getDriverClassLoader(urlString, drv));

    final String dburl = context.getProperty(DATABASE_URL).evaluateAttributeExpressions().getValue();

    dataSource.setMaxWaitMillis(maxWaitMillis);
    dataSource.setMaxTotal(maxTotal);
    dataSource.setMinIdle(minIdle);
    dataSource.setMaxIdle(maxIdle);
    dataSource.setMaxConnLifetimeMillis(maxConnLifetimeMillis);
    dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    dataSource.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);

    if (validationQuery!=null && !validationQuery.isEmpty()) {
        dataSource.setValidationQuery(validationQuery);
        dataSource.setTestOnBorrow(true);
    }

    dataSource.setUrl(dburl);
    dataSource.setUsername(user);
    dataSource.setPassword(passw);

    context.getProperties().keySet().stream().filter(PropertyDescriptor::isDynamic)
            .forEach((dynamicPropDescriptor) -> dataSource.addConnectionProperty(dynamicPropDescriptor.getName(),
                    context.getProperty(dynamicPropDescriptor).evaluateAttributeExpressions().getValue()));

}