在IDEA中部署web项目到tomcat测试后, 关闭tomcat时可能遇到以下警告信息: com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
完整信息如下:
警告 [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [XXX_war_exploded] 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. 警告 [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [XXX_war_exploded] 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:144) com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
其实意思是注册过jdbc‘驱动,但是在容器关闭时没有注销,并且是在开启了一个AbandonedConnectionCleanupThread线程中的。 所以为了内存避免泄露,就强制注销了驱动。
这是一个保护机制,并不是问题,所以可以忽略。
如果你想不打印这个警告信息,还可以这么做,写一个容器监听器,在容器销毁时,手动编码注销驱动,以下是一个简单的示例代码:
代码语言:javascript复制package org.byron4j;
import com.mysql.jdbc.AbandonedConnectionCleanupThread;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
public class MyContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
}
// 自己写代码注销驱动
@Override
public void contextDestroyed(ServletContextEvent sce) {
Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
try {
DriverManager.deregisterDriver(drivers.nextElement());
} catch (SQLException e) {
e.printStackTrace();
}
}
// 再关闭AbandonedConnectionCleanupThread线程
try {
AbandonedConnectionCleanupThread.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("手动注销驱动、关闭AbandonedConnectionCleanupThread线程");
}
}
当然,不要忘了,记得在web.xml中配置一下这个监听器。