Skip to content

Instantly share code, notes, and snippets.

@jonathanmza
Created October 8, 2019 15:35
Show Gist options
  • Save jonathanmza/38c60dde50f6f70d4a7cbfb93d598d17 to your computer and use it in GitHub Desktop.
Save jonathanmza/38c60dde50f6f70d4a7cbfb93d598d17 to your computer and use it in GitHub Desktop.
Simple database health check with Spring
package your.package;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class DatabaseHealthCheck {
private final JdbcTemplate template;
public DatabaseHealthCheck(JdbcTemplate template) {
this.template = template;
}
/**
* Check if the database is up
*
* @return a boolean value. True if the database is up, otherwise, it returns false
*/
public boolean isDatabaseUp(){
List<Object> results;
try {
results = template.query("SELECT 1",
new SingleColumnRowMapper<>());
} catch (RuntimeException e) {
results = new ArrayList<>();
}
return !results.isEmpty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment