tomcat重启警告:Abandoned connection cleanup thread)

tomcat重启警告:Abandoned connection cleanup thread)

报错信息


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:
		 java.lang.Object.wait(Native Method)
		 java.lang.ref.ReferenceQueue.remove(Unknown Source)
		 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)

解决方法

web.xml中加一个监听器;

<listener>
		<listener-class>com.ls.utils.ContextFinalizer</listener-class>
	</listener>

新建一个ContextFinalizer,在tomcat重启的时候,手动杀掉mysql的进程,类如下:

/**
 * 
 */
/**
 * @author cdy
 *
 */
package com.ls.utils;

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 ContextFinalizer 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();
		}
	}
}

你可能感兴趣的:(tomcat,tomcat8内存泄露)