Enable connection validation by default
Hikari and Commons DBCP2 are already validating that the connection is valid before borrowing it from the pool. This commit makes that behaviour consistent by enabling that feature for the Tomcat and Commons DBCP data sources. Since a validation query is required in those cases, the infrastructure of `DataSourceHealthIndicator` has been merged in a single place: the `DatabaseDriver` enum provides not only the driver class names but also the validation query, if any. Closes gh-4906
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 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.
|
||||
@@ -25,6 +25,7 @@ import java.util.List;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.boot.jdbc.DatabaseDriver;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.support.DataAccessUtils;
|
||||
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
|
||||
@@ -131,10 +132,8 @@ public class DataSourceHealthIndicator extends AbstractHealthIndicator
|
||||
protected String getValidationQuery(String product) {
|
||||
String query = this.query;
|
||||
if (!StringUtils.hasText(query)) {
|
||||
Product specific = Product.forProduct(product);
|
||||
if (specific != null) {
|
||||
query = specific.getQuery();
|
||||
}
|
||||
DatabaseDriver specific = DatabaseDriver.fromProductName(product);
|
||||
query = specific.getValidationQuery();
|
||||
}
|
||||
if (!StringUtils.hasText(query)) {
|
||||
query = DEFAULT_QUERY;
|
||||
@@ -185,74 +184,4 @@ public class DataSourceHealthIndicator extends AbstractHealthIndicator
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Known database products.
|
||||
*/
|
||||
protected enum Product {
|
||||
|
||||
HSQLDB("HSQL Database Engine",
|
||||
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_USERS"),
|
||||
|
||||
ORACLE("Oracle", "SELECT 'Hello' from DUAL"),
|
||||
|
||||
DERBY("Apache Derby", "SELECT 1 FROM SYSIBM.SYSDUMMY1"),
|
||||
|
||||
DB2("DB2", "SELECT 1 FROM SYSIBM.SYSDUMMY1") {
|
||||
|
||||
@Override
|
||||
protected boolean matchesProduct(String product) {
|
||||
return super.matchesProduct(product)
|
||||
|| product.toLowerCase().startsWith("db2/");
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
DB2_AS400("DB2 UDB for AS/400", "SELECT 1 FROM SYSIBM.SYSDUMMY1") {
|
||||
@Override
|
||||
protected boolean matchesProduct(String product) {
|
||||
return super.matchesProduct(product)
|
||||
|| product.toLowerCase().contains("as/400");
|
||||
}
|
||||
},
|
||||
|
||||
INFORMIX("Informix Dynamic Server", "select count(*) from systables"),
|
||||
|
||||
FIREBIRD("Firebird", "SELECT 1 FROM RDB$DATABASE") {
|
||||
|
||||
@Override
|
||||
protected boolean matchesProduct(String product) {
|
||||
return super.matchesProduct(product)
|
||||
|| product.toLowerCase().startsWith("firebird");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
private final String product;
|
||||
|
||||
private final String query;
|
||||
|
||||
Product(String product, String query) {
|
||||
this.product = product;
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
protected boolean matchesProduct(String product) {
|
||||
return this.product.equalsIgnoreCase(product);
|
||||
}
|
||||
|
||||
public String getQuery() {
|
||||
return this.query;
|
||||
}
|
||||
|
||||
public static Product forProduct(String product) {
|
||||
for (Product candidate : values()) {
|
||||
if (candidate.matchesProduct(product)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 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.
|
||||
@@ -24,7 +24,6 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.actuate.health.DataSourceHealthIndicator.Product;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
|
||||
@@ -104,20 +103,4 @@ public class DataSourceHealthIndicatorTests {
|
||||
verify(connection, times(2)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void productLookups() throws Exception {
|
||||
assertThat(Product.forProduct("newone")).isNull();
|
||||
assertThat(Product.forProduct("HSQL Database Engine")).isEqualTo(Product.HSQLDB);
|
||||
assertThat(Product.forProduct("Oracle")).isEqualTo(Product.ORACLE);
|
||||
assertThat(Product.forProduct("Apache Derby")).isEqualTo(Product.DERBY);
|
||||
assertThat(Product.forProduct("DB2")).isEqualTo(Product.DB2);
|
||||
assertThat(Product.forProduct("DB2/LINUXX8664")).isEqualTo(Product.DB2);
|
||||
assertThat(Product.forProduct("DB2 UDB for AS/400")).isEqualTo(Product.DB2_AS400);
|
||||
assertThat(Product.forProduct("DB3 XDB for AS/400")).isEqualTo(Product.DB2_AS400);
|
||||
assertThat(Product.forProduct("Informix Dynamic Server"))
|
||||
.isEqualTo(Product.INFORMIX);
|
||||
assertThat(Product.forProduct("Firebird 2.5.WI")).isEqualTo(Product.FIREBIRD);
|
||||
assertThat(Product.forProduct("Firebird 2.1.LI")).isEqualTo(Product.FIREBIRD);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user