最近使用了最新版的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. substr函数学习

    今天写了单词接龙这道恶心题,在想有没有函数能直接去返回string类型一个区间的字符串,没想到还真有,那就是sudstr 坑点 感觉这个函数有点逗比-- 别的都是str在前,这个偏要在后-- 也许是我 ...

  2. factory di

    services.AddScoped(typeof(Test)); //services.AddScoped<Test>() // .AddScoped<ITest, Test> ...

  3. vue中提交表单后如何清空

    只需要在提交方法里写上this.form={brand_right:0}即可.

  4. 【Appium自学】Android studio安装与配置(转)

    转自链接:https://www.cnblogs.com/xiadewang/p/7820377.html 1.首先下载Android studio安装包. 可以从http://www.android ...

  5. python3使用pymysql库连接MySQL的常用操作

    #导入pymysql模块import pymysql #连接数据库connect = pymysql.connect( host='localhost', port=3306, user='root' ...

  6. 验证demo

    // chenwenjun.cpp : 定义控制台应用程序的入口点.//#include "stdafx.h"#include <iostream>#include & ...

  7. 简单的PHP上传图片和删除图片示例代码

    分享一例简单的PHP上传图片和删除图片示例代码,很简单,适合初学的朋友参考,用来研究php上传图片还是不错的. 1.php上传图片: <?php if (!empty($_FILES[" ...

  8. MySQL(进阶部分)

    视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( SEL ...

  9. ThreadLocal 原理及一些实现

    ThreadLocal = TL 网上讲TL原理很多,我大概说下自己的理解 TL其实是不是有点像全局的配置中心,static ConcurrentHashMap<Thread,value> ...

  10. vue缓存页面之后的生命周期

    一:<router-view :key="key"></router-view> 没有作缓存时的状态 created :某单页面刚刚创建时候的回掉函数. m ...