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

The following examples show how to use com.zaxxer.hikari.HikariDataSource#setConnectionTestQuery() . 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: 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 3
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);
    }
}