完美解决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 C o n d i t i o n O b j e c t . a w a i t ( A b s t r a c t Q u e u e d S y n c h r o n i z e r . j a v a : 2039 ) c o m . a l i b a b a . d r u i d . p o o l . D r u i d D a t a S o u r c e 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<Object, Object> dataSourceMap = dynamicDataSource.getDataSourceMap();
        Iterator<Map.Entry<Object, Object>> 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<java.sql.Driver> 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才行。
运行 ,没有报错信息, 完美解决.

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值