mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
validateConnection add exception cause
This commit is contained in:
@@ -1247,8 +1247,19 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
}
|
||||
|
||||
if (validConnectionChecker != null) {
|
||||
if (!validConnectionChecker.isValidConnection(conn, validationQuery, validationQueryTimeout)) {
|
||||
throw new SQLException("validateConnection false");
|
||||
boolean result = true;
|
||||
Exception error = null;
|
||||
try {
|
||||
result = validConnectionChecker.isValidConnection(conn, validationQuery, validationQueryTimeout);
|
||||
} catch (Exception ex) {
|
||||
error = ex;
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
SQLException sqlError = error != null ? //
|
||||
new SQLException("validateConnection false", error) //
|
||||
: new SQLException("validateConnection false");
|
||||
throw sqlError;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ import java.util.Properties;
|
||||
|
||||
public interface ValidConnectionChecker {
|
||||
|
||||
boolean isValidConnection(Connection c, String query, int validationQueryTimeout);
|
||||
|
||||
boolean isValidConnection(Connection c, String query, int validationQueryTimeout) throws Exception;
|
||||
|
||||
/**
|
||||
* @param properties
|
||||
* @since 0.2.21
|
||||
|
||||
@@ -25,7 +25,7 @@ import java.util.Properties;
|
||||
public class ValidConnectionCheckerAdapter implements ValidConnectionChecker {
|
||||
|
||||
@Override
|
||||
public boolean isValidConnection(Connection c, String query, int validationQueryTimeout) {
|
||||
public boolean isValidConnection(Connection c, String query, int validationQueryTimeout) throws Exception {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -34,4 +34,5 @@ public class ValidConnectionCheckerAdapter implements ValidConnectionChecker {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+7
-19
@@ -15,17 +15,15 @@
|
||||
*/
|
||||
package com.alibaba.druid.pool.vendor;
|
||||
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
/**
|
||||
* A MSSQLValidConnectionChecker.
|
||||
*/
|
||||
@@ -33,19 +31,12 @@ public class MSSQLValidConnectionChecker extends ValidConnectionCheckerAdapter i
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(MSSQLValidConnectionChecker.class);
|
||||
|
||||
public MSSQLValidConnectionChecker(){
|
||||
|
||||
}
|
||||
|
||||
public boolean isValidConnection(final Connection c, String validateQuery, int validationQueryTimeout) {
|
||||
try {
|
||||
if (c.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
// skip
|
||||
public boolean isValidConnection(final Connection c, String validateQuery, int validationQueryTimeout) throws Exception {
|
||||
if (c.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -57,10 +48,7 @@ public class MSSQLValidConnectionChecker extends ValidConnectionCheckerAdapter i
|
||||
stmt.execute(validateQuery);
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
if (LOG.isWarnEnabled()) {
|
||||
LOG.warn("warning: connection validation failed for current managed connection.");
|
||||
}
|
||||
return false;
|
||||
throw e;
|
||||
} finally {
|
||||
JdbcUtils.close(stmt);
|
||||
}
|
||||
|
||||
+11
-36
@@ -15,6 +15,13 @@
|
||||
*/
|
||||
package com.alibaba.druid.pool.vendor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
@@ -24,15 +31,6 @@ import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.util.Utils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
|
||||
public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
|
||||
|
||||
public static final int DEFAULT_VALIDATION_QUERY_TIMEOUT = 1000;
|
||||
@@ -76,13 +74,8 @@ public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter i
|
||||
this.usePingMethod = usePingMethod;
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) {
|
||||
try {
|
||||
if (conn.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
// skip
|
||||
public boolean validConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
|
||||
if (conn.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -100,21 +93,8 @@ public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter i
|
||||
validationQueryTimeout = DEFAULT_VALIDATION_QUERY_TIMEOUT;
|
||||
}
|
||||
|
||||
try {
|
||||
ping.invoke(conn, true, validationQueryTimeout);
|
||||
return true;
|
||||
} catch (InvocationTargetException e) {
|
||||
Throwable cause = e.getCause();
|
||||
if (cause instanceof SQLException) {
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
return false;
|
||||
}
|
||||
ping.invoke(conn, true, validationQueryTimeout);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,11 +107,6 @@ public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter i
|
||||
}
|
||||
rs = stmt.executeQuery(validateQuery);
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
return false;
|
||||
} finally {
|
||||
JdbcUtils.close(rs);
|
||||
JdbcUtils.close(stmt);
|
||||
|
||||
+19
-38
@@ -18,7 +18,6 @@ package com.alibaba.druid.pool.vendor;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -26,15 +25,12 @@ import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class OracleValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2227528634302168877L;
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(OracleValidConnectionChecker.class);
|
||||
|
||||
private int timeout = 1;
|
||||
|
||||
@@ -57,48 +53,33 @@ public class OracleValidConnectionChecker extends ValidConnectionCheckerAdapter
|
||||
this.timeout = seconds;
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) {
|
||||
public boolean validConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
|
||||
if (validateQuery == null || validateQuery.isEmpty()) {
|
||||
validateQuery = this.defaultValidateQuery;
|
||||
}
|
||||
|
||||
try {
|
||||
if (conn.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
// skip
|
||||
if (conn.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (conn instanceof DruidPooledConnection) {
|
||||
conn = ((DruidPooledConnection) conn).getConnection();
|
||||
}
|
||||
|
||||
if (conn instanceof ConnectionProxy) {
|
||||
conn = ((ConnectionProxy) conn).getRawObject();
|
||||
}
|
||||
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
if (conn instanceof DruidPooledConnection) {
|
||||
conn = ((DruidPooledConnection) conn).getConnection();
|
||||
}
|
||||
|
||||
if (conn instanceof ConnectionProxy) {
|
||||
conn = ((ConnectionProxy) conn).getRawObject();
|
||||
}
|
||||
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
stmt = conn.createStatement();
|
||||
stmt.setQueryTimeout(timeout);
|
||||
rs = stmt.executeQuery(validateQuery);
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
return false;
|
||||
} finally {
|
||||
JdbcUtils.close(rs);
|
||||
JdbcUtils.close(stmt);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in pingDatabase", e);
|
||||
return false;
|
||||
stmt = conn.createStatement();
|
||||
stmt.setQueryTimeout(timeout);
|
||||
rs = stmt.executeQuery(validateQuery);
|
||||
return true;
|
||||
} finally {
|
||||
JdbcUtils.close(rs);
|
||||
JdbcUtils.close(stmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+18
-38
@@ -18,70 +18,50 @@ package com.alibaba.druid.pool.vendor;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class PGValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2227528634302168877L;
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(PGValidConnectionChecker.class);
|
||||
|
||||
private String defaultValidateQuery = "SELECT 'x'";
|
||||
|
||||
public PGValidConnectionChecker(){
|
||||
configFromProperties(System.getProperties());
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) {
|
||||
public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
|
||||
if (validateQuery == null || validateQuery.isEmpty()) {
|
||||
validateQuery = this.defaultValidateQuery;
|
||||
}
|
||||
|
||||
try {
|
||||
if (conn.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
// skip
|
||||
if (conn.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (conn instanceof DruidPooledConnection) {
|
||||
conn = ((DruidPooledConnection) conn).getConnection();
|
||||
}
|
||||
|
||||
if (conn instanceof ConnectionProxy) {
|
||||
conn = ((ConnectionProxy) conn).getRawObject();
|
||||
}
|
||||
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
if (conn instanceof DruidPooledConnection) {
|
||||
conn = ((DruidPooledConnection) conn).getConnection();
|
||||
}
|
||||
|
||||
if (conn instanceof ConnectionProxy) {
|
||||
conn = ((ConnectionProxy) conn).getRawObject();
|
||||
}
|
||||
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
stmt = conn.createStatement();
|
||||
rs = stmt.executeQuery(validateQuery);
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
return false;
|
||||
} finally {
|
||||
JdbcUtils.close(rs);
|
||||
JdbcUtils.close(stmt);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in pingDatabase", e);
|
||||
return false;
|
||||
stmt = conn.createStatement();
|
||||
rs = stmt.executeQuery(validateQuery);
|
||||
return true;
|
||||
} finally {
|
||||
JdbcUtils.close(rs);
|
||||
JdbcUtils.close(stmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user