|
17 | 17 | package org.springframework.boot.actuate.health;
|
18 | 18 |
|
19 | 19 | import java.sql.Connection;
|
| 20 | +import java.sql.ResultSet; |
| 21 | +import java.sql.ResultSetMetaData; |
20 | 22 | import java.sql.SQLException;
|
21 | 23 | import java.util.HashMap;
|
22 | 24 | import java.util.Map;
|
23 | 25 |
|
24 | 26 | import javax.sql.DataSource;
|
25 | 27 |
|
26 | 28 | import org.springframework.dao.DataAccessException;
|
| 29 | +import org.springframework.dao.support.DataAccessUtils; |
| 30 | +import org.springframework.jdbc.IncorrectResultSetColumnCountException; |
27 | 31 | import org.springframework.jdbc.core.ConnectionCallback;
|
28 | 32 | import org.springframework.jdbc.core.JdbcTemplate;
|
| 33 | +import org.springframework.jdbc.core.RowMapper; |
| 34 | +import org.springframework.jdbc.support.JdbcUtils; |
29 | 35 | import org.springframework.util.StringUtils;
|
30 | 36 |
|
31 | 37 | /**
|
|
34 | 40 | *
|
35 | 41 | * @author Dave Syer
|
36 | 42 | * @author Christian Dupuis
|
| 43 | + * @author Andy Wilkinson |
37 | 44 | * @since 1.1.0
|
38 | 45 | */
|
39 | 46 | public class DataSourceHealthIndicator extends AbstractHealthIndicator {
|
@@ -86,8 +93,23 @@ private void doDataSourceHealthCheck(Health.Builder builder) throws Exception {
|
86 | 93 | String query = detectQuery(product);
|
87 | 94 | if (StringUtils.hasText(query)) {
|
88 | 95 | 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 | + }))); |
91 | 113 | }
|
92 | 114 | catch (Exception ex) {
|
93 | 115 | builder.down(ex);
|
|
0 commit comments