Java Code Examples for com.zaxxer.hikari.HikariConfig#setDataSourceProperties()

The following examples show how to use com.zaxxer.hikari.HikariConfig#setDataSourceProperties() . 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: PipelineMavenPluginMySqlDaoIT.java    From pipeline-maven-plugin with MIT License 6 votes vote down vote up
@Override
public DataSource before_newDataSource() {
    HikariConfig config = new HikariConfig();
    String configurationFilePath = ".mysql_config";
    InputStream propertiesInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(configurationFilePath);

    Properties properties = new Properties();
    if (propertiesInputStream == null) {
        throw new IllegalArgumentException("Config file " + configurationFilePath + " not found in classpath");
    } else {
        try {
            properties.load(propertiesInputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    config.setJdbcUrl(Preconditions.checkNotNull(properties.getProperty("jdbc.url")));
    config.setUsername(Preconditions.checkNotNull(properties.getProperty("jdbc.username")));
    config.setPassword(Preconditions.checkNotNull(properties.getProperty("jdbc.password")));
    config.setDataSourceProperties(properties);
    return new HikariDataSource(config);
}
 
Example 2
Source File: DataSourceFactory.java    From extract with MIT License 6 votes vote down vote up
private DataSource createPooled(final String poolName) {
	final HikariConfig config = new HikariConfig();

	config.setJdbcUrl(createURL());
	config.setUsername(null == user ? "extract" : user);
	config.setPassword(password);

	config.setMaximumPoolSize(maximumPoolSize);

	if (null != poolName) {
		config.setPoolName(poolName);
	}

	config.setDataSourceProperties(createProperties());

	return new HikariDataSource(config);
}
 
Example 3
Source File: PostgresqlServerClientConnectionManager.java    From digdag with Apache License 2.0 6 votes vote down vote up
private HikariDataSource createDataSourceWithConnectionPool()
{
    DatabaseConfig databaseConfig = DatabaseConfig.convertFrom(systemConfig, "param_server");
    String url = DatabaseConfig.buildJdbcUrl(databaseConfig);

    HikariConfig hikari = new HikariConfig();
    hikari.setJdbcUrl(url);
    hikari.setDriverClassName(DatabaseMigrator.getDriverClassName(databaseConfig.getType()));
    hikari.setDataSourceProperties(DatabaseConfig.buildJdbcProperties(databaseConfig));

    hikari.setConnectionTimeout(databaseConfig.getConnectionTimeout() * 1000);
    hikari.setIdleTimeout(databaseConfig.getIdleTimeout() * 1000);
    hikari.setValidationTimeout(databaseConfig.getValidationTimeout() * 1000);
    hikari.setMaximumPoolSize(databaseConfig.getMaximumPoolSize());
    hikari.setMinimumIdle(databaseConfig.getMinimumPoolSize());

    // Here should not set connectionTestQuery (that overrides isValid) because
    // ThreadLocalTransactionManager.commit assumes that Connection.isValid returns
    // false when an error happened during a transaction.

    logger.debug("Using postgresql URL {}", hikari.getJdbcUrl());

    return new HikariDataSource(hikari);
}
 
Example 4
Source File: HikariCPPostgreSQLJPAConfiguration.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
public DataSource actualDataSource() {
    Properties dataSourceProperties = new Properties();
    dataSourceProperties.setProperty("user", jdbcUser);
    dataSourceProperties.setProperty("password", jdbcPassword);
    dataSourceProperties.setProperty("databaseName", jdbcDatabase);
    dataSourceProperties.setProperty("serverName", jdbcHost);
    dataSourceProperties.setProperty("portNumber", jdbcPort);

    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setDataSourceClassName(dataSourceClassName);
    hikariConfig.setDataSourceProperties(dataSourceProperties);
    hikariConfig.setMinimumPoolSize(1);
    hikariConfig.setMaximumPoolSize(3);

    return new HikariDataSource(hikariConfig);
}
 
Example 5
Source File: OrmDataSourceProperties.java    From sample-boot-micro with MIT License 5 votes vote down vote up
public DataSource dataSource() {
    HikariConfig config = new HikariConfig();
    config.setDriverClassName(driverClassName());
    config.setJdbcUrl(url);
    config.setUsername(username);
    config.setPassword(password);
    config.setMinimumIdle(minIdle);
    config.setMaximumPoolSize(maxPoolSize);
    if (validation) {
        config.setConnectionTestQuery(validationQuery());
    }
    config.setDataSourceProperties(props);
    return new HikariDataSource(config);
}
 
Example 6
Source File: ConnectionPoolingIT.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
private HikariDataSource initHikariDataSource() throws SQLException
{
  HikariConfig config = new HikariConfig();
  config.setDriverClassName(BaseJDBCTest.DRIVER_CLASS);
  config.setDataSourceProperties(setUpConnectionProperty());
  config.setJdbcUrl(connectStr);
  return new HikariDataSource(config);
}
 
Example 7
Source File: DBConnectionPoolFactory.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
public HikariDataSource generateConnectionPool(DBConnectionDetails dbConnectionDetails) {
    HikariConfig jdbcConfig = new HikariConfig();
    jdbcConfig.setUsername(dbConnectionDetails.getUsername());
    jdbcConfig.setPassword(dbConnectionDetails.password);
    jdbcConfig.setJdbcUrl(dbConnectionDetails.getJdbcUrl());
    jdbcConfig.setDataSourceProperties(getDataSourceProperties(dbConnectionDetails));
    return new HikariDataSource(jdbcConfig);
}
 
Example 8
Source File: DataSourceProvider.java    From digdag with Apache License 2.0 5 votes vote down vote up
private void createPooledDataSource()
{
    String url = DatabaseConfig.buildJdbcUrl(config);

    HikariConfig hikari = new HikariConfig();
    hikari.setJdbcUrl(url);
    hikari.setDriverClassName(DatabaseMigrator.getDriverClassName(config.getType()));
    hikari.setDataSourceProperties(DatabaseConfig.buildJdbcProperties(config));

    hikari.setConnectionTimeout(config.getConnectionTimeout() * 1000);
    hikari.setIdleTimeout(config.getIdleTimeout() * 1000);
    hikari.setValidationTimeout(config.getValidationTimeout() * 1000);
    hikari.setMaximumPoolSize(config.getMaximumPoolSize());
    hikari.setMinimumIdle(config.getMinimumPoolSize());
    hikari.setRegisterMbeans(config.getEnableJMX());
    hikari.setLeakDetectionThreshold(config.getLeakDetectionThreshold());

    // Here should not set connectionTestQuery (that overrides isValid) because
    // ThreadLocalTransactionManager.commit assumes that Connection.isValid returns
    // false when an error happened during a transaction.

    logger.debug("Using database URL {}", hikari.getJdbcUrl());

    HikariDataSource ds = new HikariDataSource(hikari);
    this.ds = ds;
    this.closer = ds;
}
 
Example 9
Source File: JdbcUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private HikariConfig createDataSourceConfig(
  HikariPoolConfigBean hikariConfigBean,
  boolean autoCommit,
  boolean readOnly
) throws StageException {
  HikariConfig config = new HikariConfig();

  config.setJdbcUrl(hikariConfigBean.getConnectionString());
  if (hikariConfigBean.useCredentials){
    config.setUsername(hikariConfigBean.getUsername().get());
    config.setPassword(hikariConfigBean.getPassword().get());
  }
  config.setAutoCommit(autoCommit);
  config.setReadOnly(readOnly);
  config.setMaximumPoolSize(hikariConfigBean.maximumPoolSize);
  config.setMinimumIdle(hikariConfigBean.minIdle);
  config.setConnectionTimeout(hikariConfigBean.connectionTimeout * MILLISECONDS);
  config.setIdleTimeout(hikariConfigBean.idleTimeout * MILLISECONDS);
  config.setMaxLifetime(hikariConfigBean.maxLifetime * MILLISECONDS);

  if (!StringUtils.isEmpty(hikariConfigBean.driverClassName)) {
    config.setDriverClassName(hikariConfigBean.driverClassName);
  }

  if (!StringUtils.isEmpty(hikariConfigBean.connectionTestQuery)) {
    config.setConnectionTestQuery(hikariConfigBean.connectionTestQuery);
  }

  if(hikariConfigBean.transactionIsolation != TransactionIsolationLevel.DEFAULT) {
    config.setTransactionIsolation(hikariConfigBean.transactionIsolation.name());
  }

  if(StringUtils.isNotEmpty(hikariConfigBean.initialQuery)) {
    config.setConnectionInitSql(hikariConfigBean.initialQuery);
  }

  config.setDataSourceProperties(hikariConfigBean.getDriverProperties());

  return config;
}
 
Example 10
Source File: ShardingConfig.java    From java-tutorial with MIT License 4 votes vote down vote up
/**
 * 配置数据源配置
 *
 * @return
 */
private Map<String, DataSource> getDataSourceMap() {
    Map<String, DataSource> dataSourceMap = new HashMap<>(4);
    if (datasource.isEmpty()) {
        new ConfigurationException("Data source configuration error");
    }

    Properties properties = new Properties();
    properties.setProperty("testOnBorrow", "true");
    properties.setProperty("testWhileIdle", "true");
    properties.setProperty("testOnReturn", "false");
    properties.setProperty("validationQuery", "select 'x'");

    for (String ds : datasource.keySet()) {
        LinkedHashMap<String, String> dbConfigList = (LinkedHashMap<String, String>) datasource.get(ds);
        String driverClassName = dbConfigList.get("driverClassName");

        String name = dbConfigList.get("alias");
        String jdbcUrl = dbConfigList.get("url");
        String dbUsername = dbConfigList.get("username");
        String dbPassword = dbConfigList.get("password");

        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName(driverClassName);
        hikariConfig.setJdbcUrl(jdbcUrl);
        hikariConfig.setUsername(dbUsername);
        hikariConfig.setPassword(dbPassword);

        //等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒
        hikariConfig.setConnectionTimeout(30000);
        //一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟
        hikariConfig.setIdleTimeout(30000);
        //一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,
        // 参考MySQL wait_timeout参数(show variables like '%timeout%';)
        hikariConfig.setMaxLifetime(1800000);
        //连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)
        hikariConfig.setMaximumPoolSize(60);
        hikariConfig.setMinimumIdle(10);
        hikariConfig.setDataSourceProperties(properties);

        HikariDataSource dataSource = new HikariDataSource(hikariConfig);
        logger.info("-------------------Init HikariDataSource {} ---------------------------", name);
        dataSourceMap.put(ds, dataSource);
    }
    return dataSourceMap;
}
 
Example 11
Source File: SkinStorage.java    From ChangeSkin with MIT License 4 votes vote down vote up
public SkinStorage(ChangeSkinCore core, String driver, String host, int port, String database
        , String user, String pass, boolean useSSL) {
    this.logger = core.getLogger();

    HikariConfig config = new HikariConfig();
    config.setPoolName(core.getPlugin().getName());

    config.setUsername(user);
    config.setPassword(pass);
    config.setDriverClassName(driver);

    ThreadFactory threadFactory = core.getPlugin().getThreadFactory();
    if (threadFactory != null) {
        config.setThreadFactory(threadFactory);
    }

    Properties properties = new Properties();

    String jdbcUrl = "jdbc:";
    if (driver.contains("sqlite")) {
        String folderPath = core.getPlugin().getPluginFolder().toAbsolutePath().toString();
        database = database.replace("{pluginDir}", folderPath);

        jdbcUrl += "sqlite://" + database;
        config.setConnectionTestQuery("SELECT 1");
        config.setMaximumPoolSize(1);

        //a try to fix https://www.spigotmc.org/threads/fastlogin.101192/page-26#post-1874647
        properties.setProperty("date_string_format", "yyyy-MM-dd HH:mm:ss");
    } else {
        jdbcUrl += "mysql://" + host + ':' + port + '/' + database;
        properties.setProperty("useSSL", String.valueOf(useSSL));
        // enable MySQL specific optimizations
        // default prepStmtCacheSize 25 - amount of cached statements - enough for us
        // default prepStmtCacheSqlLimit 256 - length of SQL - our queries are not longer
        // disabled by default - will return the same prepared statement instance
        config.addDataSourceProperty("cachePrepStmts", true);
        // default false - available in newer versions caches the statements server-side
        config.addDataSourceProperty("useServerPrepStmts", true);
    }

    config.setJdbcUrl(jdbcUrl);
    config.setDataSourceProperties(properties);
    this.dataSource = new HikariDataSource(config);
}