應(yīng)用升級MySQL驅(qū)動8.0后,在并發(fā)量較高時,查看監(jiān)控打點,Druid連接池拿到連接并執(zhí)行SQL的時間大部分都超過200ms
對系統(tǒng)進(jìn)行壓測,發(fā)現(xiàn)出現(xiàn)大量線程阻塞的情況,線程dump信息如下:
"http-nio-5366-exec-48" #210 daemon prio=5 os_prio=0 tid=0x00000000023d0800 nid=0x3be9 waiting for monitor entry [0x00007fa4c1400000] java.lang.Thread.State: BLOCKED (on object monitor) at org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader.loadClass(TomcatEmbeddedWebappClassLoader.java:66) - waiting to lock 0x0000000775af0960> (a java.lang.Object) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1186) at com.alibaba.druid.util.Utils.loadClass(Utils.java:220) at com.alibaba.druid.util.MySqlUtils.getLastPacketReceivedTimeMs(MySqlUtils.java:372)
public class MySqlUtils { public static long getLastPacketReceivedTimeMs(Connection conn) throws SQLException { if (class_connectionImpl == null !class_connectionImpl_Error) { try { class_connectionImpl = Utils.loadClass("com.mysql.jdbc.MySQLConnection"); } catch (Throwable error){ class_connectionImpl_Error = true; } } if (class_connectionImpl == null) { return -1; } if (method_getIO == null !method_getIO_error) { try { method_getIO = class_connectionImpl.getMethod("getIO"); } catch (Throwable error){ method_getIO_error = true; } } 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 (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); } }
MySqlUtils中的getLastPacketReceivedTimeMs()方法會加載com.mysql.jdbc.MySQLConnection這個類,但在MySQL驅(qū)動8.0中類名改為com.mysql.cj.jdbc.ConnectionImpl,所以MySQL驅(qū)動8.0中加載不到com.mysql.jdbc.MySQLConnection
getLastPacketReceivedTimeMs()方法實現(xiàn)中,如果Utils.loadClass("com.mysql.jdbc.MySQLConnection")加載不到類并拋出異常,會修改變量class_connectionImpl_Error,下次調(diào)用不會再進(jìn)行加載
public class Utils { public static Class?> loadClass(String className) { Class?> clazz = null; if (className == null) { return null; } try { return Class.forName(className); } catch (ClassNotFoundException e) { // skip } ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader(); if (ctxClassLoader != null) { try { clazz = ctxClassLoader.loadClass(className); } catch (ClassNotFoundException e) { // skip } } return clazz; }
但是,在Utils的loadClass()方法中同樣catch了ClassNotFoundException,這就導(dǎo)致loadClass()在加載不到類的時候,并不會拋出異常,從而會導(dǎo)致每調(diào)用一次getLastPacketReceivedTimeMs()方法,就會加載一次MySQLConnection這個類
線程dump信息中可以看到是在調(diào)用TomcatEmbeddedWebappClassLoader的loadClass()方法時,導(dǎo)致線程阻塞的
public class TomcatEmbeddedWebappClassLoader extends ParallelWebappClassLoader {
public Class?> loadClass(String name, boolean resolve) throws ClassNotFoundException { synchronized (JreCompat.isGraalAvailable() ? this : getClassLoadingLock(name)) { Class?> result = findExistingLoadedClass(name); result = (result != null) ? result : doLoadClass(name); if (result == null) { throw new ClassNotFoundException(name); } return resolveIfNecessary(result, resolve); } }
這是因為TomcatEmbeddedWebappClassLoader在加載類的時候,會加synchronized鎖,這就導(dǎo)致每調(diào)用一次getLastPacketReceivedTimeMs()方法,就會加載一次com.mysql.jdbc.MySQLConnection,而又始終加載不到,在加載類的時候會加synchronized鎖,所以會出現(xiàn)線程阻塞,性能下降的現(xiàn)象
public abstract class DruidAbstractDataSource extends WrapperAdapter implements DruidAbstractDataSourceMBean, DataSource, DataSourceProxy, Serializable { protected boolean testConnectionInternal(DruidConnectionHolder holder, Connection conn) { String sqlFile = JdbcSqlStat.getContextSqlFile(); String sqlName = JdbcSqlStat.getContextSqlName(); if (sqlFile != null) { JdbcSqlStat.setContextSqlFile(null); } if (sqlName != null) { JdbcSqlStat.setContextSqlName(null); } try { if (validConnectionChecker != null) { boolean valid = validConnectionChecker.isValidConnection(conn, validationQuery, validationQueryTimeout); long currentTimeMillis = System.currentTimeMillis(); if (holder != null) { holder.lastValidTimeMillis = currentTimeMillis; holder.lastExecTimeMillis = currentTimeMillis; } if (valid isMySql) { // unexcepted branch long lastPacketReceivedTimeMs = MySqlUtils.getLastPacketReceivedTimeMs(conn); if (lastPacketReceivedTimeMs > 0) { long mysqlIdleMillis = currentTimeMillis - lastPacketReceivedTimeMs; if (lastPacketReceivedTimeMs > 0 // mysqlIdleMillis >= timeBetweenEvictionRunsMillis) { discardConnection(holder); String errorMsg = "discard long time none received connection. " + ", jdbcUrl : " + jdbcUrl + ", jdbcUrl : " + jdbcUrl + ", lastPacketReceivedIdleMillis : " + mysqlIdleMillis; LOG.error(errorMsg); return false; } } } if (valid onFatalError) { lock.lock(); try { if (onFatalError) { onFatalError = false; } } finally { lock.unlock(); } } return valid; } if (conn.isClosed()) { return false; } if (null == validationQuery) { return true; } Statement stmt = null; ResultSet rset = null; try { stmt = conn.createStatement(); if (getValidationQueryTimeout() > 0) { stmt.setQueryTimeout(validationQueryTimeout); } rset = stmt.executeQuery(validationQuery); if (!rset.next()) { return false; } } finally { JdbcUtils.close(rset); JdbcUtils.close(stmt); } if (onFatalError) { lock.lock(); try { if (onFatalError) { onFatalError = false; } } finally { lock.unlock(); } } return true; } catch (Throwable ex) { // skip return false; } finally { if (sqlFile != null) { JdbcSqlStat.setContextSqlFile(sqlFile); } if (sqlName != null) { JdbcSqlStat.setContextSqlName(sqlName); } } }
只有DruidAbstractDataSource的testConnectionInternal()方法中會調(diào)用getLastPacketReceivedTimeMs()方法
testConnectionInternal()是用來檢測連接是否有效的,在獲取連接和歸還連接時都有可能會調(diào)用該方法,這取決于Druid檢測連接是否有效的參數(shù)
Druid檢測連接是否有效的參數(shù):
經(jīng)驗證,使用Druid 1.x版本=1.1.22會出現(xiàn)該bug,解決方案就是升級至Druid 1.x版本>=1.1.23或者Druid 1.2.x版本
GitHub issue:https://github.com/alibaba/druid/issues/3808
到此這篇關(guān)于低版本Druid連接池+MySQL驅(qū)動8.0導(dǎo)致線程阻塞、性能受限的文章就介紹到這了,更多相關(guān)MySQL驅(qū)動8.0低版本Druid連接池內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:三明 揚州 定西 阿里 無錫 溫州 山西 福州
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《低版本Druid連接池+MySQL驅(qū)動8.0導(dǎo)致線程阻塞、性能受限》,本文關(guān)鍵詞 低,版本,Druid,連接,池,+MySQL,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。