Skip to content

Commit 56ce2b8

Browse files
committed
Polish "Add metrics support for idle jdbc connections"
See gh-17504
1 parent 9acc02b commit 56ce2b8

File tree

10 files changed

+27
-60
lines changed

10 files changed

+27
-60
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/jdbc/DataSourcePoolMetrics.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
*
3939
* @author Jon Schneider
4040
* @author Phillip Webb
41-
* @author Artsiom Yudovin
4241
* @since 2.0.0
4342
*/
4443
public class DataSourcePoolMetrics implements MeterBinder {
@@ -67,9 +66,9 @@ public DataSourcePoolMetrics(DataSource dataSource, DataSourcePoolMetadataProvid
6766
public void bindTo(MeterRegistry registry) {
6867
if (this.metadataProvider.getDataSourcePoolMetadata(this.dataSource) != null) {
6968
bindPoolMetadata(registry, "active", DataSourcePoolMetadata::getActive);
69+
bindPoolMetadata(registry, "idle", DataSourcePoolMetadata::getIdle);
7070
bindPoolMetadata(registry, "max", DataSourcePoolMetadata::getMax);
7171
bindPoolMetadata(registry, "min", DataSourcePoolMetadata::getMin);
72-
bindPoolMetadata(registry, "idle", DataSourcePoolMetadata::getIdle);
7372
}
7473
}
7574

spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,8 +2023,8 @@ is required. A `CacheMetricsRegistrar` bean is made available to make that proce
20232023
==== DataSource Metrics
20242024
Auto-configuration enables the instrumentation of all available `DataSource` objects with
20252025
a metric named `jdbc`. Data source instrumentation results in gauges representing the
2026-
currently active, maximum allowed, and minimum allowed connections in the pool. Each of
2027-
these gauges has a name that is prefixed by `jdbc`.
2026+
currently active, idle, maximum allowed, and minimum allowed connections in the pool. Each
2027+
of these gauges has a name that is prefixed by `jdbc`.
20282028

20292029
Metrics are also tagged by the name of the `DataSource` computed based on the bean name.
20302030

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/metadata/CommonsDbcp2DataSourcePoolMetadata.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
* {@link DataSourcePoolMetadata} for an Apache Commons DBCP2 {@link DataSource}.
2525
*
2626
* @author Stephane Nicoll
27-
* @author Artsiom Yudovin
2827
* @since 2.0.0
2928
*/
3029
public class CommonsDbcp2DataSourcePoolMetadata extends AbstractDataSourcePoolMetadata<BasicDataSource> {
@@ -38,6 +37,11 @@ public Integer getActive() {
3837
return getDataSource().getNumActive();
3938
}
4039

40+
@Override
41+
public Integer getIdle() {
42+
return getDataSource().getNumIdle();
43+
}
44+
4145
@Override
4246
public Integer getMax() {
4347
return getDataSource().getMaxTotal();
@@ -58,9 +62,4 @@ public Boolean getDefaultAutoCommit() {
5862
return getDataSource().getDefaultAutoCommit();
5963
}
6064

61-
@Override
62-
public Integer getIdle() {
63-
return getDataSource().getNumIdle();
64-
}
65-
6665
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/metadata/DataSourcePoolMetadata.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,6 +55,7 @@ public interface DataSourcePoolMetadata {
5555
* if that information is not available.
5656
* @return the number of established but idle connections or {@code null}
5757
* @since 2.2.0
58+
* @see #getActive()
5859
*/
5960
default Integer getIdle() {
6061
return null;

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/metadata/HikariDataSourcePoolMetadata.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.jdbc.metadata;
1818

19-
import java.util.Objects;
20-
2119
import javax.sql.DataSource;
2220

2321
import com.zaxxer.hikari.HikariDataSource;
@@ -29,7 +27,6 @@
2927
* {@link DataSourcePoolMetadata} for a Hikari {@link DataSource}.
3028
*
3129
* @author Stephane Nicoll
32-
* @author Artsiom Yudovin
3330
* @since 2.0.0
3431
*/
3532
public class HikariDataSourcePoolMetadata extends AbstractDataSourcePoolMetadata<HikariDataSource> {
@@ -50,12 +47,12 @@ public Integer getActive() {
5047

5148
@Override
5249
public Integer getIdle() {
53-
HikariPool pool = getHikariPool();
54-
if (Objects.nonNull(pool)) {
55-
return pool.getIdleConnections();
50+
try {
51+
return getHikariPool().getIdleConnections();
52+
}
53+
catch (Exception ex) {
54+
return null;
5655
}
57-
58-
return null;
5956
}
6057

6158
private HikariPool getHikariPool() {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/metadata/TomcatDataSourcePoolMetadata.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
* {@link DataSourcePoolMetadata} for a Tomcat DataSource.
2424
*
2525
* @author Stephane Nicoll
26-
* @author Artsiom Yudovin
2726
* @since 2.0.0
2827
*/
2928
public class TomcatDataSourcePoolMetadata extends AbstractDataSourcePoolMetadata<DataSource> {
@@ -38,6 +37,11 @@ public Integer getActive() {
3837
return (pool != null) ? pool.getActive() : 0;
3938
}
4039

40+
@Override
41+
public Integer getIdle() {
42+
return getDataSource().getNumIdle();
43+
}
44+
4145
@Override
4246
public Integer getMax() {
4347
return getDataSource().getMaxActive();
@@ -58,9 +62,4 @@ public Boolean getDefaultAutoCommit() {
5862
return getDataSource().isDefaultAutoCommit();
5963
}
6064

61-
@Override
62-
public Integer getIdle() {
63-
return getDataSource().getNumIdle();
64-
}
65-
6665
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/metadata/AbstractDataSourcePoolMetadataTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
abstract class AbstractDataSourcePoolMetadataTests<D extends AbstractDataSourcePoolMetadata<?>> {
3535

3636
/**
37-
* Return a data source metadata instance with a min size of 0 and max size of 2.
37+
* Return a data source metadata instance with a min size of 0 and max size of 2. Idle
38+
* connections are not reclaimed immediately.
3839
* @return the data source metadata
3940
*/
4041
protected abstract D getDataSourceMetadata();
@@ -70,6 +71,8 @@ void getPoolSizeOneConnection() {
7071

7172
@Test
7273
void getIdle() {
74+
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
75+
jdbcTemplate.execute((ConnectionCallback<Void>) (connection) -> null);
7376
assertThat(getDataSourceMetadata().getIdle()).isEqualTo(Integer.valueOf(1));
7477
}
7578

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/metadata/CommonsDbcp2DataSourcePoolMetadataTests.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
import org.apache.commons.dbcp2.BasicDataSource;
2020
import org.junit.jupiter.api.Test;
2121

22-
import org.springframework.jdbc.core.ConnectionCallback;
23-
import org.springframework.jdbc.core.JdbcTemplate;
24-
2522
import static org.assertj.core.api.Assertions.assertThat;
2623

2724
/**
@@ -86,19 +83,12 @@ private CommonsDbcp2DataSourcePoolMetadata createDataSourceMetadata(int minSize,
8683
BasicDataSource dataSource = createDataSource();
8784
dataSource.setMinIdle(minSize);
8885
dataSource.setMaxTotal(maxSize);
89-
90-
this.initPool(dataSource);
91-
86+
dataSource.setMinEvictableIdleTimeMillis(5000);
9287
return new CommonsDbcp2DataSourcePoolMetadata(dataSource);
9388
}
9489

9590
private BasicDataSource createDataSource() {
9691
return initializeBuilder().type(BasicDataSource.class).build();
9792
}
9893

99-
private void initPool(BasicDataSource dataSource) {
100-
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
101-
jdbcTemplate.execute((ConnectionCallback<Void>) (connection) -> null);
102-
}
103-
10494
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/metadata/HikariDataSourcePoolMetadataTests.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@
1818

1919
import com.zaxxer.hikari.HikariDataSource;
2020

21-
import org.springframework.jdbc.core.ConnectionCallback;
22-
import org.springframework.jdbc.core.JdbcTemplate;
23-
2421
import static org.assertj.core.api.Assertions.assertThat;
2522

2623
/**
2724
* Tests for {@link HikariDataSourcePoolMetadata}.
2825
*
2926
* @author Stephane Nicoll
30-
* @author Artsiom Yudovin
3127
*/
3228
public class HikariDataSourcePoolMetadataTests
3329
extends AbstractDataSourcePoolMetadataTests<HikariDataSourcePoolMetadata> {
@@ -58,15 +54,8 @@ private HikariDataSource createDataSource(int minSize, int maxSize) {
5854
HikariDataSource dataSource = initializeBuilder().type(HikariDataSource.class).build();
5955
dataSource.setMinimumIdle(minSize);
6056
dataSource.setMaximumPoolSize(maxSize);
61-
62-
this.initPool(dataSource);
63-
57+
dataSource.setIdleTimeout(5000);
6458
return dataSource;
6559
}
6660

67-
private void initPool(HikariDataSource dataSource) {
68-
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
69-
jdbcTemplate.execute((ConnectionCallback<Void>) (connection) -> null);
70-
}
71-
7261
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/metadata/TomcatDataSourcePoolMetadataTests.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
import org.apache.tomcat.jdbc.pool.DataSource;
2020

21-
import org.springframework.jdbc.core.ConnectionCallback;
22-
import org.springframework.jdbc.core.JdbcTemplate;
23-
2421
import static org.assertj.core.api.Assertions.assertThat;
2522

2623
/**
@@ -57,19 +54,12 @@ private DataSource createDataSource(int minSize, int maxSize) {
5754
DataSource dataSource = initializeBuilder().type(DataSource.class).build();
5855
dataSource.setMinIdle(minSize);
5956
dataSource.setMaxActive(maxSize);
57+
dataSource.setMinEvictableIdleTimeMillis(5000);
6058

6159
// Avoid warnings
6260
dataSource.setInitialSize(minSize);
6361
dataSource.setMaxIdle(maxSize);
64-
65-
this.initPool(dataSource);
66-
6762
return dataSource;
6863
}
6964

70-
private void initPool(DataSource dataSource) {
71-
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
72-
jdbcTemplate.execute((ConnectionCallback<Void>) (connection) -> null);
73-
}
74-
7565
}

0 commit comments

Comments
 (0)