mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
optimized for mysql
This commit is contained in:
@@ -127,7 +127,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
stmt.setClosed(true); // soft set close
|
||||
|
||||
if (stmt.getHitCount() < 2 && isOracle()) {
|
||||
if (stmt.getHitCount() < 1 && isOracle()) {
|
||||
OracleUtils.clearDefines(stmt);
|
||||
}
|
||||
} else {
|
||||
|
||||
+5
-52
@@ -1,39 +1,21 @@
|
||||
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.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.PoolableConnection;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.util.DruidLoaderUtils;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.util.MySqlUtils;
|
||||
|
||||
public class MySqlValidConnectionChecker implements ValidConnectionChecker, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final Log LOG = LogFactory.getLog(MySqlValidConnectionChecker.class);
|
||||
|
||||
private Class<?> clazz;
|
||||
private Method ping;
|
||||
private boolean driverHasPingMethod = false;
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final Log LOG = LogFactory.getLog(MySqlValidConnectionChecker.class);
|
||||
|
||||
public MySqlValidConnectionChecker(){
|
||||
try {
|
||||
clazz = DruidLoaderUtils.loadClass("com.mysql.jdbc.Connection");
|
||||
ping = clazz.getMethod("ping");
|
||||
if (ping != null) {
|
||||
driverHasPingMethod = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Cannot resolve com.mysq.jdbc.Connection.ping method. Will use 'SELECT 1' instead.", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection conn, String valiateQuery, int validationQueryTimeout) {
|
||||
@@ -50,41 +32,12 @@ public class MySqlValidConnectionChecker implements ValidConnectionChecker, Seri
|
||||
return true;
|
||||
}
|
||||
|
||||
if (driverHasPingMethod) {
|
||||
if (conn instanceof PoolableConnection) {
|
||||
conn = ((PoolableConnection) conn).getConnection();
|
||||
}
|
||||
|
||||
if (conn instanceof ConnectionProxy) {
|
||||
conn = ((ConnectionProxy) conn).getConnectionRaw();
|
||||
}
|
||||
|
||||
if (clazz.isAssignableFrom(conn.getClass())) {
|
||||
try {
|
||||
ping.invoke(conn);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
stmt = conn.createStatement();
|
||||
stmt.setQueryTimeout(validationQueryTimeout);
|
||||
rs = stmt.executeQuery(valiateQuery);
|
||||
MySqlUtils.ping(conn);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
||||
public class MySqlUtils {
|
||||
public static com.mysql.jdbc.Connection unwrap(Connection conn) throws SQLException {
|
||||
if (conn instanceof com.mysql.jdbc.Connection) {
|
||||
return (com.mysql.jdbc.Connection) conn;
|
||||
}
|
||||
|
||||
return conn.unwrap(com.mysql.jdbc.Connection.class);
|
||||
}
|
||||
|
||||
public static void ping(Connection conn) throws SQLException {
|
||||
com.mysql.jdbc.Connection mysqlConn = unwrap(conn);
|
||||
mysqlConn.ping();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user