【问题标题】:The web application [] appears to have started a thread named [Abandoned connection cleanup thread] com.mysql.jdbc.AbandonedConnectionCleanupThreadWeb 应用程序 [] 似乎已经启动了一个名为 [Abandoned connection cleanup thread] com.mysql.jdbc.AbandonedConnectionCleanupThread 的线程
【发布时间】:2014-10-31 05:45:31
【问题描述】:

在我的网络开发过程中,我刚刚在我的 Eclipse IDE 中关闭了我的网络应用程序,大约一分钟后,我刚刚在我的 Eclipse 控制台中看到了一个 WARNING

WARNING: The web application [/Spring.MVC] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Sep 06, 2014 8:31:55 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
WARNING: The web application [/Spring.MVC] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(Unknown Source)
 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:40)
Sep 06, 2014 8:32:00 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Sep 06, 2014 8:32:00 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Sep 06, 2014 8:32:03 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: personPU
    ...]

到目前为止,它没有造成任何内存泄漏,我检查了我的 VisualVM,一切正常,但是当我更多地搜索这个东西时,我意识到这个警告是由于 MySQL 驱动程序没有释放资源或没有被关闭引起的正确(我不知道如何准确地说),我最终在 SO related issue 发表这篇文章

OP 是对的“别担心”的答案是不够的。这个警告让我很困扰,因为它可能会给我带来一些未来的持久性问题,这让我很担心,我尝试了 OP 编写的代码,但我遇到了问题 what Libraries 我应该用它来使这段代码工作吗?这就是我到目前为止所得到的..

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.hibernate.annotations.common.util.impl.LoggerFactory;
import com.mysql.jdbc.AbandonedConnectionCleanupThread;
@WebListener
public class ContextFinalizer implements ServletContextListener {

private static final Logger LOGGER = LoggerFactory.getLogger(ContextFinalizer.class);

public void contextInitialized(ServletContextEvent sce) {
}

public void contextDestroyed(ServletContextEvent sce) {
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    Driver d = null;
    while (drivers.hasMoreElements()) {
        try {
            d = drivers.nextElement();
            DriverManager.deregisterDriver(d);
            LOGGER.warn(String.format("Driver %s deregistered", d));
        }
        catch (SQLException ex) {
            LOGGER.warn(String.format("Error deregistering driver %s", d), ex);
        }
    }
    try {
        AbandonedConnectionCleanupThread.shutdown();
    }
    catch (InterruptedException e) {
        logger.warn("SEVERE problem cleaning up: " + e.getMessage());
        e.printStackTrace();
    }
  }
}

我只是想知道我需要什么库,或者如果我使用正确的库来正确实现这一点,我什至不知道我应该使用什么 Logger,谢谢你的帮助,

【问题讨论】:

  • 解决方案不需要记录器。你运行你的代码了吗?发生了什么?您仍然看到警告吗?
  • 感谢您的回复,我无法运行它,我不知道此代码不需要记录器,我只是在尝试之前重新启动了计算机,我会回帖以防发生不同的事情。谢谢。 :)
  • @SotiriosDelimanolis,我刚刚尝试完上面的示例代码,没有记录器(谢谢),是的,它正在工作,AbandonedConnectionCleanupThread 正在关闭,但是,因为我正在监视服务器在我的 VisualVM 中,permgen 空间仍然被占用,我仍然会出现内存泄漏,我仍然需要手动执行GC。但至于代码,是的,它正在工作。

标签: mysql spring jdbc memory-leaks


【解决方案1】:

this answer。看来 MySQL 驱动程序应该在{$TOMCAT]/lib 应用程序之间共享。检查您是否没有将其包含在每个应用程序中。至少它对我有用,并且我已经能够删除警告。

如果您使用 Maven,请将依赖项标记为提供。


更新:
根本原因是 Tomcat 在垃圾收集驱动程序方面存在问题,因为它注册在多个应用程序通用的单例中。关闭一个应用程序不允许 Tomcat 释放驱动程序。看到这个answer

【讨论】:

  • 谁能再扩展一下?这是与特定 jdbc 驱动程序版本相关的错误吗?
  • 大家好。查看更新。其他stackoverflow答案的链接中有更多信息。
  • @borjab 将依赖项标记为provided。但是在启动服务器时出现问题,因为我在servletcontextlistner 中配置了数据库连接。有什么解决办法吗?也完成了&lt;scope&gt;provided test&lt;/scope&gt;,但失败了。
  • 您是否已将 MySQL 驱动程序添加到您的 tomcat 库中?
  • @borjab 号哦..我误解了provided。现在添加。但它在ContextDestroyed 上显示AbandonedConnectionCleanup..。在pom.xml 中使用provided 范围为mysql-connector
【解决方案2】:

对我来说,我停止了 mysql(服务 mysql 启动),然后我停止了 bin 文件夹中的 tomcat(./shutdown.sh),问题就解决了。

【讨论】:

    【解决方案3】:

    这对我有用

    就我而言,问题在于弹性搜索。

    从邮递员发送 DELETE 请求到以下端点并重新启动您的应用程序。

    http://localhost:9200/*
    

    【讨论】:

      猜你喜欢
      • 2017-11-10
      • 2017-12-26
      • 2017-06-09
      • 2021-05-09
      • 1970-01-01
      • 2011-08-19
      • 2019-11-24
      • 2017-11-01
      • 2016-10-01
      相关资源
      最近更新 更多