Commit 8e2a6eec authored by Dave Syer's avatar Dave Syer

Add database query defaults in SimpleHealthManager

parent 063403a0
...@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.health; ...@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.health;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
...@@ -37,32 +38,44 @@ public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object ...@@ -37,32 +38,44 @@ public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
private String query = "SELECT 'Hello'"; private static Map<String, String> queries = new HashMap<String, String>();
static {
queries.put("HSQL Database Engine",
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_USERS");
queries.put("Oracle", "SELECT 'Hello' from DUAL");
queries.put("Apache Derby", "SELECT 1 FROM SYSIBM.SYSDUMMY1");
}
private static String DEFAULT_QUERY = "SELECT 'Hello'";
private String query = null;
@Override @Override
public Map<String, Object> health() { public Map<String, Object> health() {
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>(); LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
map.put("status", "ok"); map.put("status", "ok");
String product = "unknown";
if (this.dataSource != null) { if (this.dataSource != null) {
try { try {
String product = this.jdbcTemplate product = this.jdbcTemplate.execute(new ConnectionCallback<String>() {
.execute(new ConnectionCallback<String>() { @Override
@Override public String doInConnection(Connection connection)
public String doInConnection(Connection connection) throws SQLException, DataAccessException {
throws SQLException, DataAccessException { return connection.getMetaData().getDatabaseProductName();
return connection.getMetaData().getDatabaseProductName(); }
} });
});
map.put("database", product); map.put("database", product);
} }
catch (DataAccessException ex) { catch (DataAccessException ex) {
map.put("status", "error"); map.put("status", "error");
map.put("error", ex.getClass().getName() + ": " + ex.getMessage()); map.put("error", ex.getClass().getName() + ": " + ex.getMessage());
} }
if (StringUtils.hasText(this.query)) { String query = detectQuery(product);
if (StringUtils.hasText(query)) {
try { try {
map.put("hello", map.put("hello",
this.jdbcTemplate.queryForObject(this.query, String.class)); this.jdbcTemplate.queryForObject(query, String.class));
} }
catch (Exception ex) { catch (Exception ex) {
map.put("status", "error"); map.put("status", "error");
...@@ -73,6 +86,17 @@ public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object ...@@ -73,6 +86,17 @@ public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object
return map; return map;
} }
protected String detectQuery(String product) {
String query = this.query;
if (!StringUtils.hasText(query)) {
query = queries.get(product);
}
if (!StringUtils.hasText(query)) {
query = DEFAULT_QUERY;
}
return query;
}
public void setDataSource(DataSource dataSource) { public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource; this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource); this.jdbcTemplate = new JdbcTemplate(dataSource);
......
...@@ -51,7 +51,6 @@ public class SimpleHealthIndicatorTests { ...@@ -51,7 +51,6 @@ public class SimpleHealthIndicatorTests {
@Test @Test
public void database() { public void database() {
this.indicator.setDataSource(this.dataSource); this.indicator.setDataSource(this.dataSource);
this.indicator.setQuery("SELECT count(*) FROM INFORMATION_SCHEMA.SYSTEM_TABLES");
Map<String, Object> health = this.indicator.health(); Map<String, Object> health = this.indicator.health();
assertNotNull(health.get("database")); assertNotNull(health.get("database"));
assertNotNull(health.get("hello")); assertNotNull(health.get("hello"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment