最近使用了最新版的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. iOS移动开发CoreDate讲解

    ----欢迎------- 在移动端开发,数据持久化保存是基本要素,没钱在2014年之后退出了coredate,本持久化基于oc作为开发,方便程序人员操作.与SQL数据库,MySQL相比,优点颇多. ...

  2. Day71Django基础

    Django框架基础一 python中的web框架    a:socket    b:路由跟视图函数匹配关系    c:模板渲染    django:  a: 用了别人的wsgiref   b:自己写 ...

  3. JSON.stringify()的深度使用

    在使用JSON.stringify()对JSON数据进行序列化时 1> 如果里面的属性是function,则会被忽略 const data = { a: 'a', fn: funciton() ...

  4. python-web自动化-文件上传操作(非input标签的上传,需要借助第三方工具)

    文件上传操作 一.文件上传分两种情况:1. 如果是input可以直接输入路径的,可以直接调send_keys输入路径 2. 非input标签的上传,需要借助第三方工具:    2.1 Autolt 需 ...

  5. python爬虫爬取京东、淘宝、苏宁上华为P20购买评论

    爬虫爬取京东.淘宝.苏宁上华为P20购买评论 1.使用软件 Anaconda3 2.代码截图 三个网站代码大同小异,因此只展示一个 3.结果(部分) 京东 淘宝 苏宁 4.分析 这三个网站上的评论数据 ...

  6. 再见:org.apache.catalina.connector.ClientAbortException: java.io.IOException: Connection reset by peer

    这个问题我之前遇见多次了,今天又遇见了 2019-03-22 16:33:27.555 WebMvcConfigurer.java:144 ERROR - 接口 [/appservice/7za.ex ...

  7. ajax入门基础

    一.简介 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 是一种用于创建快速动态网页的技术. AJAX通过在后台与 ...

  8. makefile中打印变量名字,方便调试

    $(warning $(DVD_SERVICE)) // DVD_SerVICE是Makefile中的变量 $(warning   ST40_IMPORTS is $(ST40_IMPORTS)) 变 ...

  9. Spring MVC 注解之controller层

    第一层注解:@Controller 和 @RestController. 这两个注解的作用是:处理页面的HTTP请求,不同点 @RestController相当于@Controller +@Respo ...

  10. Ajax学习笔记——基本原理

    Ajax(Asynchronous JavaScript + XML)不是语音,不是框架,也不能算是一种技术,而是一种模式.通过这种模式实现不重新加载整个页面的情况下,与服务器交换数据并更新部分网页内 ...