"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Prevent JDBC Datasource Memory Leaks in Tomcat 7?

How to Prevent JDBC Datasource Memory Leaks in Tomcat 7?

Published on 2024-11-20
Browse:238

How to Prevent JDBC Datasource Memory Leaks in Tomcat 7?

JDBC Datasource Memory Leak in Tomcat 7

When shutting down Tomcat 7 using the JDBC datasource, users may encounter a warning message similar to the below:

SEVERE: The web application [/my_webapp] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak.

Addressing the JDBC Driver Registration Issue

To resolve the issue related to unregistering the JDBC driver, ensure that the destroy-method is properly configured within the element in context.xml. The correct configuration should be:

Resolving the MySQL Statement Cancellation Timer Error

To address the error related to the MySQL Statement Cancellation Timer thread, follow these steps:

  1. Move the MySQL Connector/Driver to tomcat/lib: Place the MySQL Connector/Driver JAR file in the tomcat/lib directory instead of deploying it within the WAR file. This prevents the creation of multiple instances of the driver upon each WAR deployment.
  2. Add a Shutdown Hook to Tomcate: Introduce a shutdown hook to gracefully close the thread. Edit the web.xml file and add the following initialization parameter:
shutdownHookcom.example.MyShutdownHook

Create the MyShutdownHook class to execute upon Tomcat shutdown:

public class MyShutdownHook implements Shutdownable {
    @Override
    public void shutdown() {
        // Logic to properly close the MySQL Statement Cancellation Timer thread
    }
}

By implementing the above solutions, memory leaks related to JDBC datasource usage and the MySQL Statement Cancellation Timer issue can be effectively mitigated in Tomcat 7.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3