有没有一种方法可以将Hikari配置为“快速失败"?数据库连接数据是否无效? [英] Is there a way to configure Hikari to "fail fast" if the database connection data is invalid?

查看:370
本文介绍了有没有一种方法可以将Hikari配置为“快速失败"?数据库连接数据是否无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用无效的主机名或用户名/密码运行此测试,它将等待约2分钟,然后失败.理想情况下,如果用户/密码不正确,或者主机名/端口不正确,我希望立即使它失败.

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.2.9引入了快速失败选项(当前版本为1.3.3).配置属性为initializationFailFast.将其设置为true,则池应会快速失败.在日志记录框架(log4j,slf4j等)中为com.zaxxer.hikari启用调试日志记录可以提供有关发生连接失败原因的更多详细信息.

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.

这篇关于有没有一种方法可以将Hikari配置为“快速失败"?数据库连接数据是否无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆