Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import javax.sql.DataSource;
Expand Down Expand Up @@ -64,12 +65,15 @@
public class DataSourceHealthContributorAutoConfiguration extends
CompositeHealthContributorConfiguration<AbstractHealthIndicator, DataSource> implements InitializingBean {

private Map<String, DataSource> dataSources;

private final Collection<DataSourcePoolMetadataProvider> metadataProviders;

private DataSourcePoolMetadataProvider poolMetadataProvider;

public DataSourceHealthContributorAutoConfiguration(Map<String, DataSource> dataSources,
ObjectProvider<DataSourcePoolMetadataProvider> metadataProviders) {
this.dataSources = dataSources;
this.metadataProviders = metadataProviders.orderedStream().collect(Collectors.toList());
}

Expand All @@ -89,7 +93,12 @@ protected AbstractHealthIndicator createIndicator(DataSource source) {
if (source instanceof AbstractRoutingDataSource) {
return new RoutingDataSourceHealthIndicator();
}
return new DataSourceHealthIndicator(source, getValidationQuery(source));
String name = this.dataSources.entrySet().stream()
.filter(entry -> entry.getValue().equals(source))
.findAny()
.map(Entry::getKey)
.orElse(null);
return new DataSourceHealthIndicator(name, source, getValidationQuery(source));
}

private String getValidationQuery(DataSource source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,18 @@ public DataSourceHealthIndicator(DataSource dataSource) {
* @param query the validation query to use (can be {@code null})
*/
public DataSourceHealthIndicator(DataSource dataSource, String query) {
super("DataSource health check failed");
this(null, dataSource, query);
}

/**
* Create a new {@link DataSourceHealthIndicator} using the specified
* {@link DataSource} and validation query.
* @param name the name of the data source
* @param dataSource the data source
* @param query the validation query to use (can be {@code null})
*/
public DataSourceHealthIndicator(String name, DataSource dataSource, String query) {
super(ex -> "DataSource " + name + " health check failed : " + ex.getMessage());
this.dataSource = dataSource;
this.query = query;
this.jdbcTemplate = (dataSource != null) ? new JdbcTemplate(dataSource) : null;
Expand Down