最近使用了最新版的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>

至此,问题解决。

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.的更多相关文章

  1. The web application [ ] registered the JDBC driver [net.sourceforge.jtds.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver

    出现以下错误时,我找了很多方法,都未解决,网上有很多,最后我实在无奈,怀疑会不会是Tomcat的原因,更换了一个版本之后就好了.The web application [ ] registered t ...

  2. 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.

    问题是tomcat的版本问题,tomcat新检测机制导致的这个问题,换版本可以解决问题,但不建议这么做,租用服务器不是你说换就换的.其实问题根源是BasicDataSource,BasicDataSo ...

  3. 解决: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.

    问题描述 在将Spring Boot程序打包生成的war包部署到Tomcat后,启动Tomcat时总是报错,但是直接在IDEA中启动Application或者用"java -jar" ...

  4. registered the JBDC driver [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped. (转)

    最近项目中遇见一问题,在开发环境没有问题的代码,到了生产环境就会报如下错误:   严重: A web application registered the JBDC driver [oracle.jd ...

  5. 严重: The web application [] registered the JDBC driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDB

    idea项目启动报如下错误, 网上的方法都试了都没用, 一直没解决, 干掉项目, 重新从svn检出就好了...坑 啊 Root WebApplicationContext: initializatio ...

  6. registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped.

    最近在用maven整合SSH做个人主页时候,在eclipse里面使用tomcat7插件发布项目是没有问题的,但当打包成war之后,使用tomcat7单独发布项目,就出现了以下的错误. 严重: Cont ...

  7. The web application [/codeMarket] registered the JBDC driver[.........] but failed to unregister it when the web application was stopped. To prevent

    如果你报错了上面的这个严重,那么你的tomcat版本一定是在6.0.25之上的 原因:tomcat 6.025以后在sever.xml中引入了内存泄露侦测,对于垃圾回收不能处理的对像,它就会做日志. ...

  8. [tomcat启动报错]registered the JDBC driver [com.alibaba.druid.proxy.DruidDriver] but failed to unregister it when the web application was stopped

    环境:一个tomcat ,一个工程配置了多数据源,在启动的时候报如下错误: SEVERE: The web application [/qdp-resource-job] registered the ...

  9. Tomcat运行一段时间后,自动停止关闭,To prevent a memory leak,Druid 数据库连接自动关闭, the JDBC Driver has been forcibly unregistered.

    1. Tomcat 错误日志 tail -100f tomcat9/logs/catalina.out 21-Sep-2017 23:05:39.301 INFO [Thread-5] org.apa ...

随机推荐

  1. [STM32F103]DMA原理

    DMA配置程序过程 使能DMA时钟 a) RCC_AHBPeriphClockCmd(); 初始化DMA通道参数 a) DMA_Init(); 使能串口DMA发送,串口DMA使能函数: a) USAR ...

  2. C#windows桌面应用小程序制作——大文件数据分段解析存储

    现在的任务就是做一个大文件解析的桌面应用小程序,具体需求就是:将一个很大的文件里的数据按一定标志拆分然后分别保存到某个文件夹下面. 解析的文件内容为以下内容: windows 应用小程序界面 具体代码 ...

  3. LayaAir疑难杂症之三:1.7版本click()、execCommand('copy')函数不生效

    问题描述 在使用Laya1.7引擎开发H5游戏时,引入Js原生函数click( ),模拟一次点击事件,发现无效.在使用Laya1.7引擎开发H5游戏时,引入Js原生函数execCommand('cop ...

  4. Spring Security开发Restful服务

    2-1开发环境安装 1.jdk8安装 2.安装STS    Spring Tool Suite实际上就是一个eclipse,只不过在此基础上又安装了一些插件 3.安装mysql 2-2代码结构介绍 打 ...

  5. chrome 如何开启网页另存为.mhtml 功能

    打开chrome浏览器,输入地址:chrome://flags/   找到将网页另存为MHTML,点击启用就可以了. 或者直接输入:chrome://flags/#save-page-as-mhtml

  6. CI/CD

    CI/CD 啥是CI/CD CI: continuous integration, 持续集成.就是频繁地把开发的工作提交到主线代码.主要是为了解决集成问题.什么是集成问题呢,白话说,就是从你本地的代码 ...

  7. Ionic框架搭建简明教程

    1.安装node.js 安装教程:https://www.cnblogs.com/zhouyu2017/p/6485265.html 安装完成后,执行:cnpm install –g cordova ...

  8. python大法好——多线程

    Python 多线程 多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件 ...

  9. LNMP 支持路由重写

    server    {        listen 80;        #listen [::]:80;        server_name www.xxx.com;        index i ...

  10. Hibernate 中出现 users is not mapped 问题

    Hibernate 中出现 users is not mapped 问题: 解答:HQL语句中表名应该是ORM映射的类名,所以应该改成:  (如果是用注解生成实体类,那就是注解的那个类)String ...