完美解決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<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添加了一些對數(shù)據(jù)庫連接的監(jiān)聽,當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); } } } } }
上面代碼,因為我采用的是多數(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
直接雙擊啟動tomcat中的startup.bat閃退原因及解決方法
免安裝的tomcat雙擊startup.bat后,啟動窗口一閃而過,而且tomcat服務未啟動,下面與大家分享下原因及解決方法2014-08-08Tomcat執(zhí)行startup.bat出現(xiàn)閃退的可能原因及解決
本文主要介紹了Tomcat執(zhí)行startup.bat出現(xiàn)閃退的可能原因及解決,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04