diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SimpleHealthIndicator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SimpleHealthIndicator.java index 5313c36e94..6f21fda8bc 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SimpleHealthIndicator.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SimpleHealthIndicator.java @@ -18,6 +18,7 @@ package org.springframework.boot.actuate.health; import java.sql.Connection; import java.sql.SQLException; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -37,32 +38,44 @@ public class SimpleHealthIndicator implements HealthIndicator queries = new HashMap(); + + 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 public Map health() { LinkedHashMap map = new LinkedHashMap(); map.put("status", "ok"); + String product = "unknown"; if (this.dataSource != null) { try { - String product = this.jdbcTemplate - .execute(new ConnectionCallback() { - @Override - public String doInConnection(Connection connection) - throws SQLException, DataAccessException { - return connection.getMetaData().getDatabaseProductName(); - } - }); + product = this.jdbcTemplate.execute(new ConnectionCallback() { + @Override + public String doInConnection(Connection connection) + throws SQLException, DataAccessException { + return connection.getMetaData().getDatabaseProductName(); + } + }); map.put("database", product); } catch (DataAccessException ex) { map.put("status", "error"); map.put("error", ex.getClass().getName() + ": " + ex.getMessage()); } - if (StringUtils.hasText(this.query)) { + String query = detectQuery(product); + if (StringUtils.hasText(query)) { try { map.put("hello", - this.jdbcTemplate.queryForObject(this.query, String.class)); + this.jdbcTemplate.queryForObject(query, String.class)); } catch (Exception ex) { map.put("status", "error"); @@ -73,6 +86,17 @@ public class SimpleHealthIndicator implements HealthIndicator health = this.indicator.health(); assertNotNull(health.get("database")); assertNotNull(health.get("hello"));