Browse Source
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-4906pull/5126/merge
10 changed files with 339 additions and 272 deletions
@ -1,163 +0,0 @@
@@ -1,163 +0,0 @@
|
||||
/* |
||||
* 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. |
||||
* 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.autoconfigure.jdbc; |
||||
|
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
/** |
||||
* Enumeration of common database drivers. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Maciej Walkowiak |
||||
* @author Marten Deinum |
||||
* @since 1.2.0 |
||||
*/ |
||||
enum DatabaseDriver { |
||||
|
||||
/** |
||||
* Unknown type. |
||||
*/ |
||||
UNKNOWN(null), |
||||
|
||||
/** |
||||
* Apache Derby. |
||||
*/ |
||||
DERBY("org.apache.derby.jdbc.EmbeddedDriver"), |
||||
|
||||
/** |
||||
* H2. |
||||
*/ |
||||
H2("org.h2.Driver", "org.h2.jdbcx.JdbcDataSource"), |
||||
|
||||
/** |
||||
* HyperSQL DataBase. |
||||
*/ |
||||
HSQLDB("org.hsqldb.jdbc.JDBCDriver", "org.hsqldb.jdbc.pool.JDBCXADataSource"), |
||||
|
||||
/** |
||||
* SQL Lite. |
||||
*/ |
||||
SQLITE("org.sqlite.JDBC"), |
||||
|
||||
/** |
||||
* MySQL. |
||||
*/ |
||||
MYSQL("com.mysql.jdbc.Driver", "com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"), |
||||
|
||||
/** |
||||
* Maria DB. |
||||
*/ |
||||
MARIADB("org.mariadb.jdbc.Driver", "org.mariadb.jdbc.MariaDbDataSource"), |
||||
|
||||
/** |
||||
* Google App Engine. |
||||
*/ |
||||
GOOGLE("com.google.appengine.api.rdbms.AppEngineDriver"), |
||||
|
||||
/** |
||||
* Oracle. |
||||
*/ |
||||
ORACLE("oracle.jdbc.OracleDriver", "oracle.jdbc.xa.client.OracleXADataSource"), |
||||
|
||||
/** |
||||
* Postgres. |
||||
*/ |
||||
POSTGRESQL("org.postgresql.Driver", "org.postgresql.xa.PGXADataSource"), |
||||
|
||||
/** |
||||
* jTDS. |
||||
*/ |
||||
JTDS("net.sourceforge.jtds.jdbc.Driver"), |
||||
|
||||
/** |
||||
* SQL Server. |
||||
*/ |
||||
SQLSERVER("com.microsoft.sqlserver.jdbc.SQLServerDriver", |
||||
"com.microsoft.sqlserver.jdbc.SQLServerXADataSource"), |
||||
|
||||
/** |
||||
* Firebird. |
||||
*/ |
||||
FIREBIRD("org.firebirdsql.jdbc.FBDriver", |
||||
"org.firebirdsql.pool.FBConnectionPoolDataSource"), |
||||
|
||||
/** |
||||
* DB2 Server. |
||||
*/ |
||||
DB2("com.ibm.db2.jcc.DB2Driver", "com.ibm.db2.jcc.DB2XADataSource"), |
||||
|
||||
/** |
||||
* DB2 AS400 Server. |
||||
*/ |
||||
AS400("com.ibm.as400.access.AS400JDBCDriver", |
||||
"com.ibm.as400.access.AS400JDBCXADataSource"), |
||||
|
||||
/** |
||||
* Teradata. |
||||
*/ |
||||
TERADATA("com.teradata.jdbc.TeraDriver"); |
||||
|
||||
private final String driverClassName; |
||||
|
||||
private final String xaDataSourceClassName; |
||||
|
||||
DatabaseDriver(String driverClassName) { |
||||
this(driverClassName, null); |
||||
} |
||||
|
||||
DatabaseDriver(String driverClassName, String xaDataSourceClassName) { |
||||
this.driverClassName = driverClassName; |
||||
this.xaDataSourceClassName = xaDataSourceClassName; |
||||
} |
||||
|
||||
/** |
||||
* Return the driver class name. |
||||
* @return the class name or {@code null} |
||||
*/ |
||||
public String getDriverClassName() { |
||||
return this.driverClassName; |
||||
} |
||||
|
||||
/** |
||||
* Return the XA driver source class name. |
||||
* @return the class name or {@code null} |
||||
*/ |
||||
public String getXaDataSourceClassName() { |
||||
return this.xaDataSourceClassName; |
||||
} |
||||
|
||||
/** |
||||
* Find a {@link DatabaseDriver} for the given URL. |
||||
* @param url JDBC URL |
||||
* @return driver class name or {@link #UNKNOWN} if not found |
||||
*/ |
||||
public static DatabaseDriver fromJdbcUrl(String url) { |
||||
if (StringUtils.hasLength(url)) { |
||||
Assert.isTrue(url.startsWith("jdbc"), "URL must start with 'jdbc'"); |
||||
String urlWithoutPrefix = url.substring("jdbc".length()).toLowerCase(); |
||||
for (DatabaseDriver driver : values()) { |
||||
String prefix = ":" + driver.name().toLowerCase() + ":"; |
||||
if (driver != UNKNOWN && urlWithoutPrefix.startsWith(prefix)) { |
||||
return driver; |
||||
} |
||||
} |
||||
} |
||||
return UNKNOWN; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,241 @@
@@ -0,0 +1,241 @@
|
||||
/* |
||||
* 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. |
||||
* 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.jdbc; |
||||
|
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
/** |
||||
* Enumeration of common database drivers. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Maciej Walkowiak |
||||
* @author Marten Deinum |
||||
* @author Stephane Nicoll |
||||
* @since 1.2.0 |
||||
*/ |
||||
public enum DatabaseDriver { |
||||
|
||||
/** |
||||
* Unknown type. |
||||
*/ |
||||
UNKNOWN(null, null), |
||||
|
||||
/** |
||||
* Apache Derby. |
||||
*/ |
||||
DERBY("Apache Derby", "org.apache.derby.jdbc.EmbeddedDriver", null, |
||||
"SELECT 1 FROM SYSIBM.SYSDUMMY1"), |
||||
|
||||
/** |
||||
* H2. |
||||
*/ |
||||
H2("H2", "org.h2.Driver", "org.h2.jdbcx.JdbcDataSource", "SELECT 1"), |
||||
|
||||
/** |
||||
* HyperSQL DataBase. |
||||
*/ |
||||
HSQLDB("HSQL Database Engine", "org.hsqldb.jdbc.JDBCDriver", |
||||
"org.hsqldb.jdbc.pool.JDBCXADataSource", |
||||
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_USERS"), |
||||
|
||||
/** |
||||
* SQL Lite. |
||||
*/ |
||||
SQLITE("SQLite", "org.sqlite.JDBC"), |
||||
|
||||
/** |
||||
* MySQL. |
||||
*/ |
||||
MYSQL("MySQL", "com.mysql.jdbc.Driver", |
||||
"com.mysql.jdbc.jdbc2.optional.MysqlXADataSource", "SELECT 1"), |
||||
|
||||
/** |
||||
* Maria DB. |
||||
*/ |
||||
MARIADB("MySQL", "org.mariadb.jdbc.Driver", "org.mariadb.jdbc.MariaDbDataSource", |
||||
"SELECT 1"), |
||||
|
||||
/** |
||||
* Google App Engine. |
||||
*/ |
||||
GAE(null, "com.google.appengine.api.rdbms.AppEngineDriver"), |
||||
|
||||
/** |
||||
* Oracle. |
||||
*/ |
||||
ORACLE("Oracle", "oracle.jdbc.OracleDriver", |
||||
"oracle.jdbc.xa.client.OracleXADataSource", "SELECT 'Hello' from DUAL"), |
||||
|
||||
/** |
||||
* Postgres. |
||||
*/ |
||||
POSTGRESQL("PostgreSQL", "org.postgresql.Driver", "org.postgresql.xa.PGXADataSource", |
||||
"SELECT 1"), |
||||
|
||||
/** |
||||
* jTDS. As it can be used for several databases, there isn't a single product name |
||||
* we could rely on. |
||||
*/ |
||||
JTDS(null, "net.sourceforge.jtds.jdbc.Driver"), |
||||
|
||||
/** |
||||
* SQL Server. |
||||
*/ |
||||
SQLSERVER("SQL SERVER", "com.microsoft.sqlserver.jdbc.SQLServerDriver", |
||||
"com.microsoft.sqlserver.jdbc.SQLServerXADataSource", "SELECT 1"), |
||||
|
||||
/** |
||||
* Firebird. |
||||
*/ |
||||
FIREBIRD("Firebird", "org.firebirdsql.jdbc.FBDriver", |
||||
"org.firebirdsql.pool.FBConnectionPoolDataSource", |
||||
"SELECT 1 FROM RDB$DATABASE") { |
||||
|
||||
@Override |
||||
protected boolean matchProductName(String productName) { |
||||
return super.matchProductName(productName) |
||||
|| productName.toLowerCase().startsWith("firebird"); |
||||
} |
||||
}, |
||||
|
||||
/** |
||||
* DB2 Server. |
||||
*/ |
||||
DB2("DB2", "com.ibm.db2.jcc.DB2Driver", "com.ibm.db2.jcc.DB2XADataSource", |
||||
"SELECT 1 FROM SYSIBM.SYSDUMMY1") { |
||||
|
||||
@Override |
||||
protected boolean matchProductName(String productName) { |
||||
return super.matchProductName(productName) |
||||
|| productName.toLowerCase().startsWith("db2/"); |
||||
} |
||||
}, |
||||
|
||||
/** |
||||
* DB2 AS400 Server. |
||||
*/ |
||||
DB2_AS400("DB2 UDB for AS/400", "com.ibm.as400.access.AS400JDBCDriver", |
||||
"com.ibm.as400.access.AS400JDBCXADataSource", |
||||
"SELECT 1 FROM SYSIBM.SYSDUMMY1") { |
||||
|
||||
@Override |
||||
protected boolean matchProductName(String productName) { |
||||
return super.matchProductName(productName) |
||||
|| productName.toLowerCase().contains("as/400"); |
||||
} |
||||
}, |
||||
|
||||
/** |
||||
* Teradata. |
||||
*/ |
||||
TERADATA("Teradata", "com.teradata.jdbc.TeraDriver"), |
||||
|
||||
/** |
||||
* Informix. |
||||
*/ |
||||
INFORMIX("Informix Dynamic Server", "com.informix.jdbc.IfxDriver", null, |
||||
"select count(*) from systables"); |
||||
|
||||
private final String productName; |
||||
|
||||
private final String driverClassName; |
||||
|
||||
private final String xaDataSourceClassName; |
||||
|
||||
private final String validationQuery; |
||||
|
||||
DatabaseDriver(String name, String driverClassName) { |
||||
this(name, driverClassName, null); |
||||
} |
||||
|
||||
DatabaseDriver(String name, String driverClassName, String xaDataSourceClassName) { |
||||
this(name, driverClassName, xaDataSourceClassName, null); |
||||
} |
||||
|
||||
DatabaseDriver(String productName, String driverClassName, |
||||
String xaDataSourceClassName, String validationQuery) { |
||||
this.productName = productName; |
||||
this.driverClassName = driverClassName; |
||||
this.xaDataSourceClassName = xaDataSourceClassName; |
||||
this.validationQuery = validationQuery; |
||||
} |
||||
|
||||
protected boolean matchProductName(String productName) { |
||||
return this.productName != null && this.productName.equalsIgnoreCase(productName); |
||||
} |
||||
|
||||
/** |
||||
* Return the driver class name. |
||||
* @return the class name or {@code null} |
||||
*/ |
||||
public String getDriverClassName() { |
||||
return this.driverClassName; |
||||
} |
||||
|
||||
/** |
||||
* Return the XA driver source class name. |
||||
* @return the class name or {@code null} |
||||
*/ |
||||
public String getXaDataSourceClassName() { |
||||
return this.xaDataSourceClassName; |
||||
} |
||||
|
||||
/** |
||||
* Return the validation query. |
||||
* @return the validation query or {@code null} |
||||
*/ |
||||
public String getValidationQuery() { |
||||
return this.validationQuery; |
||||
} |
||||
|
||||
/** |
||||
* Find a {@link DatabaseDriver} for the given URL. |
||||
* @param url JDBC URL |
||||
* @return the database driver or {@link #UNKNOWN} if not found |
||||
*/ |
||||
public static DatabaseDriver fromJdbcUrl(String url) { |
||||
if (StringUtils.hasLength(url)) { |
||||
Assert.isTrue(url.startsWith("jdbc"), "URL must start with 'jdbc'"); |
||||
String urlWithoutPrefix = url.substring("jdbc".length()).toLowerCase(); |
||||
for (DatabaseDriver driver : values()) { |
||||
String prefix = ":" + driver.name().toLowerCase() + ":"; |
||||
if (driver != UNKNOWN && urlWithoutPrefix.startsWith(prefix)) { |
||||
return driver; |
||||
} |
||||
} |
||||
} |
||||
return UNKNOWN; |
||||
} |
||||
|
||||
/** |
||||
* Find a {@link DatabaseDriver} for the given product name. |
||||
* @param productName product name |
||||
* @return the database driver or {@link #UNKNOWN} if not found |
||||
*/ |
||||
public static DatabaseDriver fromProductName(String productName) { |
||||
if (StringUtils.hasLength(productName)) { |
||||
for (DatabaseDriver candidate : values()) { |
||||
if (candidate.matchProductName(productName)) { |
||||
return candidate; |
||||
} |
||||
} |
||||
} |
||||
return UNKNOWN; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue