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

The following examples show how to use com.zaxxer.hikari.HikariDataSource#setMaxLifetime() . 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: DatabaseConfiguration.java    From galeb with Apache License 2.0 6 votes vote down vote up
@Bean
public HikariDataSource dataSource(DataSourceProperties properties) {
    HikariDataSource hikariDataSource = (HikariDataSource) properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    hikariDataSource.setConnectionTimeout(DB_CONN_TIMEOUT);
    hikariDataSource.setMaximumPoolSize(DB_MAX_POOL_SIZE);
    hikariDataSource.setMaxLifetime(DB_MAX_LIFE_TIME);
    hikariDataSource.setAutoCommit(DB_AUTOCOMMIT);
    hikariDataSource.setConnectionTestQuery("SELECT 1");
    hikariDataSource.addDataSourceProperty("cachePrepStmts", DB_CACHE_PREP_STMTS);
    hikariDataSource.addDataSourceProperty("prepStmtCacheSize", DB_PREP_STMT_CACHE_SIZE);
    hikariDataSource.addDataSourceProperty("prepStmtCacheSqlLimit", DB_PREP_STMT_CACHE_SQL_LIMIT);
    hikariDataSource.addDataSourceProperty("useServerPrepStmts", DB_USE_SERVER_PREP_STMTS);
    hikariDataSource.addDataSourceProperty("useLocalSessionState", DB_USE_LOCAL_SESSION_STATE);
    hikariDataSource.addDataSourceProperty("rewriteBatchedStatements", DB_REWRITE_BATCHED_STATEMENTS);
    hikariDataSource.addDataSourceProperty("cacheResultSetMetadata", DB_CACHE_RESULT_SET_METADATA);
    hikariDataSource.addDataSourceProperty("cacheServerConfiguration", DB_CACHE_SERVER_CONFIGURATION);
    hikariDataSource.addDataSourceProperty("elideSetAutoCommits", DB_ELIDE_SET_AUTO_COMMITS);
    hikariDataSource.addDataSourceProperty("maintainTimeStats", DB_MAINTAIN_TIME_STATS);

    return hikariDataSource;
}
 
Example 2
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 3
Source File: JobConfiguration.java    From CogStack-Pipeline with Apache License 2.0 6 votes vote down vote up
@Bean(destroyMethod = "close")
@Primary
@Qualifier("sourceDataSource")
public DataSource sourceDataSource() {
    HikariDataSource mainDatasource = new HikariDataSource();
    executeSessionScripts(mainDatasource,sourceDriver);
    mainDatasource.setDriverClassName(sourceDriver);
    mainDatasource.setJdbcUrl(sourceJdbcPath);
    mainDatasource.setUsername(sourceUserName);
    mainDatasource.setPassword(sourcePassword);
    mainDatasource.setIdleTimeout(sourceIdleTimeout);
    mainDatasource.setMaxLifetime(sourceMaxLifeTime);
    // show tables query does not work with post gres
    if (Arrays.asList(this.env.getActiveProfiles()).contains("docman") && ! Arrays.asList(this.env.getActiveProfiles()).contains("postgres")){
        mainDatasource.setConnectionTestQuery("show tables");
    }
    if (sourcePoolSize > 0){
        mainDatasource.setMaximumPoolSize(sourcePoolSize);
    }
    // set the db connection leak detection time
    mainDatasource.setLeakDetectionThreshold(sourceLeakDetection);
    return mainDatasource;
}
 
Example 4
Source File: JobConfiguration.java    From CogStack-Pipeline with Apache License 2.0 6 votes vote down vote up
@Profile({"jdbc_out", "jdbc_out_map"})
@Bean(destroyMethod = "close")
@Qualifier("targetDataSource")
public DataSource targetDataSource() {
    HikariDataSource mainDatasource = new HikariDataSource();
    // read and set the mandatory DB connector properties
    executeSessionScripts(mainDatasource, env.getRequiredProperty("target.Driver"));
    mainDatasource.setDriverClassName(env.getRequiredProperty("target.Driver"));
    mainDatasource.setJdbcUrl(env.getRequiredProperty("target.JdbcPath"));
    mainDatasource.setUsername(env.getRequiredProperty("target.username"));
    mainDatasource.setPassword(env.getRequiredProperty("target.password"));
    // set optional properties
    mainDatasource.setIdleTimeout(targetIdleTimeout);
    mainDatasource.setMaxLifetime(targetMaxLifeTime);
    if (targetPoolSize > 0){
        mainDatasource.setMaximumPoolSize(targetPoolSize);
    }
    return mainDatasource;
}
 
Example 5
Source File: BatchConfigurer.java    From CogStack-Pipeline with Apache License 2.0 6 votes vote down vote up
@Bean(destroyMethod = "close")
@Qualifier("jobRepositoryDataSource")
public DataSource jobRepositoryDataSource() {
    HikariDataSource mainDatasource = new HikariDataSource();
    mainDatasource.setDriverClassName(repoDriver);
    mainDatasource.setJdbcUrl(repoJdbcPath);
    mainDatasource.setUsername(repoUserName);
    mainDatasource.setPassword(repoPassword);
    mainDatasource.setIdleTimeout(repoIdleTimeoutMs);
    mainDatasource.setMaxLifetime(repoMaxLifeTimeMs);

    if (repoPoolSize > 0) {
        mainDatasource.setMaximumPoolSize(repoPoolSize);
    }
    //mainDatasource.setAutoCommit(false);
    return mainDatasource;
}
 
Example 6
Source File: PostgreSqlDataSource.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Sets up the connection arguments to the database.
 */
private void setConnectionArguments() {
    ds = new HikariDataSource();
    ds.setPoolName("AuthMePostgreSQLPool");

    // Pool Settings
    ds.setMaximumPoolSize(poolSize);
    ds.setMaxLifetime(maxLifetime * 1000);

    // Database URL
    ds.setDriverClassName("org.postgresql.Driver");
    ds.setJdbcUrl("jdbc:postgresql://" + this.host + ":" + this.port + "/" + this.database);

    // Auth
    ds.setUsername(this.username);
    ds.setPassword(this.password);

    // Random stuff
    ds.addDataSourceProperty("reWriteBatchedInserts", "true");

    // Caching
    ds.addDataSourceProperty("cachePrepStmts", "true");
    ds.addDataSourceProperty("preparedStatementCacheQueries", "275");

    logger.info("Connection arguments loaded, Hikari ConnectionPool ready!");
}
 
Example 7
Source File: HikariDataSourceFactory.java    From PeonyFramwork with Apache License 2.0 5 votes vote down vote up
private DataSource create() {
        HikariDataSource ds = new HikariDataSource();
        ds.setJdbcUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setDriverClassName(driver);

        ds.setReadOnly(false);
        // 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒
        ds.setConnectionTimeout(30000);
        // 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟
//        config.setIdleTimeout(configEntity.getMaxIdleTime());
        // 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQLwait_timeout参数(show variables like '%timeout%';)
        ds.setMaxLifetime(1800000 - 60000);
        // 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)
        ds.setMaximumPoolSize(Runtime.getRuntime().availableProcessors() * 5 + 30);
        ds.setConnectionInitSql("set names utf8mb4"); // 支持表情符
//        ds.setInitialSize(1);
//        config.setMinIdle(1);
//        ds.setMaxActive(400);
//        ds.setMaxWait(10000);
//        ds.setTimeBetweenEvictionRunsMillis(600000);
//        ds.setMinEvictableIdleTimeMillis(600000);
//        ds.setTestWhileIdle(true);
//        ds.setTestOnBorrow(true);
//        ds.setTestOnReturn(false);
//        ds.setValidationQuery("select 1");
//        ds.setConnectionInitSqls(Arrays.asList("set names utf8mb4")); // 支持表情符

//        ds.addDataSourceProperty("cachePrepStmts", "true");
//        ds.addDataSourceProperty("prepStmtCacheSize", "250");
//        ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        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: MySQL.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sets up the connection arguments to the database.
 */
private void setConnectionArguments() {
    ds = new HikariDataSource();
    ds.setPoolName("AuthMeMYSQLPool");

    // Pool Settings
    ds.setMaximumPoolSize(poolSize);
    ds.setMaxLifetime(maxLifetime * 1000);

    // Database URL
    ds.setJdbcUrl("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database);

    // Auth
    ds.setUsername(this.username);
    ds.setPassword(this.password);

    // Request mysql over SSL
    ds.addDataSourceProperty("useSSL", String.valueOf(useSsl));

    // Disabling server certificate verification on need
    if (!serverCertificateVerification) {
        ds.addDataSourceProperty("verifyServerCertificate", String.valueOf(false));
    }

    // Encoding
    ds.addDataSourceProperty("characterEncoding", "utf8");
    ds.addDataSourceProperty("encoding", "UTF-8");
    ds.addDataSourceProperty("useUnicode", "true");

    // Random stuff
    ds.addDataSourceProperty("rewriteBatchedStatements", "true");
    ds.addDataSourceProperty("jdbcCompliantTruncation", "false");

    // Caching
    ds.addDataSourceProperty("cachePrepStmts", "true");
    ds.addDataSourceProperty("prepStmtCacheSize", "275");
    ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

    logger.info("Connection arguments loaded, Hikari ConnectionPool ready!");
}
 
Example 10
Source File: HikariCPProvider.java    From tastjava with MIT License 4 votes vote down vote up
@Override
public boolean register() {
    if (IsRegistered) {
        return HAS_REGISTERED;
    }

    HikariDataSource ds = new HikariDataSource();

    //basic config
    ds.setJdbcUrl(jdbcURL);
    ds.setDriverClassName(jdbcDriver);
    ds.setUsername(jdbcUsername);
    ds.setPassword(jdbcPassword);

    //custom config
    ds.setAutoCommit(autoCommit);
    ds.setConnectionTimeout(connectionTimeout);
    ds.setIdleTimeout(idleTimeout);
    ds.setMaxLifetime(maxLifetime);
    ds.setMaximumPoolSize(maximumPoolSize);
    ds.setValidationTimeout(validationTimeout);
    ds.setLeakDetectionThreshold(leakDetectionThreshold);

    if (!StrUtil.isBlank(poolName)) {
        ds.setPoolName(poolName);
    }

    if (!StrUtil.isBlank(catalog)) {
        ds.setCatalog(catalog);
    }

    if (!StrUtil.isBlank(connectionInitSql)) {
        ds.setConnectionInitSql(connectionInitSql);
    }

    if (!StrUtil.isBlank(transactionIsolation)) {
        ds.setTransactionIsolation(transactionIsolation);
    }

    if (jdbcURL.contains(":mysql:")) {
        ds.addDataSourceProperty("cachePrepStmts", "true");
        ds.addDataSourceProperty("prepStmtCacheSize", "250");
        ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        ds.addDataSourceProperty("useServerPrepStmts", "true");
    }

    setDataSource(ds);
    setIsRegistered(HAS_REGISTERED);
    return HAS_REGISTERED;
}