1. 程式人生 > >registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped.

registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped.

配置錯誤 value ror been leak param cccccc 錯誤 tex

最近在用maven整合SSH做個人主頁時候,在eclipse裏面使用tomcat7插件發布項目是沒有問題的,但當打包成war之後,使用tomcat7單獨發布項目,就出現了以下的錯誤.

嚴重: Context [/wangxin] startup failed due to previous errors
八月 16, 2017 7:29:12 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
嚴重: The web application [/wangxin] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped
. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. 八月
16, 2017 7:29:12 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads 嚴重: The web application [/wangxin] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak. 八月
16, 2017 7:29:12 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads 嚴重: The web application [/wangxin] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] but has failed to stop it. This is very likely to create a memory leak. 八月
16, 2017 7:29:12 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads 嚴重: The web application [/wangxin] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1] but has failed to stop it. This is very likely to create a memory leak. 八月 16, 2017 7:29:12 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads 嚴重: The web application [/wangxin] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2] but has failed to stop it. This is very likely to create a memory leak. 八月 16, 2017 7:29:12 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads 嚴重: The web application [/wangxin] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak.

這個錯誤的意思是:在tomcat7啟動時候,tomcat自帶的一個內存檢查的監聽器發現,你的項目使用了一個數據庫的連接池,並沒有關閉的情況下,又去註冊了一個連接池,為了保證內存不泄露,所以不允許你項目進行部署.

理論上,使用了SSH框架去整合了數據庫,使用了jpa和c3P0連接池,是根本不需要去關心是要去關閉連接和GC的,但是還是報錯,對此,就很有疑問了.

網上給出的理論是,因為tomcat7采用了一個監聽技術,所以就會這樣,完全可以關閉.但事實上,我關閉了之後,雖然啟動不報錯了,但是還是部署不了,日誌裏面還是這個錯誤.

同樣的,網上還說了一個方法,就是把數據庫的jar包放到tomcat的lib下面,我試了還是沒用.

最後層層排查,發現,錯誤出現在了我的applicationContext.xml上,為了方便開發,我把applicationContext.xml裏面的jdbc方面全部提取出來,放到了一個性的applicationContext-jdbc.xml文件裏面,並且在總的xml裏面使用了<import>標簽去插入.

然後,我發現我的web.xml文件配置有點不同,即spring的監聽器的classpath配置錯誤

<context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:applicationContext*.xml</param-value>
</context-param>

我在classpath那邊配置多加了一個*這就導致了

正確的代碼:

<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
</context-param>

由於多配了一個*,這就導致了當啟動加載的時候,先加載了applicationContext-jdbc的內容,然後再加載applicationContext的內容,當加載appliactionContext的時候,再次去加載裏面<import>標簽導入的applicationContext-jdbc內容,這就造成了一次項目加載了兩次數據庫的連接池,因此造成了重復加載,項目無法啟動的問題.

對於web.xml的配置,在這裏我還得說一個非常騷氣的問題.

不知先前什麽情況,在WEF-INF下自動生成了一個classes文件夾,然後文件夾裏面竟然多了一個applicationContext文件(理論上編譯時候,生成到target裏面的WEF-INF文件夾下),這就造成了,當修改resource裏面的xml文件時候,沒有修改到classes裏面的文件,於是悲劇的一幕發生了:

使用SSH框架整合,使用SpringJPA整合的時候,寫了一個action,action裏面是使用的註解,註入的service,service裏面用註解註入的dao,dao只寫了一個接口,接口繼承了JpaRepository.

使用junit直接調用service裏面方法,能獲取到數據庫裏面的數據,而使用tomcat運行的話,調用action獲取不到數據.根本不報錯,使用debug發現,運行過程中,並沒有註入service以及dao.即都是null.

結果原因就是:在maven項目中的classes文件夾下面,自動將resource全部復制過來,而在mavenresource裏面的配置文件是修改過的,這就導致了,junit和tomcat運行時候采用的applicationContext.xml文件不同,導致了這個錯誤.而同時,在web.xml中,配置spring的監聽器使用的是

<param-value>classpath*:applicationContext.xml</param-value>

即加載所有classpath下面的xml文件,這就造成了一種情況,就是在junit時候能加載spring自動註入,而使用tomcat卻加載不了,同時還不會報錯....

就這兩個關於web.xml的錯誤,與君共勉!

registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped.