由于JDBC驱动程序关闭后,Tomcat抱怨因打开连接导致内存泄漏

时间:2014-10-02 19:36:16

标签: java mysql apache tomcat

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

@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,因此它被加载了。没有别的分数。当我关闭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

我错过了什么?

1 个答案:

答案 0 :(得分:6)

看来你遇到了这个问题:

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

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