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>