Home Ask Login Register

Developers Planet

Your answer is one click away!

Christian February 2016

Check for connections and eventually reconnect using dropwizard 0.9.2 and JDBI

I'm using Drowizard 0.9.2 with JDBI to connect to my MySql Server. Now it could happen that my MySql Database is not active if my app starts due to async deployments. I then want my App to loop and check each 5 secs or so whether it can reach the database.

How is that possible with the framework mentioned above?


Christian February 2016

Just fyi, I've found a solution that works for me.

This method checks for a connection. If it returns false, I'll enter a synchronous loop that checks (and also establish a connection) when possible..

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){
  } catch (Exception e){
    LOGGER.error("Error trying to close connection");
    return false;
return true;

Unfortunately, I can't use ConnectionFactory as it is a private member of DBI.

Post Status

Asked in February 2016
Viewed 3,054 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life