完美解決Tomcat關(guān)閉后報(bào)錯(cuò)問題
今天關(guān)閉Tomcat時(shí)報(bào)錯(cuò)
如下所示:
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 嚴(yán)重 [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 嚴(yán)重 [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.
項(xiàng)目是SpringBoot+SSM+Oracle,報(bào)錯(cuò)的主要原因就是在關(guān)閉Tomcat時(shí)沒有將一些資源釋放出去,從而導(dǎo)致錯(cuò)誤
解決方法
如下所示:
解決com.alibaba.druid.proxy.DruidDriver和Druid-ConnectionPool-Create報(bào)錯(cuò)
要編寫如下類
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容器銷毀時(shí),需要釋放一些資源,避免報(bào)錯(cuò) * @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時(shí)報(bào)錯(cuò) 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添加了一些對(duì)數(shù)據(jù)庫連接的監(jiān)聽,當(dāng)tomcat關(guān)閉時(shí),若這些連接未關(guān) 閉,tomcat會(huì)提示錯(cuò)誤,但tomcat會(huì)幫我們關(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)閉連接池錯(cuò)誤",ex); } } } } }
上面代碼,因?yàn)槲也捎玫氖嵌鄶?shù)據(jù)源,
所有數(shù)據(jù)源信息DruidDateSource都是放在Map中的,所以獲取遍歷了map
解決Druid-ConnectionPool-Create,主要代碼就是調(diào)用DruidDateSource.close就行了
解決oracle.jdbc.OracleDriver和ThreadLocal中的錯(cuò)誤
在項(xiàng)目target目錄下找到WEB-INFO下的lib,在lib中把ojdbc6.jar刪除,當(dāng)然你Tomcat中l(wèi)ib目錄下要有ojdbc6.jar才行。
運(yùn)行 ,沒有報(bào)錯(cuò)信息, 完美解決!
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
直接雙擊啟動(dòng)tomcat中的startup.bat閃退原因及解決方法
免安裝的tomcat雙擊startup.bat后,啟動(dòng)窗口一閃而過,而且tomcat服務(wù)未啟動(dòng),下面與大家分享下原因及解決方法2014-08-08Tomcat執(zhí)行startup.bat出現(xiàn)閃退的可能原因及解決
本文主要介紹了Tomcat執(zhí)行startup.bat出現(xiàn)閃退的可能原因及解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Tomcat配置https SSL證書的項(xiàng)目實(shí)踐
本文主要介紹了Tomcat配置https SSL證書的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Idea部署tomcat服務(wù)實(shí)現(xiàn)過程圖解
這篇文章主要介紹了Idea部署tomcat服務(wù)實(shí)現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08非常實(shí)用的Tomcat啟動(dòng)腳本實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于非常實(shí)用的Tomcat啟動(dòng)腳本的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Tomcat具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05解決啟動(dòng)tomcat報(bào)錯(cuò)發(fā)生服務(wù)特定錯(cuò)誤1的問題
這篇文章主要介紹了解決啟動(dòng)tomcat報(bào)錯(cuò)發(fā)生服務(wù)特定錯(cuò)誤1的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12解決Tomcat重新部署后圖片等資源被自動(dòng)刪除的問題
這篇文章主要介紹了解決Tomcat重新部署后圖片等資源被自動(dòng)刪除的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11