@Bean(destroyMethod = "close")
  @ConditionalOnExpression(
      "#{!environment.acceptsProfiles('cloud') && !environment.acceptsProfiles('heroku')}")
  public DataSource dataSource(
      DataSourceProperties dataSourceProperties, JHipsterProperties jHipsterProperties) {
    log.debug("Configuring Datasource");
    if (dataSourceProperties.getUrl() == null) {
      log.error(
          "Your database connection pool configuration is incorrect! The application"
              + " cannot start. Please check your Spring profile, current profiles are: {}",
          Arrays.toString(env.getActiveProfiles()));

      throw new ApplicationContextException("Database connection pool is not configured correctly");
    }
    HikariConfig config = new HikariConfig();
    config.setDataSourceClassName(dataSourceProperties.getDriverClassName());
    config.addDataSourceProperty("url", dataSourceProperties.getUrl());
    if (dataSourceProperties.getUsername() != null) {
      config.addDataSourceProperty("user", dataSourceProperties.getUsername());
    } else {
      config.addDataSourceProperty("user", ""); // HikariCP doesn't allow null user
    }
    if (dataSourceProperties.getPassword() != null) {
      config.addDataSourceProperty("password", dataSourceProperties.getPassword());
    } else {
      config.addDataSourceProperty("password", ""); // HikariCP doesn't allow null password
    }

    if (metricRegistry != null) {
      config.setMetricRegistry(metricRegistry);
    }
    return new HikariDataSource(config);
  }
  @Bean(destroyMethod = "shutdown")
  @ConditionalOnExpression(
      "#{!environment.acceptsProfiles('cloud') && !environment.acceptsProfiles('heroku')}")
  public DataSource dataSource() {
    log.debug("Configuring Datasource");
    if (dataSourcePropertyResolver.getProperty("url") == null
        && dataSourcePropertyResolver.getProperty("databaseName") == null) {
      log.error(
          "Your database connection pool configuration is incorrect! The application"
              + " cannot start. Please check your Spring profile, current profiles are: {}",
          Arrays.toString(env.getActiveProfiles()));

      throw new ApplicationContextException("Database connection pool is not configured correctly");
    }
    HikariConfig config = new HikariConfig();
    config.setDataSourceClassName(dataSourcePropertyResolver.getProperty("dataSourceClassName"));
    if (StringUtils.isEmpty(dataSourcePropertyResolver.getProperty("url"))) {
      config.addDataSourceProperty(
          "databaseName", dataSourcePropertyResolver.getProperty("databaseName"));
      config.addDataSourceProperty(
          "serverName", dataSourcePropertyResolver.getProperty("serverName"));
    } else {
      config.addDataSourceProperty("url", dataSourcePropertyResolver.getProperty("url"));
    }
    config.addDataSourceProperty("user", dataSourcePropertyResolver.getProperty("username"));
    config.addDataSourceProperty("password", dataSourcePropertyResolver.getProperty("password"));

    if (metricRegistry != null) {
      config.setMetricRegistry(metricRegistry);
    }
    return new HikariDataSource(config);
  }
  @Bean(destroyMethod = "close")
  @ConditionalOnExpression(
      "#{!environment.acceptsProfiles('cloud') && !environment.acceptsProfiles('heroku')}")
  public DataSource dataSource(
      DataSourceProperties dataSourceProperties, JHipsterProperties jHipsterProperties) {
    log.debug("Configuring Datasource");
    if (dataSourceProperties.getUrl() == null) {
      log.error(
          "Your database connection pool configuration is incorrect! The application"
              + " cannot start. Please check your Spring profile, current profiles are: {}",
          Arrays.toString(env.getActiveProfiles()));

      throw new ApplicationContextException("Database connection pool is not configured correctly");
    }
    HikariConfig config = new HikariConfig();
    config.setDataSourceClassName(dataSourceProperties.getDriverClassName());
    config.addDataSourceProperty("url", dataSourceProperties.getUrl());
    if (dataSourceProperties.getUsername() != null) {
      config.addDataSourceProperty("user", dataSourceProperties.getUsername());
    } else {
      config.addDataSourceProperty("user", ""); // HikariCP doesn't allow null user
    }
    if (dataSourceProperties.getPassword() != null) {
      config.addDataSourceProperty("password", dataSourceProperties.getPassword());
    } else {
      config.addDataSourceProperty("password", ""); // HikariCP doesn't allow null password
    }

    // MySQL optimizations, see https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration
    if ("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
        .equals(dataSourceProperties.getDriverClassName())) {
      config.addDataSourceProperty(
          "cachePrepStmts", jHipsterProperties.getDatasource().isCachePrepStmts());
      config.addDataSourceProperty(
          "prepStmtCacheSize", jHipsterProperties.getDatasource().getPrepStmtCacheSize());
      config.addDataSourceProperty(
          "prepStmtCacheSqlLimit", jHipsterProperties.getDatasource().getPrepStmtCacheSqlLimit());
    }
    if (metricRegistry != null) {
      config.setMetricRegistry(metricRegistry);
    }
    return new HikariDataSource(config);
  }
Ejemplo n.º 4
0
  @Bean(destroyMethod = "shutdown")
  @ConditionalOnExpression(
      "#{!environment.acceptsProfiles('cloud') && !environment.acceptsProfiles('heroku')}")
  public DataSource dataSource() {
    log.debug("Configuring Datasource");
    if (dataSourcePropertyResolver.getProperty("url") == null
        && dataSourcePropertyResolver.getProperty("databaseName") == null) {
      log.error(
          "Your database connection pool configuration is incorrect! The application"
              + " cannot start. Please check your Spring profile, current profiles are: {}",
          Arrays.toString(env.getActiveProfiles()));

      throw new ApplicationContextException("Database connection pool is not configured correctly");
    }
    HikariConfig config = new HikariConfig();
    config.setDataSourceClassName(dataSourcePropertyResolver.getProperty("dataSourceClassName"));
    if (StringUtils.isEmpty(dataSourcePropertyResolver.getProperty("url"))) {
      config.addDataSourceProperty(
          "databaseName", dataSourcePropertyResolver.getProperty("databaseName"));
      config.addDataSourceProperty(
          "serverName", dataSourcePropertyResolver.getProperty("serverName"));
    } else {
      config.addDataSourceProperty("url", dataSourcePropertyResolver.getProperty("url"));
    }
    config.addDataSourceProperty("user", dataSourcePropertyResolver.getProperty("username"));
    config.addDataSourceProperty("password", dataSourcePropertyResolver.getProperty("password"));

    // MySQL optimizations, see https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration
    if ("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
        .equals(dataSourcePropertyResolver.getProperty("dataSourceClassName"))) {
      config.addDataSourceProperty(
          "cachePrepStmts", dataSourcePropertyResolver.getProperty("cachePrepStmts", "true"));
      config.addDataSourceProperty(
          "prepStmtCacheSize", dataSourcePropertyResolver.getProperty("prepStmtCacheSize", "250"));
      config.addDataSourceProperty(
          "prepStmtCacheSqlLimit",
          dataSourcePropertyResolver.getProperty("prepStmtCacheSqlLimit", "2048"));
    }
    if (metricRegistry != null) {
      config.setMetricRegistry(metricRegistry);
    }
    return new HikariDataSource(config);
  }