tomcat重啟警告:Abandoned connection cleanup thread 服務器宕機解決方案


每次出現這個報錯都會導致tomcat應用服務器停機

報錯信息:

1 The web application [HelloWeb] 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:
2          java.lang.Object.wait(Native Method)
3          java.lang.ref.ReferenceQueue.remove(Unknown Source)
4          com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)

加了下面的java代碼后就再也沒有停過了。

 1 package cn.listener;
 2 
 3 import java.sql.Driver;
 4 import java.sql.DriverManager;
 5 import java.sql.SQLException;
 6 import java.util.Enumeration;
 7 
 8 import javax.servlet.ServletContextEvent;
 9 import javax.servlet.ServletContextListener;
10 import javax.servlet.annotation.WebListener;
11 
12 import com.mysql.jdbc.AbandonedConnectionCleanupThread;
13 
14 
15 @WebListener
16 public class ContextFinalizer implements ServletContextListener {
17 
18     public void contextInitialized(ServletContextEvent sce) {
19     }
20 
21     public void contextDestroyed(ServletContextEvent sce) {
22         Enumeration<Driver> drivers = DriverManager.getDrivers();
23         Driver d = null;
24         while (drivers.hasMoreElements()) {
25             try {
26                 d = drivers.nextElement();
27                 DriverManager.deregisterDriver(d);
28                 System.out.println(String.format("ContextFinalizer:Driver %s deregistered", d));
29             } catch (SQLException ex) {
30                 System.out.println(String.format("ContextFinalizer:Error deregistering driver %s", d) + ":" + ex);
31             }
32         }
33         try {
34             AbandonedConnectionCleanupThread.shutdown();
35         } catch (InterruptedException e) {
36             System.out.println("ContextFinalizer:SEVERE problem cleaning up: " + e.getMessage());
37             e.printStackTrace();
38         }
39     }
40 }

@WebListener,這個注解相當於在web.xml配置如下內容
1   <listener>
2     <listener-class>cn.listener.ContextFinalizer</listener-class>
3   </listener>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM