Java Code Examples for com.zaxxer.hikari.HikariDataSource#setMinimumIdle()

The following examples show how to use com.zaxxer.hikari.HikariDataSource#setMinimumIdle() . 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: LocDataSourceAutoConfiguration.java    From loc-framework with MIT License 6 votes vote down vote up
private HikariDataSource createHikariDataSource(JdbcProperties jdbcProperties) {
  HikariDataSource hikariDataSource = new HikariDataSource();
  hikariDataSource.setJdbcUrl(jdbcProperties.getJdbcUrl());
  hikariDataSource.setUsername(jdbcProperties.getUsername());
  hikariDataSource.setPassword(jdbcProperties.getPassword());

  JdbcPoolProperties jdbcPoolProperties = jdbcProperties.getJdbcPool();
  hikariDataSource.setAutoCommit(jdbcPoolProperties.isAutoCommit());
  hikariDataSource.setConnectionTimeout(jdbcPoolProperties.getConnectionTimeout());
  hikariDataSource.setIdleTimeout(jdbcPoolProperties.getIdleTimeout());
  hikariDataSource.setMaxLifetime(jdbcPoolProperties.getMaxLifetime());
  hikariDataSource.setMaximumPoolSize(jdbcPoolProperties.getMaximumPoolSize());
  hikariDataSource.setMinimumIdle(jdbcPoolProperties.getMinimumIdle());
  hikariDataSource
      .setInitializationFailTimeout(jdbcPoolProperties.getInitializationFailTimeout());
  hikariDataSource.setIsolateInternalQueries(jdbcPoolProperties.isIsolateInternalQueries());
  hikariDataSource.setReadOnly(jdbcPoolProperties.isReadOnly());
  hikariDataSource.setRegisterMbeans(jdbcPoolProperties.isRegisterMbeans());
  Optional.ofNullable(jdbcPoolProperties.getDriverClassName())
      .ifPresent(hikariDataSource::setDriverClassName);
  hikariDataSource.setValidationTimeout(jdbcPoolProperties.getValidationTimeout());
  hikariDataSource.setLeakDetectionThreshold(jdbcPoolProperties.getLeakDetectionThreshold());
  return hikariDataSource;
}
 
Example 2
Source File: BaseQueryTool.java    From datax-web with MIT License 5 votes vote down vote up
private void getDataSource(JobDatasource jobDatasource) throws SQLException {
    String userName = AESUtil.decrypt(jobDatasource.getJdbcUsername());

    //这里默认使用 hikari 数据源
    HikariDataSource dataSource = new HikariDataSource();
    dataSource.setUsername(userName);
    dataSource.setPassword(AESUtil.decrypt(jobDatasource.getJdbcPassword()));
    dataSource.setJdbcUrl(jobDatasource.getJdbcUrl());
    dataSource.setDriverClassName(jobDatasource.getJdbcDriverClass());
    dataSource.setMaximumPoolSize(1);
    dataSource.setMinimumIdle(0);
    dataSource.setConnectionTimeout(30000);
    this.datasource = dataSource;
    this.connection = this.datasource.getConnection();
}
 
Example 3
Source File: ACSShell.java    From freeacs with MIT License 5 votes vote down vote up
public static DataSource getHikariDataSource(String prefix) {
  HikariDataSource ds = new HikariDataSource();
  String jdbcUrl = prefix + ".datasource.jdbcUrl";
  ds.setJdbcUrl(
      Optional.ofNullable(System.getProperty(jdbcUrl))
          .orElseGet(() -> Properties.pr.getProperty(jdbcUrl)));
  String className = prefix + ".datasource.driverClassName";
  ds.setDriverClassName(
      Optional.ofNullable(System.getProperty(className))
          .orElseGet(() -> Properties.pr.getProperty(className)));
  String user = prefix + ".datasource.username";
  ds.setUsername(
      Optional.ofNullable(System.getProperty(user))
          .orElseGet(() -> Properties.pr.getProperty(user)));
  String password = prefix + ".datasource.password";
  ds.setPassword(
      Optional.ofNullable(System.getProperty(password))
          .orElseGet(() -> Properties.pr.getProperty(password)));
  String poolSize = prefix + ".datasource.maximum-pool-size";
  ds.setMaximumPoolSize(
      Integer.parseInt(
          Optional.ofNullable(System.getProperty(poolSize))
              .orElseGet(() -> Properties.pr.getProperty(poolSize))));
  String minimumIdle = prefix + ".datasource.minimum-idle";
  ds.setMinimumIdle(
      Integer.parseInt(
          Optional.ofNullable(System.getProperty(minimumIdle))
              .orElseGet(() -> Properties.pr.getProperty(minimumIdle))));
  String poolName = prefix + ".datasource.poolName";
  ds.setPoolName(
      Optional.ofNullable(System.getProperty(poolName))
          .orElseGet(() -> Properties.pr.getProperty(poolName)));
  return ds;
}
 
Example 4
Source File: PostgreSQLSourceConfig.java    From Launcher with GNU General Public License v3.0 5 votes vote down vote up
public synchronized Connection getConnection() throws SQLException {
    if (source == null) { // New data source
        PGSimpleDataSource postgresqlSource = new PGSimpleDataSource();

        // Set credentials
        postgresqlSource.setServerNames(addresses);
        postgresqlSource.setPortNumbers(ports);
        postgresqlSource.setUser(username);
        postgresqlSource.setPassword(password);
        postgresqlSource.setDatabaseName(database);

        // Try using HikariCP
        source = postgresqlSource;

        //noinspection Duplicates
        try {
            Class.forName("com.zaxxer.hikari.HikariDataSource");
            hikari = true; // Used for shutdown. Not instanceof because of possible classpath error

            // Set HikariCP pool
            HikariDataSource hikariSource = new HikariDataSource();
            hikariSource.setDataSource(source);

            // Set pool settings
            hikariSource.setPoolName(poolName);
            hikariSource.setMinimumIdle(0);
            hikariSource.setMaximumPoolSize(MAX_POOL_SIZE);
            hikariSource.setIdleTimeout(TIMEOUT * 1000L);

            // Replace source with hds
            source = hikariSource;
            LogHelper.info("HikariCP pooling enabled for '%s'", poolName);
        } catch (ClassNotFoundException ignored) {
            LogHelper.warning("HikariCP isn't in classpath for '%s'", poolName);
        }
    }
    return source.getConnection();
}
 
Example 5
Source File: HikariPool.java    From Java-11-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
private static DataSource createDataSource1() {
    HikariDataSource ds = new HikariDataSource();
    ds.setPoolName("cookpool");
    ds.setDriverClassName("org.postgresql.Driver");
    ds.setJdbcUrl("jdbc:postgresql://localhost/cookbook");
    ds.setUsername( "cook");
    //ds.setPassword("123Secret");
    ds.setMaximumPoolSize(10);
    ds.setMinimumIdle(2);
    ds.addDataSourceProperty("cachePrepStmts", Boolean.TRUE);
    ds.addDataSourceProperty("prepStmtCacheSize", 256);
    ds.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
    ds.addDataSourceProperty("useServerPrepStmts", Boolean.TRUE);
    return ds;
}
 
Example 6
Source File: BatchProcessing.java    From Java-11-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
private static DataSource createDataSource() {
    HikariDataSource ds = new HikariDataSource();
    ds.setPoolName("cookpool");
    ds.setDriverClassName("org.postgresql.Driver");
    ds.setJdbcUrl("jdbc:postgresql://localhost/cookbook");
    ds.setUsername( "cook");
    //ds.setPassword("123Secret");
    ds.setMaximumPoolSize(2);
    ds.setMinimumIdle(2);
    ds.addDataSourceProperty("reWriteBatchedInserts", Boolean.TRUE);
    return ds;
}
 
Example 7
Source File: DataSourceConfig.java    From spring-cloud-example with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource primaryDataSource() {
    HikariDataSource ds = new HikariDataSource();
    ds.setMaximumPoolSize(100);
    ds.setMinimumIdle(30);
    ds.setDriverClassName("org.h2.Driver");
    ds.setJdbcUrl("jdbc:h2:mem:test");
    ds.setUsername("root");
    ds.setPassword("root");
    return ds;
}
 
Example 8
Source File: JdbcCoordinatorRepository.java    From hmily with Apache License 2.0 5 votes vote down vote up
@Override
public void init(final String modelName, final HmilyConfig txConfig) {
    try {
        final HmilyDbConfig hmilyDbConfig = txConfig.getHmilyDbConfig();
        if (hmilyDbConfig.getDataSource() != null && StringUtils.isBlank(hmilyDbConfig.getUrl())) {
            dataSource = hmilyDbConfig.getDataSource();
        } else {
            HikariDataSource hikariDataSource = new HikariDataSource();
            hikariDataSource.setJdbcUrl(hmilyDbConfig.getUrl());
            hikariDataSource.setDriverClassName(hmilyDbConfig.getDriverClassName());
            hikariDataSource.setUsername(hmilyDbConfig.getUsername());
            hikariDataSource.setPassword(hmilyDbConfig.getPassword());
            hikariDataSource.setMaximumPoolSize(hmilyDbConfig.getMaxActive());
            hikariDataSource.setMinimumIdle(hmilyDbConfig.getMinIdle());
            hikariDataSource.setConnectionTimeout(hmilyDbConfig.getConnectionTimeout());
            hikariDataSource.setIdleTimeout(hmilyDbConfig.getIdleTimeout());
            hikariDataSource.setMaxLifetime(hmilyDbConfig.getMaxLifetime());
            hikariDataSource.setConnectionTestQuery(hmilyDbConfig.getConnectionTestQuery());
            if (hmilyDbConfig.getDataSourcePropertyMap() != null && !hmilyDbConfig.getDataSourcePropertyMap().isEmpty()) {
                hmilyDbConfig.getDataSourcePropertyMap().forEach(hikariDataSource::addDataSourceProperty);
            }
            dataSource = hikariDataSource;
        }
        this.tableName = RepositoryPathUtils.buildDbTableName(modelName);
        //save current database type
        this.currentDBType = DbTypeUtils.buildByDriverClassName(hmilyDbConfig.getDriverClassName());
        executeUpdate(SqlHelper.buildCreateTableSql(hmilyDbConfig.getDriverClassName(), tableName));
    } catch (Exception e) {
        LogUtil.error(LOGGER, "hmily jdbc log init exception please check config:{}", e::getMessage);
        throw new HmilyRuntimeException(e);
    }
}
 
Example 9
Source File: HmilyAdminConfiguration.java    From hmily with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    HikariDataSource hikariDataSource = new HikariDataSource();
    hikariDataSource.setDriverClassName(env.getProperty("compensation.db.driver"));
    hikariDataSource.setJdbcUrl(env.getProperty("compensation.db.url"));
    //用户名
    hikariDataSource.setUsername(env.getProperty("compensation.db.username"));
    //密码
    hikariDataSource.setPassword(env.getProperty("compensation.db.password"));
    hikariDataSource.setMinimumIdle(5);
    hikariDataSource.setMaximumPoolSize(10);
    return hikariDataSource;
}
 
Example 10
Source File: AbstractConfiguration.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Override
public DataSource createDataSource() {
    final HikariDataSource ds = new HikariDataSource();
    ds.setDriverClassName("org.neo4j.jdbc.Driver");
    ds.setUsername(username);
    ds.setPassword(password);
    ds.setJdbcUrl(connectionUrl);
    ds.setMinimumIdle(1);
    ds.setMinimumIdle(2);

    return ds;
}
 
Example 11
Source File: DataSourceUtils.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private static HikariDataSource createHikariDataSource(final DatabaseType databaseType, final String databaseName) {
    HikariDataSource result = new HikariDataSource();
    result.setJdbcUrl(getURL(databaseType, databaseName));
    result.setUsername("root");
    result.setPassword("root");
    result.setMaximumPoolSize(10);
    result.setMinimumIdle(2);
    result.setConnectionTimeout(15 * 1000);
    result.setIdleTimeout(40 * 1000);
    return result;
}
 
Example 12
Source File: HikariCPDataSourceBuilder.java    From micro-server with Apache License 2.0 5 votes vote down vote up
private DataSource getDataSource() {
	HikariDataSource ds = new HikariDataSource();

	ds.setDriverClassName(mainEnv.getDriverClassName());
	ds.setJdbcUrl(mainEnv.getUrl());
	ds.setUsername(mainEnv.getUsername());
	ds.setPassword(mainEnv.getPassword());
	ds.setMaximumPoolSize(hikariCPEnv.getMaxPoolSize());
	ds.setMinimumIdle(hikariCPEnv.getMinimumIdle());
	ds.setIdleTimeout(hikariCPEnv.getIdleTimeout());

	return ds;
}
 
Example 13
Source File: HikariPool.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void init(String url, String user, String pass, boolean enable) {
	HikariConfig c = new HikariConfig(props);
				 c.setJdbcUrl(url);
				 c.setUsername(user);
				 c.setPassword(pass);
				 c.setThreadFactory(ThreadManager.getInstance().getFactory());
	datasource = new HikariDataSource(c);
	
	  if(!enable) {
		  datasource.setMinimumIdle(1);
          datasource.setMaximumPoolSize(1);
	  }
	
}
 
Example 14
Source File: MySQLDbService.java    From ecs-sync with Apache License 2.0 5 votes vote down vote up
@Override
protected JdbcTemplate createJdbcTemplate() {
    HikariDataSource ds = new HikariDataSource();
    ds.setJdbcUrl(connectString);
    if (username != null) ds.setUsername(username);
    if (password != null) ds.setPassword(password);
    ds.setMaximumPoolSize(16);
    ds.setMinimumIdle(2);
    ds.addDataSourceProperty("characterEncoding", "utf8");
    ds.addDataSourceProperty("cachePrepStmts", "true");
    ds.addDataSourceProperty("prepStmtCacheSize", "256");
    ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
    ds.addDataSourceProperty("defaultFetchSize", "" + Integer.MIN_VALUE);
    return new JdbcTemplate(ds);
}
 
Example 15
Source File: DataSourceConfiguration.java    From alf.io with GNU General Public License v3.0 5 votes vote down vote up
@Bean
@Profile({"!"+Initializer.PROFILE_INTEGRATION_TEST, "travis"})
public DataSource getDataSource(Environment env, PlatformProvider platform) {
    if(platform == PlatformProvider.CLOUD_FOUNDRY) {
        return new FakeCFDataSource();
    } else {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(platform.getUrl(env));
        dataSource.setUsername(platform.getUsername(env));
        dataSource.setPassword(platform.getPassword(env));
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setMaximumPoolSize(platform.getMaxActive(env));
        dataSource.setMinimumIdle(platform.getMinIdle(env));
        dataSource.setConnectionTimeout(1000L);

        log.debug("Connection pool properties: max active {}, initial size {}", dataSource.getMaximumPoolSize(), dataSource.getMinimumIdle());

        // check
        boolean isSuperAdmin = Boolean.TRUE.equals(new NamedParameterJdbcTemplate(dataSource)
            .queryForObject("select usesuper from pg_user where usename = CURRENT_USER",
                new EmptySqlParameterSource(),
                Boolean.class));

        if (isSuperAdmin) {
            log.warn("You're accessing the database using a superuser. This is highly discouraged since it will disable the row security policy checks.");
        }

        //
        return dataSource;
    }
}
 
Example 16
Source File: ConnectionProviderPooled.java    From rxjava-jdbc with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a new pooled data source based on jdbc url.
 * 
 * @param url
 *            jdbc url
 * @param username
 *            login username
 * @param password
 *            login password
 * @param minPoolSize
 *            minimum database connection pool size
 * @param maxPoolSize
 *            maximum database connection pool size
 * @param connectionTimeoutMs
 * @return
 */
private static HikariDataSource createPool(String url, String username, String password,
        int minPoolSize, int maxPoolSize, long connectionTimeoutMs) {

    HikariDataSource ds = new HikariDataSource();
    ds.setJdbcUrl(url);
    ds.setUsername(username);
    ds.setPassword(password);
    ds.setMinimumIdle(minPoolSize);
    ds.setMaximumPoolSize(maxPoolSize);
    ds.setConnectionTimeout(connectionTimeoutMs);
    return ds;
}