Created
October 8, 2019 15:35
-
-
Save jonathanmza/38c60dde50f6f70d4a7cbfb93d598d17 to your computer and use it in GitHub Desktop.
Simple database health check with Spring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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