我在tomcat 7中遇到了一个问题,这里有一些关于它的信息,
1-我有这样一条消息:
INFO: Reloading Context with name [/WebApp] has started
Oct 04, 2013 12:20:50 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SEVERE: The web application [/WebApp] 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.
Oct 04, 2013 12:20:50 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/WebApp] 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.
Oct 04, 2013 12:20:51 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/WebApp] is completed
2-当我重新加载应用程序时,问题解决了大约20个小时,然后又回来了。
3-我在tomcat上部署了大约10个应用程序,但其中只有2个得到了这个错误。
4-问题并不是从这两个应用程序的乞讨中出现的,而是在大约2周后出现的。
那么我该如何解决这个问题呢?它与我的代码相关吗?
发布于 2013-10-05 03:51:22
当您在Tomcat中停止web应用程序时,它会尝试关闭它启动的线程,并关闭一堆资源,例如JDBC驱动程序。虽然在这种情况下,它能够关闭它们,但更安全的做法是自己做。
您可以在ServletContextListener
中完成此操作。我按如下方式实现了我的
@WebListener // register it as you wish
public class ContainerContextClosedHandler implements ServletContextListener {
private static final Logger logger = LoggerFactory.getLogger(ContainerContextClosedHandler.class);
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
// nothing to do
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
Enumeration<Driver> drivers = DriverManager.getDrivers();
Driver driver = null;
// clear drivers
while(drivers.hasMoreElements()) {
try {
driver = drivers.nextElement();
DriverManager.deregisterDriver(driver);
} catch (SQLException ex) {
// deregistration failed, might want to do something, log at the very least
}
}
// MySQL driver leaves around a thread. This static method cleans it up.
try {
AbandonedConnectionCleanupThread.shutdown();
} catch (InterruptedException e) {
// again failure, not much you can do
}
}
}
MySQL确实启动了一个Tomcat无法关闭的线程。对于当前版本(5.1.23+),他们提供了AbandonedConnectionCleanupThread
类来关闭衍生的Thread
,正如您在上面看到的。
https://stackoverflow.com/questions/19189312
复制相似问题