Tomcat Memory Leak: Resolving JDBC Driver and MySQL Statement Cancellation Timer Errors
In Tomcat 7.x, users may encounter memory leak warnings in the catalina.out log upon shutdown. These warnings indicate that the JDBC driver failed to unregister properly and that a thread named "MySQL Statement Cancellation Timer" is still running.
JDBC Driver Unregistration Error
Despite configuring the destroy-method for the DataSource, the error message suggests that the JDBC driver (com.mysql.jdbc.Driver) was not unregistered by the web application. As noted in the error message, Tomcat will forcibly unregister the driver to prevent a memory leak.
To prevent this issue, it is recommended to move the MySQL JDBC connector/driver to the tomcat/lib folder instead of including it in the WAR. This ensures that the connector/driver is loaded only once by the Tomcat classloader, avoiding multiple instantiations and ensuring proper cleanup upon shutdown.
MySQL Statement Cancellation Timer Thread Error
The "MySQL Statement Cancellation Timer" thread is created by the JDBC driver to cancel outstanding statements when the connection is closed. If this thread is not stopped properly, it can lead to a memory leak.
To resolve this issue, ensure that the JDBC connection is properly closed before the web application is stopped. This can be achieved by using a finally block or by using a try-with-resources statement.
Additional Considerations
The above is the detailed content of Why Does Tomcat Experience Memory Leaks with JDBC Drivers and MySQL Statement Cancellation Timers?. For more information, please follow other related articles on the PHP Chinese website!