tomcat关闭时jdbc驱动没有解除注册以及mysql的一个线程没有关闭

今天关闭tomcat时,发现日志有下面两个警告:

27-Feb-2017 15:23:52.919 WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [ROOT] 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.
27-Feb-2017 15:23:52.920 WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] 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(ReferenceQueue.java:143) com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)

查了下之后发现需要手动解除注册以及关闭mysql的AbandonedConnectionCleanupThread线程

log.info("Destroying Context...");

try {
    log.info("Calling MySQL AbandonedConnectionCleanupThread shutdown");
    com.mysql.jdbc.AbandonedConnectionCleanupThread.shutdown();

} catch (InterruptedException e) {
    log.error("Error calling MySQL AbandonedConnectionCleanupThread shutdown {}", e);
}

ClassLoader cl = Thread.currentThread().getContextClassLoader();

Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
    Driver driver = drivers.nextElement();

    if (driver.getClass().getClassLoader() == cl) {

        try {
            log.info("Deregistering JDBC driver {}", driver);
            DriverManager.deregisterDriver(driver);

        } catch (SQLException ex) {
            log.error("Error deregistering JDBC driver {}", driver, ex);
        }

    } else {
        log.trace("Not deregistering JDBC driver {} as it does not belong to this webapp's ClassLoader", driver);
    }
} 
              

参考:

https://github.com/spring-projects/spring-boot/issues/2612

http://docs.oracle.com/cd/E17952_01/connector-j-relnotes-en/news-5-1-23.html