Example #1
1
 @Bean(destroyMethod = "close")
 DataSource dataSource() {
   HikariConfig dataSourceConfig = new HikariConfig();
   dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url"));
   dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
   dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
   dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));
   return new HikariDataSource(dataSourceConfig);
 }
 public static JdbcFs getFileSystemProvider(String url, String user, String pass, String dialect) {
   HikariConfig config = new HikariConfig();
   config.setDataSource(new DriverManagerDataSource(url, user, pass));
   config.setMinimumIdle(1);
   config.setMaximumPoolSize(2);
   return new JdbcFs(
       new AlarmEnabledDataSource(
           url, ChimeraFsHelper.class.getSimpleName(), new HikariDataSource(config)),
       dialect);
 }
Example #3
0
  /**
   * Slave(Write) DataSource Bean Configuration for HikariCP
   *
   * @return
   */
  @Bean(destroyMethod = "shutdown")
  public DataSource readDataSource() {
    HikariConfig config = getHikariConfig();

    config.addDataSourceProperty("user", slaveUser);
    config.addDataSourceProperty("password", slavePassword);
    config.setDriverClassName(slaveDriverClassName);
    config.setJdbcUrl(slaveDataUrl);

    return new HikariDataSource(config);
  }
Example #4
0
  /**
   * Master(Write) DataSource Bean Configuration for HikariCP
   *
   * @return
   */
  @Bean(destroyMethod = "shutdown")
  public DataSource writeDataSource() {
    HikariConfig config = getHikariConfig();

    config.addDataSourceProperty("user", masterUser);
    config.addDataSourceProperty("password", masterPassword);
    config.setDriverClassName(masterDriverClassName);
    config.setJdbcUrl(masterDataUrl);

    return new HikariDataSource(config);
  }
Example #5
0
  @Bean
  public DataSource dataSource() {

    HikariConfig config = new HikariConfig();
    config.setDriverClassName("com.mysql.jdbc.Driver");
    config.setJdbcUrl("jdbc:mysql://127.0.0.1:3307/meer-spring-jpa");
    config.setUsername("root");
    config.setPassword("");

    return new HikariDataSource(config);
  }
  @Bean(destroyMethod = "close")
  public DataSource dataSource() throws SQLException {
    HikariConfig hikariConfig = new HikariConfig();

    hikariConfig.setDriverClassName(databaseDriverClassName);
    hikariConfig.setJdbcUrl(datasourceUrl);
    hikariConfig.setUsername(databaseUsername);
    hikariConfig.setPassword(databasePassword);

    return new HikariDataSource(hikariConfig);
  }
  @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);
  }
Example #8
0
  @Provides
  @Singleton
  DataSource provideDataSource() {
    HikariConfig config = new HikariConfig();

    // The URL and driver should be changed to match your database
    // for MSSQL, this is a valid URI, cwad is the address of the database server and LM is the
    // database name
    // Similar to saying "USE LM" in your sql code
    config.setJdbcUrl("jdbc:sqlserver://cwad;database=LM");
    config.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    config.setConnectionTimeout(20000);
    config.setMaximumPoolSize(20);

    // Change this to an easy to identify thread pool name.  This helps with debugging
    config.setPoolName("LM-DB-POOL");

    // This should probably be gotten in a different way, ideally it would be something like
    // an environment variable or a config file value rather than a hard coded value.
    config.setUsername("perseus");
    config.setPassword("perseus");

    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
    }

    if (metricRegistry != null) {
      config.setMetricRegistry(metricRegistry);
    }
    return new HikariDataSource(config);
  }
  @Bean(destroyMethod = "close")
  public DataSource dataSource() {

    HikariConfig config = new HikariConfig();
    config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
    config.addDataSourceProperty(
        "url",
        "jdbc:postgresql://ec2-54-225-223-40.compute-1.amazonaws.com:5432/dc1jif337ap365?sslmode=require");
    config.addDataSourceProperty("user", "bitlvzkeswwxad");
    config.addDataSourceProperty("password", "bbSAJrt6D4S8rntrm-rJKhKIfr");
    // config.setJdbcUrl("jdbc://ec2-54-225-223-40.compute-1.amazonaws.com:5432/dc1jif337ap365");

    return new HikariDataSource(config);
  }
  @Bean
  public DataSource configureDataSource() {
    HikariConfig config = new HikariConfig();
    config.setDriverClassName(driver);
    config.setJdbcUrl(url);
    config.setUsername(username);
    config.setPassword(password);
    config.addDataSourceProperty("cachePrepStmts", "true");
    config.addDataSourceProperty("prepStmtCacheSize", "250");
    config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
    config.addDataSourceProperty("useServerPrepStmts", "true");

    return new HikariDataSource(config);
  }
  @Bean
  public DataSource dataSource() {
    log.debug("Configuring Datasource");
    if (propertyResolver.getProperty("url") == null
        && propertyResolver.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(environment.getActiveProfiles()));

      throw new ApplicationContextException("Database connection pool is not configured correctly");
    }
    HikariConfig config = new HikariConfig();
    config.setDataSourceClassName(propertyResolver.getProperty("dataSourceClassName"));
    if (propertyResolver.getProperty("url") == null
        || "".equals(propertyResolver.getProperty("url"))) {
      config.addDataSourceProperty("databaseName", propertyResolver.getProperty("databaseName"));
      config.addDataSourceProperty("serverName", propertyResolver.getProperty("serverName"));
    } else {
      config.addDataSourceProperty("url", propertyResolver.getProperty("url"));
    }
    config.addDataSourceProperty("user", propertyResolver.getProperty("username"));
    config.addDataSourceProperty("password", propertyResolver.getProperty("password"));
    return new HikariDataSource(config);
  }
  // @Test
  public void testConnectionCloseBlocking() throws SQLException {
    HikariConfig config = new HikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(1);
    config.setConnectionTimeout(1500);
    config.setDataSource(new CustomMockDataSource());

    long start = ClockSource.INSTANCE.currentTime();
    try (HikariDataSource ds = new HikariDataSource(config)) {
      Connection connection = ds.getConnection();
      connection.close();

      // Hikari only checks for validity for connections with lastAccess > 1000 ms so we sleep for
      // 1001 ms to force
      // Hikari to do a connection validation which will fail and will trigger the connection to be
      // closed
      UtilityElf.quietlySleep(1100L);

      shouldFail = true;

      // on physical connection close we sleep 2 seconds
      connection = ds.getConnection();

      Assert.assertTrue(
          "Waited longer than timeout",
          (ClockSource.INSTANCE.elapsedMillis(start) < config.getConnectionTimeout()));
    } catch (SQLException e) {
      Assert.assertTrue(
          "getConnection failed because close connection took longer than timeout",
          (ClockSource.INSTANCE.elapsedMillis(start) < config.getConnectionTimeout()));
    }
  }
  public static HikariDataSource getHikari() {

    config.setJdbcUrl(urlHC);
    config.setUsername(userHC);
    config.setPassword(passwordHC);
    config.addDataSourceProperty("cachePrepStmts", "true");
    config.addDataSourceProperty("prepStmtCacheSize", "250");
    config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
    config.setMaximumPoolSize(3);
    config.setIdleTimeout(28740000);
    config.setMaxLifetime(28740000);
    config.setConnectionTimeout(34000);

    HikariDataSource ds = new HikariDataSource(config);

    System.out.println("Returning HikairDataSource \n");
    return ds;
  }
Example #15
0
  /**
   * get HikariCP Common Configuration
   *
   * @return
   */
  private HikariConfig getHikariConfig() {
    HikariConfig config = new HikariConfig();

    config.setMinimumIdle(minimumIdle);
    config.setMaximumPoolSize(maximumPoolSize);
    config.setConnectionTestQuery(validationQuery);
    config.setConnectionTimeout(connectionTimeout);
    config.setAutoCommit(isAutoCommit);

    config.addDataSourceProperty("cachePrepStmts", cachePrepStmts);
    config.addDataSourceProperty("prepStmtCacheSize", prepStmtCacheSize);
    config.addDataSourceProperty("useServerPrepStmts", useServerPrepStmts);

    return config;
  }
Example #16
0
 public void init() throws ServletException {
   super.init();
   HikariConfig config = new HikariConfig();
   config.setMaximumPoolSize(100);
   config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
   config.addDataSourceProperty("serverName", "localhost");
   config.addDataSourceProperty("port", "3306");
   config.addDataSourceProperty("databaseName", "Q4");
   config.addDataSourceProperty("user", "root");
   config.addDataSourceProperty("password", "root");
   this.ds = new HikariDataSource(config);
 }
  public MySQLDatabase(
      Logger log, String ip, String port, String database, String username, String password) {
    super(log, "MySQL");
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setJdbcUrl("jdbc:mysql://" + ip + ":" + port + "/" + database);
    hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
    hikariConfig.setUsername(username);
    hikariConfig.setPassword(password);
    hikariConfig.setMinimumIdle(1);
    hikariConfig.setMaximumPoolSize(10);
    hikariConfig.setConnectionTimeout(10000);

    _source = new HikariDataSource(hikariConfig);
  }
Example #18
0
  /**
   * Configure the database pool.
   *
   * @param logger A JUL logger to use for error reporting, etc.
   * @param user The user to connect as
   * @param pass The password to use
   * @param host The host to connect to
   * @param port The port to connect to
   * @param database The database to use
   * @param poolSize How many connections at most to keep in the connection pool awaiting activity.
   *     Consider this against your Database's max connections across all askers.
   * @param connectionTimeout How long will a single connection wait for activity before timing out.
   * @param idleTimeout How long will a connection wait in the pool, typically, inactive.
   * @param maxLifetime How long will a connection be kept alive at maximum
   */
  public Database(
      Logger logger,
      String user,
      String pass,
      String host,
      int port,
      String database,
      int poolSize,
      long connectionTimeout,
      long idleTimeout,
      long maxLifetime) {
    this.logger = logger;
    if (user != null && host != null && port > 0 && database != null) {
      HikariConfig config = new HikariConfig();
      config.setJdbcUrl("jdbc:mysql://" + host + ":" + port + "/" + database);
      config.setConnectionTimeout(connectionTimeout); // 10000l);
      config.setIdleTimeout(idleTimeout); // 600000l);
      config.setMaxLifetime(maxLifetime); // 7200000l);
      config.setMaximumPoolSize(poolSize); // 10);
      config.setUsername(user);
      if (pass != null) {
        config.setPassword(pass);
      }
      this.datasource = new HikariDataSource(config);

      try { // No-op test.
        Connection connection = getConnection();
        Statement statement = connection.createStatement();
        statement.execute("SELECT 1");
        statement.close();
        connection.close();
      } catch (SQLException se) {
        this.logger.log(Level.SEVERE, "Unable to initialize Database", se);
        this.datasource = null;
      }
    } else {
      this.datasource = null;
      this.logger.log(Level.SEVERE, "Database not configured and is unavaiable");
    }
  }
  @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);
  }
  @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);
  }
  @Provides
  @Singleton
  public HikariDataSource provideConnectionPool() {

    // See https://github.com/brettwooldridge/HikariCP

    final HikariConfig config = new HikariConfig();
    config.setMaximumPoolSize(15);
    config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
    config.addDataSourceProperty("serverName", "localhost");
    config.addDataSourceProperty("port", "3306");
    config.addDataSourceProperty("databaseName", "HiveMQ3");
    config.addDataSourceProperty("user", "root");
    config.addDataSourceProperty("password", "root");

    // See https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration

    config.addDataSourceProperty("cachePrepStmts", true);
    config.addDataSourceProperty("prepStmtCacheSize", 250);
    config.addDataSourceProperty("useServerPrepStmts", true);

    return new HikariDataSource(config);
  }