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;
  }
Пример #2
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");
    }
  }