【问题标题】:Tomcat complaining about memory leak due to open connection after shut due to JDBC driverTomcat 抱怨由于 JDBC 驱动程序关闭后打开连接导致内存泄漏
【发布时间】:2014-11-27 21:11:37
【问题描述】:

我有一个覆盖init() 方法的Servlet,如下所示:

@Override
public void init() throws ServletException {
    BookDAO bookDAO = new BookDAOImpl();
    List<Category> categoryList = bookDAO.findAllCategories();
    getServletContext().setAttribute("categoryList", categoryList);
}

BookDAO#findAllCategories 是:

@Override
public List<Category> findAllCategories() {

    List<Category> result = new ArrayList<>();
    String sql = "select * from category";

    Connection connection = null;

    try {
        connection = getConnection();
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            Category category = new Category();
            category.setId(resultSet.getLong("id"));
            category.setCategoryDescription(resultSet
                    .getString("category_description"));
            result.add(category);
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
    } finally {
        closeConnection(connection);
    }

    return result;

}

我只是点击了这个 servlet,让它被加载。 db-wise 没有别的东西了。当我关闭 Tomcat 时,我得到:

02-Oct-2014 22:29:45.182 INFO [main] org.apache.catalina.core.StandardService.stopInternal Stopping service Catalina
02-Oct-2014 22:29:45.189 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoader.clearReferencesJdbc The web application [/bookwebapp] 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.
02-Oct-2014 22:29:45.190 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoader.clearReferencesThreads The web application [/bookwebapp] 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.
02-Oct-2014 22:29:45.190 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoader.clearReferencesThreads Stack trace of thread "Abandoned connection cleanup thread":
 java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:142)
 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
02-Oct-2014 22:29:45.191 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"]
02-Oct-2014 22:29:45.193 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio-8009"]
02-Oct-2014 22:29:45.193 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"]
02-Oct-2014 22:29:45.194 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["ajp-nio-8009"]
Disconnected from server

我错过了什么?

【问题讨论】:

  • MySQL 留下一个线程。见here

标签: java mysql apache tomcat


【解决方案1】:

您似乎遇到了这个问题:

tomcat7 - jdbc datasource - This is very likely to create a memory leak

具体来说,您将 jdbc 驱动程序作为战争的一部分而不是 tomcat lib 文件夹。

【讨论】:

    猜你喜欢
    • 2019-08-14
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 2016-02-15
    • 2016-07-10
    • 1970-01-01
    相关资源
    最近更新 更多