2

我有两个 sql 数据库连接,dropwizard 会自动为其添加健康检查。但是当应用程序失去与其中之一的连接时,/healthcheck 端点需要无限长的时间来响应,我希望它在几秒钟后超时。

我已经设置了maxWaitForConnection设置,并且我也尝试了各种checkConnectionOn..设置,但没有任何帮助。

更新:如果服务器主动拒绝连接,健康检查会正确失败,但如果不是这种情况,它会无限期挂起,例如网络问题。

无论问题是什么,是否可以在指定的时间值让 sql 健康检查超时?

4

1 回答 1

2

如果 JDBC 超时设置不起作用,您总是可以将数据库检查包装在Future中并限制它可以运行的时间:

protected Result check() throws Exception {
  ExecutorService executor = Executors.newSingleThreadExecutor();
  Future<Void> future = executor.submit(new DbConnectionChecker());

  try {
    future.get(SECONDS_THRESHOLD, TimeUnit.SECONDS);
  } catch (TimeoutException e) {
    return Result.unhealthy("DB timed out");
  }
  executor.shutdownNow();
  return Result.healthy();
}

DbConnectionChecker类似的东西在哪里:

static class DbConnectionChecker implements Callable<Void> {
  public Void call() throws Exception {
    // Check your DB connection as you normally would
  }
}

不过,很高兴弄清楚为什么 JDBC 连接设置没有按预期工作。

于 2015-02-25T16:54:49.143 回答