Logging issue: Critical: The web application registered the JDBC driver [com.mysql.cj.jdbc.Driver] but could not unregister it when the web application stopped

Set up a listening class to implement the contextDestroyed method in ServletContextListener when the servlet logs out.

The specific code is as follows

package com.demo.myssm.myspringmvc;

import com.mysql.cj.jdbc.AbandonedConnectionCleanupThread;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;

/**
 * JDBC监听类
 */
@WebListener
public class MyContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("webService start");
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("webService stop");
        Enumeration<Driver> drivers = DriverManager.getDrivers();
        Driver d = null;
        try {
            while(DriverManager.getDrivers().hasMoreElements()) {
                d=DriverManager.getDrivers().nextElement();
                DriverManager.deregisterDriver(d);
                System.out.println(" 消除数据库连接驱动 --> : Driver "+d+" deregistered");
            }
        } catch (SQLException ex) {
            System.out.println("Error: deregister driver "+d+" exceptionName:"+ex.getClass().getName()+" detail:"+ex.getMessage());
        }finally {
            System.out.println("jdbc Driver close");
            AbandonedConnectionCleanupThread.uncheckedShutdown();
            System.out.println("clean thread success");
        }
    }
}

What needs to be noted here is that AbandonedConnectionCleanupThread.uncheckedShutdown(); the mysql version of this line of code is the 8.0 series

If the database of the mysql5.0 series should call the shutdown method directly, the code line should be AbandonedConnectionCleanupThread.shuthutdown();

Guess you like

Origin blog.csdn.net/m0_62945506/article/details/122021226