完美解決Tomcat關(guān)閉后報錯問題
今天關(guān)閉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,報錯的主要原因就是在關(guān)閉Tomcat時沒有將一些資源釋放出去,從而導(dǎo)致錯誤
解決方法
如下所示:
解決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關(guān)閉 解決了 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();
}
//解決關(guān)閉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添加了一些對數(shù)據(jù)庫連接的監(jiān)聽,當tomcat關(guān)閉時,若這些連接未關(guān) 閉,tomcat會提示錯誤,但tomcat會幫我們關(guān)閉掉這些連接
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("關(guān)閉連接池錯誤",ex);
}
}
}
}
}上面代碼,因為我采用的是多數(shù)據(jù)源,
所有數(shù)據(jù)源信息DruidDateSource都是放在Map中的,所以獲取遍歷了map
解決Druid-ConnectionPool-Create,主要代碼就是調(diào)用DruidDateSource.close就行了
解決oracle.jdbc.OracleDriver和ThreadLocal中的錯誤
在項目target目錄下找到WEB-INFO下的lib,在lib中把ojdbc6.jar刪除,當然你Tomcat中l(wèi)ib目錄下要有ojdbc6.jar才行。
運行 ,沒有報錯信息, 完美解決!
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用TomCat,service輸出臺出現(xiàn)亂碼的解決
本文介紹了解決Tomcat服務(wù)輸出臺中文亂碼問題的兩種方法,第一種方法是修改`logging.properties`文件中的`prefix`和`encoding`參數(shù);第二種方法是配置IDEA的VMoptions和文件編碼設(shè)置,通過這兩種方法,可以有效解決Tomcat服務(wù)輸出臺出現(xiàn)的亂碼問題2025-01-01
Tomcat的安裝與使用及Maven與Servlet的使用教程
這篇文章主要介紹了Tomcat的安裝與使用及Maven與Servlet的使用教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
Tomcat Server的設(shè)計和實現(xiàn):StandardServer詳解
文章主要介紹了Tomcat中StandardServer的整體類依賴結(jié)構(gòu),通過解析server.xml文件來理解Server的相關(guān)配置屬性及其作用,感興趣的朋友跟隨小編一起看看吧2026-04-04
關(guān)于tomcat點擊startup.bat后閃退問題的解決辦法
這篇文章給大家分享了關(guān)于tomcat點擊startup.bat后閃退問題分析及解決辦法,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-01-01
查看tomcat并發(fā)連接數(shù)實現(xiàn)方式
文章介紹了兩種查看Tomcat并發(fā)連接數(shù)的方法:一是通過Tomcat管理控制臺,需配置賬號并登錄后查看;二是使用命令行實時查詢,并說明了相關(guān)端口設(shè)置和操作權(quán)限要求2025-10-10
tomcat自定義Web部署文件中docBase和workDir的區(qū)別介紹
這篇文章主要給大家介紹了關(guān)于tomcat自定義Web部署文件中docBase和workDir的區(qū)別,文中介紹的很詳細,有需要的可以參考借鑒,下面來一起看看吧。2016-12-12

