IT俱乐部 Tomcat 完美解决Tomcat关闭后报错问题

完美解决Tomcat关闭后报错问题

今天关闭Tomcat时报错

如下所示:

06-May-2019 17:10:13.543 警告 [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] registered the JDBC driver [com.alibaba.druid.proxy.DruidDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
06-May-2019 17:10:13.543 警告 [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [Druid-ConnectionPool-Create-316799830] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
java.util.concurrent.locks.AbstractQueuedSynchronizer ConditionObject.await( AbstractQueuedSynchronizer.java:2039 ) com.alibaba.druid.pool.DruidData Source ConditionObject.await(AbstractQueuedSynchronizer.java:2039) com.alibaba.druid.pool.DruidDataSource ConditionObject.await(AbstractQueuedSynchronizer.java:2039)com.alibaba.druid.pool.DruidDataSourceCreateConnectionThread.run(DruidDataSource.java:2443)

06-May-2019 16:00:49.348 警告 [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [ROOT] registered the JDBC driver [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
06-May-2019 15:39:25.116 严重 [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@770cddd]) and a value of type [java.lang.Class] (value [class oracle.sql.AnyDataFactory]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
06-May-2019 15:39:25.116 严重 [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@799b61b6]) and a value of type [java.lang.Class] (value [class oracle.sql.TypeDescriptorFactory]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

项目是SpringBoot+SSM+Oracle,报错的主要原因就是在关闭Tomcat时没有将一些资源释放出去,从而导致错误

解决方法

如下所示:

解决com.alibaba.druid.proxy.DruidDriver和Druid-ConnectionPool-Create报错

要编写如下类

import com.alibaba.druid.pool.DruidDataSource;
import com.smm.datasource.DynamicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
/**
 * @description: servlet容器销毁时,需要释放一些资源,避免报错
 * @author: songMingMing
 * @create: 2019-05-06 16:35
 */
@WebListener
public class  ContextFinalizeListener implements ServletContextListener  {
    private Logger logger = LoggerFactory.getLogger(testConfig.class);
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
    }
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        DynamicDataSource dynamicDataSource = DynamicDataSource.getInstance();
        Map dataSourceMap = dynamicDataSource.getDataSourceMap();
        Iterator> iterator = dataSourceMap.entrySet().iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next().getKey();
            DruidDataSource   dataSource = (DruidDataSource)dataSourceMap.get(key);
            //将dateSource关闭 解决了 org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [Druid-ConnectionPool-Create-316799830] but has failed to stop it. This is very likely to create a memory leak.
            dataSource.close();
        }
        //解决关闭Tomcat时报错 registered the JDBC driver [com.alibaba.druid.proxy.DruidDriver] 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关闭时,若这些连接未关 闭,tomcat会提示错误,但tomcat会帮我们关闭掉这些连接
        Enumeration drivers = DriverManager.getDrivers();
        while (drivers.hasMoreElements()) {
            java.sql.Driver driver = drivers.nextElement();
                try {
                    DriverManager.deregisterDriver(driver);
                } catch (SQLException ex) {
                    if (logger.isErrorEnabled()) {
                        logger.error("关闭连接池错误",ex);
                    }
                }
        }
    }
}

上面代码,因为我采用的是多数据源,

所有数据源信息DruidDateSource都是放在Map中的,所以获取遍历了map

解决Druid-ConnectionPool-Create,主要代码就是调用DruidDateSource.close就行了

解决oracle.jdbc.OracleDriver和ThreadLocal中的错误

在项目target目录下找到WEB-INFO下的lib,在lib中把ojdbc6.jar删除,当然你Tomcat中lib目录下要有ojdbc6.jar才行。

运行 ,没有报错信息, 完美解决!

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持IT俱乐部。

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/server/tomcat/12658.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部