Skip to content

Commit 89c73bc

Browse files
committed
Hacking
See gh-17582
1 parent 334f3e0 commit 89c73bc

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.jdbc;
18+
19+
import java.sql.Connection;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
24+
import org.springframework.boot.actuate.health.Health.Builder;
25+
import org.springframework.boot.actuate.health.HealthIndicator;
26+
import org.springframework.boot.actuate.health.Status;
27+
28+
/**
29+
* {@link HealthIndicator} that tests the status of a {@link DataSource} using
30+
* {@link Connection#isValid(int) Connection#isValid}.
31+
*
32+
* @author Stephane Nicoll
33+
* @since 2.3.0
34+
*/
35+
public class QueryLessDatasourceHealthIndicator extends AbstractHealthIndicator {
36+
37+
private final DataSource dataSource;
38+
39+
/**
40+
* Create a new instance with the {@link DataSource} to monitor.
41+
* @param dataSource the data source to monitor
42+
*/
43+
public QueryLessDatasourceHealthIndicator(DataSource dataSource) {
44+
this.dataSource = dataSource;
45+
}
46+
47+
@Override
48+
protected void doHealthCheck(Builder builder) throws Exception {
49+
try (Connection connection = this.dataSource.getConnection()) {
50+
boolean valid = connection.isValid(0);
51+
builder.status((valid) ? Status.UP : Status.DOWN);
52+
}
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.jdbc;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.actuate.health.Health;
22+
import org.springframework.boot.actuate.health.Status;
23+
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
24+
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link QueryLessDatasourceHealthIndicator}.
30+
*
31+
* @author Stephane Nicoll
32+
*/
33+
class QueryLessDatasourceHealthIndicatorTests {
34+
35+
@Test
36+
void healthIndicatorWithDefaultSettings() {
37+
EmbeddedDatabaseConnection db = EmbeddedDatabaseConnection.HSQL;
38+
SingleConnectionDataSource dataSource = new SingleConnectionDataSource(db.getUrl("testdb") + ";shutdown=true",
39+
"sa", "", false);
40+
try {
41+
dataSource.setDriverClassName(db.getDriverClassName());
42+
QueryLessDatasourceHealthIndicator indicator = new QueryLessDatasourceHealthIndicator(dataSource);
43+
Health health = indicator.health();
44+
assertThat(health.getStatus()).isEqualTo(Status.UP);
45+
assertThat(health.getDetails()).isEmpty();
46+
}
47+
finally {
48+
dataSource.destroy();
49+
}
50+
}
51+
52+
}

0 commit comments

Comments
 (0)