Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat complaining about memory leak due to open connection after shut due to JDBC driver

I have a Servlet that overrides the init() method as follows:

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

And BookDAO#findAllCategories is:

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

}

I simply hit this servlet so it gets loaded. There is nothing else db-wise. When I shutdown Tomcat I get:

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

What is it that I am missing?

like image 403
Koray Tugay Avatar asked Oct 02 '14 19:10

Koray Tugay


1 Answers

It appears you are having this problem:

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

Specifically, you are including your jdbc driver as part of the war instead of the tomcat lib folder.

like image 104
Zeki Avatar answered Nov 07 '22 06:11

Zeki