java - HikariCP:连接池超时时间
本文介绍了java - HikariCP:连接池超时时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
现有一耗时操作,为避免请求长时间未返回而抛出异常设置了超时时间
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不是指查询超时时间?
解决方案
不是。这个是获取连接的超时时间,就是从连接池返回连接的超时时间。
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.setQueryTimeoutSpring
可以使用 @Transactional(timeout=10)
这篇关于java - HikariCP:连接池超时时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文