Skip to content

Commit 2abf3eb

Browse files
committed
Merge branch '1.1.x'
2 parents 6c718c2 + 333bc3e commit 2abf3eb

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DataSourceHealthIndicator.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@
1717
package org.springframework.boot.actuate.health;
1818

1919
import java.sql.Connection;
20+
import java.sql.ResultSet;
21+
import java.sql.ResultSetMetaData;
2022
import java.sql.SQLException;
2123
import java.util.HashMap;
2224
import java.util.Map;
2325

2426
import javax.sql.DataSource;
2527

2628
import org.springframework.dao.DataAccessException;
29+
import org.springframework.dao.support.DataAccessUtils;
30+
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
2731
import org.springframework.jdbc.core.ConnectionCallback;
2832
import org.springframework.jdbc.core.JdbcTemplate;
33+
import org.springframework.jdbc.core.RowMapper;
34+
import org.springframework.jdbc.support.JdbcUtils;
2935
import org.springframework.util.StringUtils;
3036

3137
/**
@@ -34,6 +40,7 @@
3440
*
3541
* @author Dave Syer
3642
* @author Christian Dupuis
43+
* @author Andy Wilkinson
3744
* @since 1.1.0
3845
*/
3946
public class DataSourceHealthIndicator extends AbstractHealthIndicator {
@@ -86,8 +93,23 @@ private void doDataSourceHealthCheck(Health.Builder builder) throws Exception {
8693
String query = detectQuery(product);
8794
if (StringUtils.hasText(query)) {
8895
try {
89-
builder.withDetail("hello",
90-
this.jdbcTemplate.queryForObject(query, Object.class));
96+
builder.withDetail("hello", DataAccessUtils
97+
.requiredSingleResult(this.jdbcTemplate.query(query,
98+
new RowMapper<Object>() {
99+
100+
@Override
101+
public Object mapRow(ResultSet rs, int rowNum)
102+
throws SQLException {
103+
ResultSetMetaData rsmd = rs.getMetaData();
104+
int nrOfColumns = rsmd.getColumnCount();
105+
if (nrOfColumns != 1) {
106+
throw new IncorrectResultSetColumnCountException(
107+
1, nrOfColumns);
108+
}
109+
return JdbcUtils.getResultSetValue(rs, 1);
110+
}
111+
112+
})));
91113
}
92114
catch (Exception ex) {
93115
builder.down(ex);

0 commit comments

Comments
 (0)