java - HikariCP:连接池超时时间
巴扎黑
巴扎黑 2017-04-18 10:31:59
[Java讨论组]

现有一耗时操作,为避免请求长时间未返回而抛出异常设置了超时时间

HikariConfig config = new HikariConfig();
config.setDriverClassName("com.mysql.jdbc.Driver");
config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mysql");
config.setUsername("root");
config.setPassword("123456");
config.setConnectionTimeout(1000); // 设置超时时间1秒

为了明确setConnectionTimeout()的作用,我导入了30万条数据进行查询并将超时时间设置为1秒,启动后查询出结果并耗时2秒

【预期结果】由于设置了timeout为1秒,实际耗时2秒,所以会抛出超时异常

【实际结果】没有抛出超时异常,耗时2秒查询出了结果

请教前辈们我是不是对JDBC中的timeout有误解呢?这里的timeout不是指查询超时时间?

巴扎黑
巴扎黑

全部回复(1)
天蓬老师

不是。这个是获取连接的超时时间,就是从连接池返回连接的超时时间。

connectionTimeout
This property controls the maximum number of milliseconds that a client (that's you) will wait for a connection from the pool. If this time is exceeded without a connection becoming available, a SQLException will be thrown. Lowest acceptable connection timeout is 250 ms. Default: 30000 (30 seconds)
上面的是官方描述。

SQL 执行超时时间
JDBC 可以直接使用 Statement.setQueryTimeout
Spring 可以使用 @Transactional(timeout=10)

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号