mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
代码重构
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@311 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
+12
-14
@@ -10,6 +10,7 @@ import java.sql.Statement;
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.util.DruidLoaderUtils;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class MySqlValidConnectionChecker implements ValidConnectionChecker, Serializable {
|
||||
@@ -21,13 +22,10 @@ public class MySqlValidConnectionChecker implements ValidConnectionChecker, Seri
|
||||
private Method ping;
|
||||
private boolean driverHasPingMethod = false;
|
||||
|
||||
// The timeout (apparently the timeout is ignored?)
|
||||
private static Object[] params = new Object[] {};
|
||||
|
||||
public MySqlValidConnectionChecker(){
|
||||
try {
|
||||
clazz = Thread.currentThread().getContextClassLoader().loadClass("com.mysql.jdbc.Connection");
|
||||
ping = clazz.getMethod("ping", new Class[] {});
|
||||
clazz = DruidLoaderUtils.loadClass("com.mysql.jdbc.Connection");
|
||||
ping = clazz.getMethod("ping");
|
||||
if (ping != null) {
|
||||
driverHasPingMethod = true;
|
||||
}
|
||||
@@ -39,18 +37,18 @@ public class MySqlValidConnectionChecker implements ValidConnectionChecker, Seri
|
||||
public boolean isValidConnection(Connection c) {
|
||||
try {
|
||||
Connection conn = (Connection) c.unwrap(clazz);
|
||||
if (conn != null) {
|
||||
c = conn;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (conn != null) {
|
||||
c = conn;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
// if there is a ping method then use it, otherwise just use a 'SELECT 1' statement
|
||||
if (driverHasPingMethod) {
|
||||
try {
|
||||
ping.invoke(c, params);
|
||||
ping.invoke(c);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
|
||||
Reference in New Issue
Block a user