/**
   * Constructor used for MySQL
   *
   * @param host
   * @param port
   * @param database
   * @param username
   * @param password
   * @throws SQLException
   */
  public DataSourceHandler(
      final String host,
      final String port,
      final String database,
      final String username,
      final String password)
      throws SQLException {
    // Check database's informations and init connection
    this.host = Preconditions.checkNotNull(host);
    this.port = Preconditions.checkNotNull(port);
    this.database = Preconditions.checkNotNull(database);
    this.username = Preconditions.checkNotNull(username);
    this.password = Preconditions.checkNotNull(password);

    BAT.getInstance().getLogger().config("Initialization of HikariCP in progress ...");
    BasicConfigurator.configure(new NullAppender());
    ds = new HikariDataSource();
    ds.setJdbcUrl(
        "jdbc:mysql://"
            + this.host
            + ":"
            + this.port
            + "/"
            + this.database
            + "?useLegacyDatetimeCode=false&serverTimezone="
            + TimeZone.getDefault().getID());
    ds.setUsername(this.username);
    ds.setPassword(this.password);
    ds.addDataSourceProperty("cachePrepStmts", "true");
    ds.setMaximumPoolSize(8);
    try {
      final Connection conn = ds.getConnection();
      int intOffset =
          Calendar.getInstance().getTimeZone().getOffset(Calendar.getInstance().getTimeInMillis())
              / 1000;
      String offset =
          String.format("%02d:%02d", Math.abs(intOffset / 3600), Math.abs((intOffset / 60) % 60));
      offset = (intOffset >= 0 ? "+" : "-") + offset;
      conn.createStatement().executeQuery("SET time_zone='" + offset + "';");
      conn.close();
      BAT.getInstance().getLogger().config("BoneCP is loaded !");
    } catch (final SQLException e) {
      BAT.getInstance()
          .getLogger()
          .severe(
              "BAT encounters a problem during the initialization of the database connection."
                  + " Please check your logins and database configuration.");
      if (e.getCause() instanceof CommunicationsException) {
        BAT.getInstance().getLogger().severe(e.getCause().getMessage());
      }
      if (BAT.getInstance().getConfiguration().isDebugMode()) {
        BAT.getInstance().getLogger().log(Level.SEVERE, e.getMessage(), e);
      }
      throw e;
    }
    sqlite = false;
  }
  @Bean(name = "datasource")
  public DataSource restDataSource() {
    HikariDataSource dataSource = new HikariDataSource();
    dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
    dataSource.setJdbcUrl(env.getProperty("jdbc.url"));
    dataSource.setUsername(env.getProperty("jdbc.user"));
    dataSource.setPassword(env.getProperty("jdbc.pass"));
    dataSource.setMaximumPoolSize(Integer.valueOf(env.getProperty("jdbc.maximumPoolSize")));

    return dataSource;
  }
 /**
  * Configure datasource.
  *
  * @return datasource
  * @throws PropertyVetoException
  */
 @Bean(destroyMethod = "close")
 public DataSource dataSource() throws PropertyVetoException {
   HikariDataSource dataSource = new HikariDataSource();
   dataSource.setDriverClassName(environment.getProperty("jdbc.driverClass"));
   dataSource.setJdbcUrl(environment.getProperty("jdbc.url"));
   dataSource.setUsername(environment.getProperty("jdbc.user"));
   dataSource.setPassword(environment.getProperty("jdbc.password"));
   dataSource.setMinimumIdle(environment.getProperty("datasource.pool.min_size", Integer.class));
   dataSource.setMaximumPoolSize(
       environment.getProperty("datasource.pool.max_size", Integer.class));
   return dataSource;
 }
 public void connect() {
   if (dataSource == null || dataSource.isClosed()) {
     dataSource = new HikariDataSource();
     dataSource.setJdbcUrl(jdbcURL);
     dataSource.setUsername(username);
     dataSource.setPoolName(poolname);
     dataSource.setAutoCommit(autoCommit);
     dataSource.setMaximumPoolSize(maximalPoolSize);
     if (passwd != null) dataSource.setPassword(passwd);
     if (sqlInit != null) dataSource.setConnectionInitSql(sqlInit);
     if (sqlTest != null) dataSource.setConnectionTestQuery(sqlTest);
     if (timeout != null) dataSource.setConnectionTimeout(timeout);
     if (jdbcDriverClassName != null) dataSource.setDriverClassName(jdbcDriverClassName);
   }
 }
Beispiel #5
0
  private ConnectionSource setupConnection(DatabaseConfig dbConfig) throws SQLException {
    HikariDataSource ds = new HikariDataSource();

    if (!dbConfig.getUser().isEmpty()) {
      ds.setUsername(dbConfig.getUser());
    }
    if (!dbConfig.getPassword().isEmpty()) {
      ds.setPassword(dbConfig.getPassword());
    }

    ds.setJdbcUrl(dbConfig.getJDBCUrl());

    ds.setMaximumPoolSize(dbConfig.getMaxConnections());
    /* Keep the connection open for 5 minutes */
    //    ds.setMaxLifetime(300000);

    return new DataSourceConnectionSource(ds, new MySQLDatabase());
  }
  /**
   * @return Connection from the connection pool. Clients must <code>close()</code> it when they are
   *     done with it.
   * @throws SQLException
   */
  public static synchronized Connection getConnection() throws SQLException {
    if (dataSource == null) {
      final Configuration config = Application.getConfiguration();
      final String connectionString = config.getString(JDBC_URL_CONFIG_KEY, "");
      final int connectionTimeout = 1000 * config.getInt(CONNECTION_TIMEOUT_CONFIG_KEY, 10);
      final int maxPoolSize = config.getInt(MAX_POOL_SIZE_CONFIG_KEY, 10);
      final String user = config.getString(USER_CONFIG_KEY, "");
      final String password = config.getString(PASSWORD_CONFIG_KEY, "");

      dataSource = new HikariDataSource();
      dataSource.setJdbcUrl(connectionString);
      dataSource.setUsername(user);
      dataSource.setPassword(password);
      dataSource.setPoolName("JdbcResolverPool");
      dataSource.setMaximumPoolSize(maxPoolSize);
      dataSource.setConnectionTimeout(connectionTimeout);
    }
    return dataSource.getConnection();
  }
Beispiel #7
0
  @BeforeClass
  public static void initDatabaseScheme() throws Exception {
    HikariDataSource dataSource = new HikariDataSource();
    dataSource.setJdbcUrl("jdbc:h2:mem:test");
    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUsername("user");
    dataSource.setPassword("password");

    queryTemplate = new QueryTemplate(dataSource);

    queryTemplate.run(
        new UpdateQuery() {
          @Override
          public Integer perform(DatabaseConnection connection) throws Exception {
            return connection.executeUpdate(
                "CREATE TABLE Person(id INT PRIMARY KEY, name VARCHAR, birthday DATE, employed BOOLEAN);");
          }
        },
        true);
  }