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

The following examples show how to use org.apache.commons.dbcp2.BasicDataSource#setMinIdle() . 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: 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 2
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 3
Source File: DBCP2DataSourcePool.java    From EasyReport with Apache License 2.0 6 votes vote down vote up
@Override
public DataSource wrap(final ReportDataSource rptDs) {
    try {
        final BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(rptDs.getDriverClass());
        dataSource.setUrl(rptDs.getJdbcUrl());
        dataSource.setUsername(rptDs.getUser());
        dataSource.setPassword(rptDs.getPassword());
        dataSource.setInitialSize(MapUtils.getInteger(rptDs.getOptions(), "initialSize", 3));
        dataSource.setMaxIdle(MapUtils.getInteger(rptDs.getOptions(), "maxIdle", 20));
        dataSource.setMinIdle(MapUtils.getInteger(rptDs.getOptions(), "minIdle", 1));
        dataSource.setLogAbandoned(MapUtils.getBoolean(rptDs.getOptions(), "logAbandoned", true));
        dataSource.setRemoveAbandonedTimeout(
            MapUtils.getInteger(rptDs.getOptions(), "removeAbandonedTimeout", 180));
        dataSource.setMaxWaitMillis(MapUtils.getInteger(rptDs.getOptions(), "maxWait", 1000));
        return dataSource;
    } catch (final Exception ex) {
        throw new RuntimeException("C3p0DataSourcePool Create Error", ex);
    }
}
 
Example 4
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 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: 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 7
Source File: MyDataSourceFactory.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
/**
 * apache commons dbcp2 数据源
 *
 * @return
 */
protected static DataSource getDBCP2DataSource() {
    //创建BasicDataSource类对象
    BasicDataSource datasource = new BasicDataSource();
    //数据库连接信息(必须)
    datasource.setDriverClassName("com.mysql.jdbc.Driver");
    datasource.setUrl("jdbc:mysql://localhost:3306/ztree?useUnicode=true&characterEncoding=utf-8&useSSL=false");
    datasource.setUsername("root");
    datasource.setPassword("123456");
    //连接池中的连接数量配置(可选)
    datasource.setInitialSize(10);//初始化的连接数
    datasource.setMaxOpenPreparedStatements(8);//最大连接数
    datasource.setMaxIdle(5);//最大空闲连接
    datasource.setMinIdle(1);//最小空闲连接
    return datasource;
}
 
Example 8
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 9
Source File: SqlDbConfiguration.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
public DataSource createDataSource() {
    final String driverClass = (String) dbConfig.get("javax.persistence.jdbc.driver");
    final String connectionUrl = (String) dbConfig.get("javax.persistence.jdbc.url");
    final String username = (String) dbConfig.get("javax.persistence.jdbc.user");
    final String password = (String) dbConfig.get("javax.persistence.jdbc.password");

    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(driverClass);
    ds.setUsername(username);
    ds.setPassword(password);
    ds.setUrl(connectionUrl);
    ds.setMinIdle(1);
    ds.setMaxIdle(2);

    return ds;
}
 
Example 10
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 11
Source File: MyDataSourceFactory.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
protected static DataSource getDBCP2DataSource2() {
    //创建BasicDataSource类对象
    BasicDataSource datasource = new BasicDataSource();
    //数据库连接信息(必须)
    datasource.setDriverClassName("com.mysql.jdbc.Driver");
    datasource.setUrl("jdbc:mysql://129.9.100.16:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false");
    datasource.setUsername("test");
    datasource.setPassword("123456");
    //连接池中的连接数量配置(可选)
    datasource.setInitialSize(10);//初始化的连接数
    datasource.setMaxOpenPreparedStatements(8);//最大连接数
    datasource.setMaxIdle(5);//最大空闲连接
    datasource.setMinIdle(1);//最小空闲连接
    return datasource;
}
 
Example 12
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 13
Source File: CommonUtil.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
/**
 * check the database url
 *
 * @param databaseUrl data bae url
 * @return connection
 */
public static Connection getDbcpConnection(String databaseUrl, String databaseType) {
    try {
        Map<String, String> requestUrlMap = uRLRequest(databaseUrl);
        // check all parameter
        if (!requestUrlMap.containsKey("user") || !requestUrlMap.containsKey("password") || StringUtils.isEmpty(urlPage(databaseUrl))) {
            return null;
        }
        // use the cache
        if (dsMap.containsKey(databaseUrl)) {
            // use the old connection
            return dsMap.get(databaseUrl).getConnection();
        } else {
            Properties properties = new Properties();
            BasicDataSource ds = BasicDataSourceFactory.createDataSource(properties);
            dsMap.put(databaseUrl, ds);
            if (DatabaseTypeEnum.H2_DATABASE.getCode().equals(databaseType)) {
                ds.setDriverClassName("org.h2.Driver");
            } else {
                ds.setDriverClassName("org.mariadb.jdbc.Driver");
            }
            ds.setUrl(urlPage(databaseUrl));
            ds.setUsername(requestUrlMap.get("user"));
            ds.setPassword(requestUrlMap.get("password"));

            ds.setInitialSize(Integer.parseInt(Objects.requireNonNull(ProcessorApplication.environment.getProperty("spring.datasource.dbcp2.initial-size"))));
            ds.setMinIdle(Integer.parseInt(Objects.requireNonNull(ProcessorApplication.environment.getProperty("spring.datasource.dbcp2.min-idle"))));
            ds.setMaxWaitMillis(Integer.parseInt(Objects.requireNonNull(ProcessorApplication.environment.getProperty("spring.datasource.dbcp2.max-wait-millis"))));
            ds.setMaxTotal(Integer.parseInt(Objects.requireNonNull(ProcessorApplication.environment.getProperty("spring.datasource.dbcp2.max-total"))));

            return ds.getConnection();
        }
    } catch (Exception e) {
        log.error("e:{}", e.toString());
        return null;
    }
}
 
Example 14
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 15
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 16
Source File: DataSourceContainer.java    From DBus with Apache License 2.0 4 votes vote down vote up
public boolean register(JdbcVo conf) {
    boolean isOk = true;
    try {
        BasicDataSource bds = new BasicDataSource();
        bds.setDriverClassName(conf.getDriverClass());
        bds.setUrl(conf.getUrl());
        bds.setUsername(conf.getUserName());
        bds.setPassword(conf.getPassword());
        bds.setInitialSize(conf.getInitialSize());
        bds.setMaxTotal(conf.getMaxActive());
        bds.setMaxIdle(conf.getMaxIdle());
        bds.setMinIdle(conf.getMinIdle());
        // 设置等待获取连接的最长时间
        // bds.setMaxWaitMillis(20 * 1000);
        // validQuery 只给test idle 使用,不给 test Borrow使用, 为什么?
        // 发现 oracle版本 insert心跳被block, 下面的link 有人说 可以通过设置TestOnBorrow=false 解决。
        // https://stackoverflow.com/questions/4853732/blocking-on-dbcp-connection-pool-open-and-close-connnection-is-database-conne
        bds.setTestOnBorrow(true);
        bds.setTestWhileIdle(false);

        if (StringUtils.equalsIgnoreCase(Constants.CONFIG_DB_TYPE_ORA, conf.getType())) {
            bds.setValidationQuery("select 1 from dual");
            bds.setValidationQueryTimeout(5);
        } else if (StringUtils.equalsIgnoreCase(Constants.CONFIG_DB_TYPE_MYSQL, conf.getType())) {
            bds.setValidationQuery("select 1");
            bds.setValidationQueryTimeout(5);
        }

        /*bds.setTimeBetweenEvictionRunsMillis(1000 * 300);
        if (org.apache.commons.lang.StringUtils.equals(Constants.CONFIG_DB_TYPE_ORA, conf.getType())) {
            bds.setValidationQuery("select 1 from dual");
            bds.setValidationQueryTimeout(1);
        } else if (org.apache.commons.lang.StringUtils.equals(Constants.CONFIG_DB_TYPE_MYSQL, conf.getType())) {
            bds.setValidationQuery("select 1");
            bds.setValidationQueryTimeout(1);
        }*/
        LoggerFactory.getLogger().info("create datasource key:" + conf.getKey() + " url:" + conf.getUrl());
        cmap.put(conf.getKey(), bds);

        // 为了支持查询主库和被库的延时,一个ds需要同时建立同主库和被库的连接
        if (conf instanceof DsVo) {
            DsVo ds = (DsVo) conf;
            if (StringUtils.isNotBlank(ds.getSlvaeUrl())) {
                BasicDataSource slaveBds = new BasicDataSource();
                slaveBds.setDriverClassName(ds.getDriverClass());
                slaveBds.setUrl(ds.getSlvaeUrl());
                slaveBds.setUsername(ds.getUserName());
                slaveBds.setPassword(ds.getPassword());
                slaveBds.setInitialSize(ds.getInitialSize());
                slaveBds.setMaxTotal(ds.getMaxActive());
                slaveBds.setMaxIdle(ds.getMaxIdle());
                slaveBds.setMinIdle(ds.getMinIdle());
                slaveBds.setTestOnBorrow(true);
                slaveBds.setTestWhileIdle(false);

                if (StringUtils.equalsIgnoreCase(Constants.CONFIG_DB_TYPE_ORA, conf.getType())) {
                    slaveBds.setValidationQuery("select 1 from dual");
                    slaveBds.setValidationQueryTimeout(5);
                } else if (StringUtils.equalsIgnoreCase(Constants.CONFIG_DB_TYPE_MYSQL, conf.getType())) {
                    slaveBds.setValidationQuery("select 1");
                    slaveBds.setValidationQueryTimeout(5);
                }

                // 设置等待获取连接的最长时间
                // slaveBds.setMaxWaitMillis(20 * 1000);
                String key = StringUtils.join(new String[]{ds.getKey(), "slave"}, "_");
                LoggerFactory.getLogger().info("create datasource key:" + key + " url:" + ds.getSlvaeUrl());
                cmap.put(key, slaveBds);
            } else {
                LoggerFactory.getLogger().warn("db container initDsPool key " + ds.getKey() + " of slave url is empty.");
            }
        }

    } catch (Exception e) {
        LoggerFactory.getLogger().error("[db container initDsPool key " + conf.getKey() + " datasource error!]", e);
        isOk = false;
    }
    return isOk;
}
 
Example 17
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 18
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()));

}