Search code examples
hikaricp

Is there a way to configure Hikari to "fail fast" if the database connection data is invalid?


Running this test with an invalid hostname, or user/password, it waits about 2 minutes before failing. I would ideally like to have it fail immediately if user/password is incorrect, or if the hostname/port are not correct.

    HikariConfig config = new HikariConfig();
    config.setMaximumPoolSize(1);
    config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
    config.addDataSourceProperty("serverName", "localhost");
    config.addDataSourceProperty("url", "jdbc:mysql://localhost:3306/project_one?useServerPrepStmts=true&autoReconnect=false");
    config.addDataSourceProperty("port", "3306");
    config.addDataSourceProperty("databaseName", "project_one");
    config.addDataSourceProperty("user", "root");
    config.addDataSourceProperty("password", "");
    config.addDataSourceProperty("autoReconnect", false);

    HikariDataSource ds = new HikariDataSource(config);
    Connection connection = ds.getConnection();

    Statement statement = connection.createStatement();
    statement.executeQuery("SELECT 1");

Solution

  • Yes, release 1.2.9 introduced a fail-fast option (current release is 1.3.3). The configuration property is initializationFailFast. Set that to true, and the pool should fail quickly. Enabling debug logging for com.zaxxer.hikari in your logging framework (log4j, slf4j, etc) can provide more details about why the connection failure occurred.