如果 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 连接设置没有按预期工作。