diff --git a/src/main/java/com/alibaba/druid/util/MySqlUtils.java b/src/main/java/com/alibaba/druid/util/MySqlUtils.java index 4b1b81e908..26148af83a 100644 --- a/src/main/java/com/alibaba/druid/util/MySqlUtils.java +++ b/src/main/java/com/alibaba/druid/util/MySqlUtils.java @@ -331,6 +331,11 @@ public class MySqlUtils { private static transient Method method_getLastPacketReceivedTimeMs = null; private static transient boolean method_getLastPacketReceivedTimeMs_error = false; + private volatile static boolean mysqlJdbcVersion6 = false; + private static transient Class classJdbc = null; + private static transient Method getIdleFor = null; + private static transient boolean getIdleForError = false; + public static Long getId(Object conn) { if (conn == null) { return null; @@ -370,6 +375,12 @@ public class MySqlUtils { if (class_connectionImpl == null && !class_connectionImpl_Error) { try { class_connectionImpl = Utils.loadClass("com.mysql.jdbc.MySQLConnection"); + if(null == class_connectionImpl){ + class_connectionImpl = Utils.loadClass("com.mysql.cj.MysqlConnection"); + if(null != class_connectionImpl){ + mysqlJdbcVersion6 = true; + } + } } catch (Throwable error){ class_connectionImpl_Error = true; } @@ -379,59 +390,92 @@ public class MySqlUtils { return -1; } - if (method_getIO == null && !method_getIO_error) { - try { - method_getIO = class_connectionImpl.getMethod("getIO"); - } catch (Throwable error){ - method_getIO_error = true; + if(mysqlJdbcVersion6){ + if(null == classJdbc) { + classJdbc = Utils.loadClass("com.mysql.cj.jdbc.JdbcConnection"); } - } - - if (method_getIO == null) { - return -1; - } - - if (class_MysqlIO == null && !class_MysqlIO_Error) { - try { - class_MysqlIO = Utils.loadClass("com.mysql.jdbc.MysqlIO"); - } catch (Throwable error){ - class_MysqlIO_Error = true; + if(null == classJdbc){ + return -1; } - } - - if (class_MysqlIO == null) { - return -1; - } - - if (method_getLastPacketReceivedTimeMs == null && !method_getLastPacketReceivedTimeMs_error) { - try { - Method method = class_MysqlIO.getDeclaredMethod("getLastPacketReceivedTimeMs"); - method.setAccessible(true); - method_getLastPacketReceivedTimeMs = method; - } catch (Throwable error){ - method_getLastPacketReceivedTimeMs_error = true; + if(null == getIdleFor && !getIdleForError) { + try { + getIdleFor = classJdbc.getMethod("getIdleFor"); + getIdleFor.setAccessible(true); + } catch (Throwable error) { + getIdleForError = true; + } } - } + if (getIdleFor == null) { + return -1; + } + try { + Object connImpl = conn.unwrap(class_connectionImpl); + if (connImpl == null) { + return -1; + } + Long ms = (long)getIdleFor.invoke(connImpl); + return 0 == ms.longValue()?System.currentTimeMillis() : System.currentTimeMillis()-ms.longValue(); + } catch (IllegalAccessException e) { + throw new SQLException("getIdleFor error", e); + } catch (InvocationTargetException e) { + throw new SQLException("getIdleFor error", e); + } + }else { - if (method_getLastPacketReceivedTimeMs == null) { - return -1; - } + if (method_getIO == null && !method_getIO_error) { + try { + method_getIO = class_connectionImpl.getMethod("getIO"); + } catch (Throwable error) { + method_getIO_error = true; + } + } - try { - Object connImpl = conn.unwrap(class_connectionImpl); - if (connImpl == null) { + if (method_getIO == null) { return -1; } - Object mysqlio = method_getIO.invoke(connImpl); - Long ms = (Long) method_getLastPacketReceivedTimeMs.invoke(mysqlio); - return ms.longValue(); - } catch (IllegalArgumentException e) { - throw new SQLException("getLastPacketReceivedTimeMs error", e); - } catch (IllegalAccessException e) { - throw new SQLException("getLastPacketReceivedTimeMs error", e); - } catch (InvocationTargetException e) { - throw new SQLException("getLastPacketReceivedTimeMs error", e); + if (class_MysqlIO == null && !class_MysqlIO_Error) { + try { + class_MysqlIO = Utils.loadClass("com.mysql.jdbc.MysqlIO"); + } catch (Throwable error) { + class_MysqlIO_Error = true; + } + } + + if (class_MysqlIO == null) { + return -1; + } + + if (method_getLastPacketReceivedTimeMs == null && !method_getLastPacketReceivedTimeMs_error) { + try { + Method method = class_MysqlIO.getDeclaredMethod("getLastPacketReceivedTimeMs"); + method.setAccessible(true); + method_getLastPacketReceivedTimeMs = method; + } catch (Throwable error) { + method_getLastPacketReceivedTimeMs_error = true; + } + } + + if (method_getLastPacketReceivedTimeMs == null) { + return -1; + } + + try { + Object connImpl = conn.unwrap(class_connectionImpl); + if (connImpl == null) { + return -1; + } + + Object mysqlio = method_getIO.invoke(connImpl); + Long ms = (Long)method_getLastPacketReceivedTimeMs.invoke(mysqlio); + return ms.longValue(); + } catch (IllegalArgumentException e) { + throw new SQLException("getLastPacketReceivedTimeMs error", e); + } catch (IllegalAccessException e) { + throw new SQLException("getLastPacketReceivedTimeMs error", e); + } catch (InvocationTargetException e) { + throw new SQLException("getLastPacketReceivedTimeMs error", e); + } } }