首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JDBC驱动已经被Tomcat7强制注销,为什么?

JDBC驱动已经被Tomcat7强制注销,为什么?
EN

Stack Overflow用户
提问于 2013-10-05 03:31:46
回答 1查看 47K关注 0票数 17

我在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周后出现的。

那么我该如何解决这个问题呢?它与我的代码相关吗?

EN

回答 1

Stack Overflow用户

发布于 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,正如您在上面看到的。

票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19189312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档