@Test public void checkUnhealthyConnectionInvalid() throws Exception { Connection connectionMock = Mockito.mock(Connection.class); when(connectionMock.isValid(anyInt())).thenReturn(false); when(dataSource.getConnection()).thenReturn(connectionMock); assertThat(healthChecker.check(dataSource).isHealthy(), is(false)); }
public Result check(DataSource dataSource) { try (Connection connection = dataSource.getConnection()) { if (connection == null) { return Result.unhealthy("No database connection could be established"); } if (!connection.isValid(1)) { return Result.unhealthy("Connection is not valid"); } } catch (SQLException e) { return Result.unhealthy(e); } return Result.healthy(); }
DataSourceHealthChecker { public Result check(DataSource dataSource) { try (Connection connection = dataSource.getConnection()) { if (connection == null) { return Result.unhealthy("No database connection could be established"); } if (!connection.isValid(1)) { return Result.unhealthy("Connection is not valid"); } } catch (SQLException e) { return Result.unhealthy(e); } return Result.healthy(); } }
DataSourceHealthChecker { public Result check(DataSource dataSource) { try (Connection connection = dataSource.getConnection()) { if (connection == null) { return Result.unhealthy("No database connection could be established"); } if (!connection.isValid(1)) { return Result.unhealthy("Connection is not valid"); } } catch (SQLException e) { return Result.unhealthy(e); } return Result.healthy(); }  }
DataSourceHealthChecker { public Result check(DataSource dataSource) { try (Connection connection = dataSource.getConnection()) { if (connection == null) { return Result.unhealthy("No database connection could be established"); } if (!connection.isValid(1)) { return Result.unhealthy("Connection is not valid"); } } catch (SQLException e) { return Result.unhealthy(e); } return Result.healthy(); }  Result check(DataSource dataSource); }
DataSourceHealthChecker { public Result check(DataSource dataSource) { try (Connection connection = dataSource.getConnection()) { if (connection == null) { return Result.unhealthy("No database connection could be established"); } if (!connection.isValid(1)) { return Result.unhealthy("Connection is not valid"); } } catch (SQLException e) { return Result.unhealthy(e); } return Result.healthy(); }  Result check(DataSource dataSource);  }