关闭Tomcat报错appears to have started a thread named [Abandoned connection-cleanup] but has failed to st

本文介绍了解决Tomcat关闭时报错的问题,通过在contextDestroyed函数中添加代码来释放MySQL驱动,避免了因数据库连接未正确关闭而导致的服务异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关闭Tomcat报错The web application [my_login] appears to have started a thread named [mysql-cj-abandoned-connection-cleanup] but has failed to stop it

1.原因是在关闭Tomcat服务时,数据库的(mysql和oracle)驱动此时还无法释放。
2.针对驱动无法释放,可以通过ServlectContextListner事件的contextDestroyed函数里加一句代码,在服务停止前释放所有的驱动Driver
3.释放驱动代码:com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.uncheckedShutdown();

如图下代码33行的 AbandonedConnectionCleanupThread.shutdown();这句可以放在第23行的 while (drivers.hasMoreElements()) while循环外面,度娘说先删除驱动,再释放驱动资源,但是代码可以换过来,先释放或者先关闭服务都可以,本人亲自试过运行ok

  1. DriverManager.deregisterDriver(d); //关闭tomcat服务
  2. AbandonedConnectionCleanupThread.shutdown();//是释放驱动资源 列如:mysql-connector-java-8.0.28-bin.jar是项目的驱动连接桥
package reyo.sdk.utils.mysql;

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 com.mysql.jdbc.AbandonedConnectionCleanupThread;

@WebListener
public class MyServletContextListener implements ServletContextListener {

    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);
                System.out.println(String.format("ContextFinalizer:Driver %s deregistered", d));
            } catch (SQLException ex) {
                System.out.println(String.format("ContextFinalizer:Error deregistering driver %s", d) + ":" + ex);
            }
        }
        try {
            AbandonedConnectionCleanupThread.shutdown();
        } catch (InterruptedException e) {
            System.out.println("ContextFinalizer:SEVERE problem cleaning up: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
  <!--ServletContext监听器--> 
   <listener>
    <listener-class>com.modle.listener.MyServletContextListener</listener-class>
   </listener>

如图下所示:
在这里插入图片描述
解决Eclipse报错:‘org.eclipse.jst.jee.server:ProName’ did not find a matching property
eclipse中java web.xml报错cvc-complex-type.2.3: Element ‘web-app’ cannot have character [children], be…
web.xml文件添加servlet访问限制后出现如下错误:
  cvc-complex-type.2.3: Element ‘web-app’ cannot have character [children], because the type’s content type is element- only.
翻译:
  cvc-complex-type.2.3:元素’web-app’不能包含character [children],因为该类型的内容类型是仅包含元素的。
解决办法一:
  出错原因为xml头文件中第三行:
  xmlns=“http://java.sun.com/xml/ns/javaee”
  把javaee改成: j2ee后解决问题。
解决办法二:
  由于是粘贴过来的编码可能有问题,将xml中的文本重新手打输入一遍,一般就会解决这种问题。
  在这里插入图片描述

12-Jul-2023 18:36:28.896 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file 12-Jul-2023 18:36:28.898 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Context [/setting] startup failed due to previous errors 2023-07-12 18:36:28,899 [// - - ] INFO org.springframework.web.context.support.XmlWebApplicationContext - Closing Root WebApplicationContext: startup date [Wed Jul 12 18:36:27 CST 2023]; root of context hierarchy 2023-07-12 18:36:28,900 [// - - ] INFO org.springframework.cache.ehcache.EhCacheManagerFactoryBean - Shutting down EhCache CacheManager 12-Jul-2023 18:36:28.908 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [setting] 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. 12-Jul-2023 18:36:28.908 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [setting] 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(ReferenceQueue.java:143) com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:40)
07-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenzhiyuan09

你的鼓励是我最大的动力一起加吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值