zoukankan      html  css  js  c++  java
  • The web application registered the 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.

    最近使用了最新版的tomcat9,使用jdbc链接mysql数据库。关闭tomcat过程中出现警告

    13-Sep-2017 22:22:54.369 WARNING [main] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [license] 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.

    时间有限没有去追源码,网上的说法:

    tomcat6最新版本引入内存溢出检测阻止机制,检测到jdbc在tomcat运行时进行注册,但是当tomcat停止时没有解除注册。

    原因是在tomcat停止之前没注销驱动。

    网上有的方式是重写org.apache.commons.dbcp.BasicDataSource,这个不推荐。既然是容器级别的事件,那就从事件入手。

    @WebListener
    public class AppContextListener implements ServletContextListener{
    
        public void contextDestroyed(ServletContextEvent event)  {
             try{
                 while(DriverManager.getDrivers().hasMoreElements()){
                     DriverManager.deregisterDriver(DriverManager.getDrivers().nextElement());
                }
             }catch(Exception e){
                 e.printStackTrace();
             }
        }
    
        public void contextInitialized(ServletContextEvent event)  { 
              ServletContext context = event.getServletContext();
              String rootPath = context.getRealPath("/");
              System.setProperty("rootPath", rootPath);
              
              //logger.info("global setting,rootPath:{}",rootPath);
               //logger.info("deployed on architecture:{},operation System:{},version:{}",
                //       System.getProperty("os.arch"), System.getProperty("os.name"),
                    //   System.getProperty("os.version"));           
               //logger.info("app startup completed....");
        }
    }

    Tomcat在停止web应用的时候会调用contextDestroyed方法,加入你的项目,即可在tomcat关闭时注销已经注册的JDBC驱动。

    以上代码适用于servlet3.0 web容器,servlet 2.5容器需要在web.xml添加配置文件。

    servlet容器2.5用法

    public class AppContextListener implements ServletContextListener{
    
        public void contextDestroyed(ServletContextEvent event)  {
             try{
                 while(DriverManager.getDrivers().hasMoreElements()){
                     DriverManager.deregisterDriver(DriverManager.getDrivers().nextElement());
                }
             }catch(Exception e){
                 e.printStackTrace();
             }
        }
    
        public void contextInitialized(ServletContextEvent event)  { 
              ServletContext context = event.getServletContext();
              String rootPath = context.getRealPath("/");
              System.setProperty("rootPath", rootPath);
              
              //logger.info("global setting,rootPath:{}",rootPath);
               //logger.info("deployed on architecture:{},operation System:{},version:{}",
                //       System.getProperty("os.arch"), System.getProperty("os.name"),
                    //   System.getProperty("os.version"));           
               //logger.info("app startup completed....");
        }
    }

    web.xml

    <web-app>
      <listener>
            <listener-class>com.xxxx.init.AppContextListener</listener-class>
        </listener>
    </web-app>

    至此,问题解决。

  • 相关阅读:
    一线架构师实践指南阅读体会_需求之于架构
    Multi-modal Sentence Summarization with Modality Attention and Image Filtering 论文笔记
    开始用PyTorch
    【微软大法好】VS Tools for AI全攻略(4)——选择适合自己的虚拟机
    【零基础】【Fungus首个中文教程】10分钟快速构建Unity中的万能对话系统 / 叙事系统 / 剧情系统
    Unity使用脚本进行批量动态加载贴图
    【转】Python处理wave文件
    让linux远程主机在后台运行脚本
    【bug清除】新Surface Pro使用OneNote出现毛刺现象的解决方案
    【bug清除】Surface Pro系列使用Drawboard PDF出现手写偏移、卡顿、延迟现象的解决方式
  • 原文地址:https://www.cnblogs.com/passedbylove/p/7520410.html
Copyright © 2011-2022 走看看