To prevent a memory leak, the JDBC Driver has been forcibly unregistered

后端 未结 14 1448
执念已碎
执念已碎 2020-11-22 02:15

I am getting this message when I run my web application. It runs fine but I get this message during shutdown.

SEVERE: A web application registered the

14条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 03:18

    To prevent this memory leak, simply deregister the driver on context shutdown.

    pom.xml

    
    
        4.0.0
    
        com.mywebsite
        emusicstore
        1.0-SNAPSHOT
    
        
            
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    3.7.0
                    
                        1.9
                        1.9
                    
                
            
        
    
        
            
    
            
                org.hibernate
                hibernate-core
                4.0.1.Final
            
    
            
                org.hibernate.javax.persistence
                hibernate-jpa-2.0-api
                1.0.1.Final
            
    
            
            
                mysql
                mysql-connector-java
                8.0.11
            
    
            
            
                javax.servlet
                servlet-api
                2.5
                provided
            
        
    
    
    

    MyWebAppContextListener.java

    package com.emusicstore.utils;
    
    import com.mysql.cj.jdbc.AbandonedConnectionCleanupThread;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import java.sql.Driver;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Enumeration;
    
    public class MyWebAppContextListener implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            System.out.println("************** Starting up! **************");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            System.out.println("************** Shutting down! **************");
            System.out.println("Destroying Context...");
            System.out.println("Calling MySQL AbandonedConnectionCleanupThread checkedShutdown");
            AbandonedConnectionCleanupThread.checkedShutdown();
    
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
    
            Enumeration drivers = DriverManager.getDrivers();
            while (drivers.hasMoreElements()) {
                Driver driver = drivers.nextElement();
    
                if (driver.getClass().getClassLoader() == cl) {
                    try {
                        System.out.println("Deregistering JDBC driver {}");
                        DriverManager.deregisterDriver(driver);
    
                    } catch (SQLException ex) {
                        System.out.println("Error deregistering JDBC driver {}");
                        ex.printStackTrace();
                    }
                } else {
                    System.out.println("Not deregistering JDBC driver {} as it does not belong to this webapp's ClassLoader");
                }
            }
        }
    
    }
    

    web.xml

    
    
    
        
            com.emusicstore.utils.MyWebAppContextListener
        
    
    
    
    
    

    Source that inspired me for this bug fix.

提交回复
热议问题