Skip to content

Commit 333bc3e

Browse files
committed
Avoid RS.getObject(index, Object.class) as it breaks MySQL on Java 7
Previously, when running on Java 7+, the JDBC query driven by DataSourceHealthIndicator resulted in a call to ResultSet.getObject(index, Object.class). When using MySQL's JDBC driver this failed with an SQLException with the message "Conversion not supported for type java.lang.Object". The problem does not occur on Java 6 as the overload of getObject that takes a type does not exist; ResultSet.getObject(index) is called instead and MySQL happily returns whatever type it deems to be appropriate for the column. This commit updates DataSourceHealthIndicator so that ResultSet.getObject(index) will always be used, irrespective of the version of Java that Boot is running on. Closes spring-projects#1306
1 parent 86d47f6 commit 333bc3e

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)