The web application [] appears to have started a thread named [Abandoned connection cleanup thread] com.mysql.jdbc.AbandonedConnectionCleanupThread
Asked Answered
W

4

33

In the middle of my web-development I just close my web-app in my eclipse IDE, about a minute, I just saw a WARNING in my eclipse console.

WARNING: The web application [/Spring.MVC] 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.
Sep 06, 2014 8:31:55 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
WARNING: The web application [/Spring.MVC] 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. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(Unknown Source)
 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:40)
Sep 06, 2014 8:32:00 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Sep 06, 2014 8:32:00 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Sep 06, 2014 8:32:03 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: personPU
    ...]

It did not caused any memory leak so far, I checked my VisualVM, everything is working as usual, but as I search more about this thing I realize that this warning is caused by the MySQL driver not releasing resources or not being closed properly(I dont know how to say it exactly) and I ended up in this post at SO related issue

and the OP is right the answer of "don't worry about it" won't be sufficient. This warning bothers me because it may give me some future persistence problems and that worries me a lot, I tried the code the OP has written but I'm having a problem what Libraries should I use to make this code work. This is what I got sor far..

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.hibernate.annotations.common.util.impl.LoggerFactory;
import com.mysql.jdbc.AbandonedConnectionCleanupThread;
@WebListener
public class ContextFinalizer implements ServletContextListener {

private static final Logger LOGGER = LoggerFactory.getLogger(ContextFinalizer.class);

public void contextInitialized(ServletContextEvent sce) {
}

public void contextDestroyed(ServletContextEvent sce) {
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    Driver d = null;
    while (drivers.hasMoreElements()) {
        try {
            d = drivers.nextElement();
            DriverManager.deregisterDriver(d);
            LOGGER.warn(String.format("Driver %s deregistered", d));
        }
        catch (SQLException ex) {
            LOGGER.warn(String.format("Error deregistering driver %s", d), ex);
        }
    }
    try {
        AbandonedConnectionCleanupThread.shutdown();
    }
    catch (InterruptedException e) {
        logger.warn("SEVERE problem cleaning up: " + e.getMessage());
        e.printStackTrace();
    }
  }
}

I just want to know what Libraries do I need or If I'm using the right libraries to implement this properly, I do not even know what Logger should I use, thank you for any help,

Weikert answered 6/9, 2014 at 11:57 Comment(3)
The logger is not necessary to the solution. Did you run your code? What happened? Do you still see the warning?Interrogate
thank you for the response, I was not able to run it, I did not know that the logger isn't necessary for this code, I just restarted my computer before I try this, Ill post back in case something different will happen. thank you. :)Weikert
@SotiriosDelimanolis, I just finished trying the sample code above, without logger(thanks for that), and yes it is working, AbandonedConnectionCleanupThread is being shutdown, however, as I'm monitoring the server in my VisualVM, the permgen space is still being occupied and I'm still ending up with a memory leak, I still need to perform a manual GC. But as for the code, yes it is working.Weikert
G
32

See this answer. It seems that MySQL driver should be in {$TOMCAT]/lib shared between applications. Check that you are not including it with each application. At least it worked for me and I have been able to remove the warning.

If you are using Maven mark the dependency as provided.


UPDATE:
root cause is that Tomcat have problems to garbage collect the driver because it is registered in a singleton common to several applications. Closing one application does not allow Tomcat to release the driver. See this answer.

Garik answered 8/10, 2014 at 16:23 Comment(10)
Can anyone expand this a bit more? Is this a bug related to a specific jdbc driver version?Zygote
Hi mass. See the update. There are more information in the links from other stackoverflow answers.Garik
@Garik Marked the dependency as provided. but problem in starting server as i have configured db connection in servletcontextlistner. Any solution? Also done <scope>provided test</scope>, but failed.Inglenook
Have you added the MySQL driver to your tomcat libs?Garik
@Garik No. oh..I misunderstood provided. Now added. But it shows AbandonedConnectionCleanup.. on ContextDestroyed. Used provided scope for mysql-connector in pom.xmlInglenook
Was de jar added into $tomcat/lib? so that it is common to all apps?Garik
@Garik yes. apache-tomcat-8.0.37\lib\mysql-connector-java-5.1.33.jarInglenook
I have included <scope>provided</scope> in my pom.xml and I got this exception ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Cannot load JDBC driver class 'com.mysql.cj.jdbc.Driver'. I'm using hibernate5 so using this driver.Revue
@RamanujanR I have copied the mysql jar file from my m2 repository to tomcat lib folder and error is gone.Revue
I am using the mysql-connector-java version 8. It appears this has a dependency (mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.21), would I place the jar for the dependency in the [$TOMCAT]/lib folder as well? Or should that go in my application's classpath?Lapful
P
2

For me i stop mysql (service mysql start) and then i stop tomcat (./shutdown.sh) in bin folder and the problem was fixed.

Pride answered 2/7, 2020 at 10:41 Comment(0)
A
0

I fixed the error running the following command:

systemctl status tomcat10.service

It shows the specific error that was failing.

PS. I'm using a Debian OS, MariaDb and Tomcat10.

Apograph answered 21/11, 2023 at 21:27 Comment(0)
C
-5

This works for me.

Issue was with elastic search in my case.

Send a DELETE request from postman to below endpoint and restart your application .

http://localhost:9200/*
Cascabel answered 15/5, 2019 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.