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不是指查询超时时间?

阅读 15k
2 个回答

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

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)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题