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

天涯浪子 提交于 2019-12-11 01:28:32

问题


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");

回答1:


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.




回答2:


Starting from v3.0.0 of HikariCP, you have to use the property initializationFailTimeout.

The property initializationFailFast was deprecated in v2.4.10 and removed in v3.0.0.

Related issue #770 and pull request #771



来源:https://stackoverflow.com/questions/22649479/is-there-a-way-to-configure-hikari-to-fail-fast-if-the-database-connection-dat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!