Skip to content

Commit c3dc907

Browse files
committed
refactor: reduce health check duplication and fix SQLException
1 parent 98f92e1 commit c3dc907

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

src/main/java/com/iemr/admin/service/health/HealthService.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,23 @@ private Map<String, Object> checkMySQLHealth() {
122122
details.put("database", extractDatabaseName(dbUrl));
123123

124124
return performHealthCheck("MySQL", details, () -> {
125-
try (Connection connection = dataSource.getConnection()) {
126-
if (connection.isValid(2)) {
127-
try (PreparedStatement stmt = connection.prepareStatement(DB_HEALTH_CHECK_QUERY)) {
128-
stmt.setQueryTimeout(3);
129-
try (ResultSet rs = stmt.executeQuery()) {
130-
if (rs.next() && rs.getInt(1) == 1) {
131-
String version = getMySQLVersion(connection);
132-
return new HealthCheckResult(true, version, null);
125+
try {
126+
try (Connection connection = dataSource.getConnection()) {
127+
if (connection.isValid(2)) {
128+
try (PreparedStatement stmt = connection.prepareStatement(DB_HEALTH_CHECK_QUERY)) {
129+
stmt.setQueryTimeout(3);
130+
try (ResultSet rs = stmt.executeQuery()) {
131+
if (rs.next() && rs.getInt(1) == 1) {
132+
String version = getMySQLVersion(connection);
133+
return new HealthCheckResult(true, version, null);
134+
}
133135
}
134136
}
135137
}
138+
return new HealthCheckResult(false, null, "Connection validation failed");
136139
}
137-
return new HealthCheckResult(false, null, "Connection validation failed");
140+
} catch (Exception e) {
141+
throw new RuntimeException(e);
138142
}
139143
});
140144
}

0 commit comments

Comments
 (0)