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
063403a0
Commit
063403a0
authored
Dec 23, 2013
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Close connection properly in /health
Fixes gh-181
parent
22762f20
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
3 deletions
+86
-3
SimpleHealthIndicator.java
...gframework/boot/actuate/health/SimpleHealthIndicator.java
+13
-3
SimpleHealthIndicatorTests.java
...ework/boot/actuate/health/SimpleHealthIndicatorTests.java
+73
-0
No files found.
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SimpleHealthIndicator.java
View file @
063403a0
...
...
@@ -16,12 +16,15 @@
package
org
.
springframework
.
boot
.
actuate
.
health
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
javax.sql.DataSource
;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.jdbc.core.ConnectionCallback
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.util.StringUtils
;
...
...
@@ -42,10 +45,17 @@ public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object
map
.
put
(
"status"
,
"ok"
);
if
(
this
.
dataSource
!=
null
)
{
try
{
map
.
put
(
"database"
,
this
.
dataSource
.
getConnection
().
getMetaData
()
.
getDatabaseProductName
());
String
product
=
this
.
jdbcTemplate
.
execute
(
new
ConnectionCallback
<
String
>()
{
@Override
public
String
doInConnection
(
Connection
connection
)
throws
SQLException
,
DataAccessException
{
return
connection
.
getMetaData
().
getDatabaseProductName
();
}
});
map
.
put
(
"database"
,
product
);
}
catch
(
SQL
Exception
ex
)
{
catch
(
DataAccess
Exception
ex
)
{
map
.
put
(
"status"
,
"error"
);
map
.
put
(
"error"
,
ex
.
getClass
().
getName
()
+
": "
+
ex
.
getMessage
());
}
...
...
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/SimpleHealthIndicatorTests.java
0 → 100644
View file @
063403a0
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
actuate
.
health
;
import
java.sql.Connection
;
import
java.util.Map
;
import
javax.sql.DataSource
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection
;
import
org.springframework.jdbc.datasource.DriverManagerDataSource
;
import
org.springframework.jdbc.datasource.SingleConnectionDataSource
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
/**
* @author Dave Syer
*/
public
class
SimpleHealthIndicatorTests
{
private
SimpleHealthIndicator
indicator
=
new
SimpleHealthIndicator
();
private
DriverManagerDataSource
dataSource
;
@Before
public
void
init
()
{
EmbeddedDatabaseConnection
db
=
EmbeddedDatabaseConnection
.
HSQL
;
this
.
dataSource
=
new
SingleConnectionDataSource
(
db
.
getUrl
(),
"sa"
,
""
,
false
);
this
.
dataSource
.
setDriverClassName
(
db
.
getDriverClassName
());
}
@Test
public
void
database
()
{
this
.
indicator
.
setDataSource
(
this
.
dataSource
);
this
.
indicator
.
setQuery
(
"SELECT count(*) FROM INFORMATION_SCHEMA.SYSTEM_TABLES"
);
Map
<
String
,
Object
>
health
=
this
.
indicator
.
health
();
assertNotNull
(
health
.
get
(
"database"
));
assertNotNull
(
health
.
get
(
"hello"
));
}
@Test
public
void
connectionClosed
()
throws
Exception
{
DataSource
dataSource
=
mock
(
DataSource
.
class
);
Connection
connection
=
mock
(
Connection
.
class
);
when
(
connection
.
getMetaData
()).
thenReturn
(
this
.
dataSource
.
getConnection
().
getMetaData
());
when
(
dataSource
.
getConnection
()).
thenReturn
(
connection
);
this
.
indicator
.
setDataSource
(
dataSource
);
Map
<
String
,
Object
>
health
=
this
.
indicator
.
health
();
assertNotNull
(
health
.
get
(
"database"
));
verify
(
connection
,
times
(
2
)).
close
();
}
}
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