web应用程序似乎启动了一个名为[mysql-cj-abandoned-connection-cleanup]的线程,但未停止,可能会造成内存泄漏...

在Tomcat关闭或重启的时候,会报这样的异常信息。

解决方法:设置监听器,在服务器注销的时候运行这句话就不会有问题了。

AbandonedConnectionCleanupThread.uncheckedShutdown();

它的来源:com.mysql.cj.jdbc

名字直译过来:断开连接,清理线程。

它的源码中无任何注释,也没找到官方API,本人没法深究。

操作步骤:

任意位置新建一个监听类,代码如下:

import com.mysql.cj.jdbc.AbandonedConnectionCleanupThread;
import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Enumeration;

@WebListener
public class Listener_My implements HttpSessionListener, ServletRequestListener, ServletContextListener, ServletContextAttributeListener,
        ServletRequestAttributeListener, HttpSessionAttributeListener {

    @Override
    public void sessionCreated(HttpSessionEvent se) {
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        System.out.println("请求初始化");
    }
    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        System.out.println("请求销毁");

    }

    @Override
    public void contextInitialized(ServletContextEvent sce){
        try{
            System.out.println("Servlet初始化");
        }catch (Exception e){
            System.out.println("初始化失败");
        }
    }
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try {
            System.out.println("Servlet销毁");
            SqlConnection.dbDestroyed();
            Enumeration drivers = DriverManager.getDrivers();
            while (drivers.hasMoreElements()) {
                Driver driver = (Driver) drivers.nextElement();
                DriverManager.deregisterDriver(driver);
                System.out.println("deregistering jdbc driver: " + driver);
            }
            AbandonedConnectionCleanupThread.uncheckedShutdown();
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("销毁工作异常");
        }

    }
}

解决了

你可能感兴趣的:(JavaWeb,tomcat)