Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
9483c91d
Commit
9483c91d
authored
Dec 03, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make sure to include validation query in case of failure
See gh-15055
parent
745ff883
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
22 deletions
+32
-22
DataSourceHealthIndicator.java
...ramework/boot/actuate/jdbc/DataSourceHealthIndicator.java
+10
-6
DataSourceHealthIndicatorTests.java
...ork/boot/actuate/jdbc/DataSourceHealthIndicatorTests.java
+22
-16
No files found.
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/jdbc/DataSourceHealthIndicator.java
View file @
9483c91d
...
...
@@ -110,12 +110,16 @@ public class DataSourceHealthIndicator extends AbstractHealthIndicator
builder
.
up
().
withDetail
(
"database"
,
product
);
String
validationQuery
=
getValidationQuery
(
product
);
if
(
StringUtils
.
hasText
(
validationQuery
))
{
// Avoid calling getObject as it breaks MySQL on Java 7
List
<
Object
>
results
=
this
.
jdbcTemplate
.
query
(
validationQuery
,
new
SingleColumnRowMapper
());
Object
result
=
DataAccessUtils
.
requiredSingleResult
(
results
);
builder
.
withDetail
(
"result"
,
result
);
builder
.
withDetail
(
"validationQuery"
,
validationQuery
);
try
{
// Avoid calling getObject as it breaks MySQL on Java 7
List
<
Object
>
results
=
this
.
jdbcTemplate
.
query
(
validationQuery
,
new
SingleColumnRowMapper
());
Object
result
=
DataAccessUtils
.
requiredSingleResult
(
results
);
builder
.
withDetail
(
"result"
,
result
);
}
finally
{
builder
.
withDetail
(
"validationQuery"
,
validationQuery
);
}
}
}
...
...
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/jdbc/DataSourceHealthIndicatorTests.java
View file @
9483c91d
...
...
@@ -32,6 +32,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
import
org.springframework.jdbc.datasource.SingleConnectionDataSource
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
entry
;
import
static
org
.
mockito
.
BDDMockito
.
given
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
times
;
...
...
@@ -41,6 +42,7 @@ import static org.mockito.Mockito.verify;
* Tests for {@link DataSourceHealthIndicator}.
*
* @author Dave Syer
* @author Stephane Nicoll
*/
public
class
DataSourceHealthIndicatorTests
{
...
...
@@ -64,41 +66,45 @@ public class DataSourceHealthIndicatorTests {
}
@Test
public
void
database
()
{
public
void
healthIndicatorWithDefaultSettings
()
{
this
.
indicator
.
setDataSource
(
this
.
dataSource
);
Health
health
=
this
.
indicator
.
health
();
assertThat
(
health
.
get
Details
().
get
(
"database"
)).
isNotNull
(
);
assertThat
(
health
.
getDetails
()
.
get
(
"result"
)).
isNotNull
();
assertThat
(
health
.
getDetails
().
get
(
"validationQuery"
))
.
isEqualTo
(
DatabaseDriver
.
HSQLDB
.
getValidationQuery
(
));
assertThat
(
health
.
get
Status
()).
isEqualTo
(
Status
.
UP
);
assertThat
(
health
.
getDetails
()
).
containsOnly
(
entry
(
"database"
,
"HSQL Database Engine"
),
entry
(
"result"
,
1L
),
entry
(
"validationQuery"
,
DatabaseDriver
.
HSQLDB
.
getValidationQuery
()
));
}
@Test
public
void
custom
Query
()
{
this
.
indicator
.
setDataSource
(
this
.
dataSource
)
;
public
void
healthIndicatorWithCustomValidation
Query
()
{
String
customValidationQuery
=
"SELECT COUNT(*) from FOO"
;
new
JdbcTemplate
(
this
.
dataSource
)
.
execute
(
"CREATE TABLE FOO (id INTEGER IDENTITY PRIMARY KEY)"
);
String
customValidationQuery
=
"SELECT COUNT(*) from FOO"
;
this
.
indicator
.
setDataSource
(
this
.
dataSource
)
;
this
.
indicator
.
setQuery
(
customValidationQuery
);
Health
health
=
this
.
indicator
.
health
();
assertThat
(
health
.
getDetails
().
get
(
"database"
)).
isNotNull
();
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
UP
);
assertThat
(
health
.
getDetails
()
.
get
(
"result"
)).
isNotNull
();
assertThat
(
health
.
getDetails
().
get
(
"validationQuery"
))
.
isEqualTo
(
customValidationQuery
);
assertThat
(
health
.
getDetails
()
).
containsOnly
(
entry
(
"database"
,
"HSQL Database Engine"
),
entry
(
"result"
,
0L
),
entry
(
"validationQuery"
,
customValidationQuery
)
);
}
@Test
public
void
error
()
{
public
void
healthIndicatorWithInvalidValidationQuery
()
{
String
invalidValidationQuery
=
"SELECT COUNT(*) from BAR"
;
this
.
indicator
.
setDataSource
(
this
.
dataSource
);
this
.
indicator
.
setQuery
(
"SELECT COUNT(*) from BAR"
);
this
.
indicator
.
setQuery
(
invalidValidationQuery
);
Health
health
=
this
.
indicator
.
health
();
assertThat
(
health
.
getDetails
().
get
(
"database"
)).
isNotNull
();
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
DOWN
);
assertThat
(
health
.
getDetails
()).
contains
(
entry
(
"database"
,
"HSQL Database Engine"
),
entry
(
"validationQuery"
,
invalidValidationQuery
));
assertThat
(
health
.
getDetails
()).
containsOnlyKeys
(
"database"
,
"error"
,
"validationQuery"
);
}
@Test
public
void
connectionClosed
()
throws
Exception
{
public
void
healthIndicatorCloseConnection
()
throws
Exception
{
DataSource
dataSource
=
mock
(
DataSource
.
class
);
Connection
connection
=
mock
(
Connection
.
class
);
given
(
connection
.
getMetaData
())
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment