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

Kor*_*gay 5 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);
}
Run Code Online (Sandbox Code Playgroud)

而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;

}
Run Code Online (Sandbox Code Playgroud)

我只是点击这个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
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Zek*_*eki 6

看来你遇到了这个问题:

tomcat7 - jdbc datasource - 这很可能会造成内存泄漏

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