使用 dropwizard 0.9.2 和 JDBI 检查连接并最终重新连接

Check for connections and eventually reconnect using dropwizard 0.9.2 and JDBI

我使用带有 JDBI 的 Drowizard 0.9.2 连接到我的 MySql 服务器。 现在,如果我的应用程序由于异步部署而启动,我的 MySql 数据库可能会不活动。然后我希望我的应用程序循环并每 5 秒左右检查一次它是否可以访问数据库。

上述框架如何实现?

仅供参考,我找到了适合我的解决方案。

此方法检查连接。如果它 returns 为假,我将进入一个同步循环,在可能的情况下检查(并建立连接)..

public boolean checkForConnection() {
Handle handle = null;
try {
  jdbi = factory.build(environment, config.getDatabaseFactory(), "postgresql");
   handle = jdbi.open();
} catch (Exception e) {
  LOGGER.error("Error while checking Postgres connection.");
  return false;
} finally {
  try {
    if(handle != null){
      handle.close();
    }
  } catch (Exception e){
    LOGGER.error("Error trying to close connection");
    return false;
  }
}
return true;
}

很遗憾,我无法使用 ConnectionFactory,因为它是 DBI 的私有成员。