问题描述
在进行Web开发的过程中,我只是在Eclipse IDE中关闭了Web应用程序,大约一分钟后,我在Eclipse控制台中看到了警告.
In the middle of my web-development I just close my web-app in my eclipse IDE, about a minute, I just saw a WARNING in my eclipse console.
WARNING: The web application [/Spring.MVC] 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.
Sep 06, 2014 8:31:55 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
WARNING: The web application [/Spring.MVC] 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:40)
Sep 06, 2014 8:32:00 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Sep 06, 2014 8:32:00 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Sep 06, 2014 8:32:03 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
name: personPU
...]
到目前为止,它并没有引起任何内存泄漏,我检查了我的VisualVM,一切都照常进行,但是当我对此事进行更多搜索时,我意识到此警告是由MySQL驱动程序未释放资源或未关闭导致的正确地(我不知道该怎么说),我最终在这篇文章中发表了相关问题
It did not caused any memory leak so far, I checked my VisualVM, everything is working as usual, but as I search more about this thing I realize that this warning is caused by the MySQL driver not releasing resources or not being closed properly(I dont know how to say it exactly) and I ended up in this post at SO related issue
OP是正确的回答不用担心" 是不够的.此警告令我感到困扰,因为它可能会给我带来一些未来持久性问题,并且让我很担心的是,我尝试了OP编写的代码,但是我遇到了什么是图书馆的问题. >我应该使用它来使此代码正常工作.这是我很抱歉的事情.
and the OP is right the answer of "don't worry about it" won't be sufficient. This warning bothers me because it may give me some future persistence problems and that worries me a lot, I tried the code the OP has written but I'm having a problem what Libraries should I use to make this code work. This is what I got sor far..
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 org.hibernate.annotations.common.util.impl.LoggerFactory;
import com.mysql.jdbc.AbandonedConnectionCleanupThread;
@WebListener
public class ContextFinalizer implements ServletContextListener {
private static final Logger LOGGER = LoggerFactory.getLogger(ContextFinalizer.class);
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);
LOGGER.warn(String.format("Driver %s deregistered", d));
}
catch (SQLException ex) {
LOGGER.warn(String.format("Error deregistering driver %s", d), ex);
}
}
try {
AbandonedConnectionCleanupThread.shutdown();
}
catch (InterruptedException e) {
logger.warn("SEVERE problem cleaning up: " + e.getMessage());
e.printStackTrace();
}
}
}
我只是想知道我需要什么库,或者如果我使用正确的库来正确实现此目的,我什至不知道我应该使用什么Logger,谢谢您的帮助,
I just want to know what Libraries do I need or If I'm using the right libraries to implement this properly, I do not even know what Logger should I use, thank you for any help,
推荐答案
请参见.似乎应该在应用程序之间的{$TOMCAT]/lib
中共享MySQL驱动程序.检查您没有在每个应用程序中都包括它.至少它对我有用,并且我已经能够删除警告.
See this answer. It seems that MySQL driver should be in {$TOMCAT]/lib
shared between applications. Check that you are not including it with each application. At least it worked for me and I have been able to remove the warning.
如果您使用的是Maven,请标记提供的依赖项.
If you are using Maven mark the dependency as provided.
更新:
根本原因是Tomcat遇到了垃圾回收驱动程序的问题,因为它是在多个应用程序通用的单例中注册的.关闭一个应用程序不允许Tomcat释放驱动程序.参见此 answer .
UPDATE:
root cause is that Tomcat have problems to garbage collect the driver because it is registered in a singleton common to several applications. Closing one application does not allow Tomcat to release the driver. See this answer.
这篇关于Web应用程序[]似乎已经启动了一个名为[Abandoned connection cleanup thread] com.mysql.jdbc.AbandonedConnectionCleanupThread的线程.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!