通過(guò)自己親自對(duì)各種方案進(jìn)行測(cè)試,目前我認(rèn)為能解決這個(gè)問(wèn)題的最優(yōu)辦法是(直接貼代碼):
那么如何測(cè)試這個(gè)方案是否解決了問(wèn)題?
第一、將MySQL的空閑時(shí)間設(shè)置短一些。設(shè)置方法:找到MySQL安裝目錄下的my.ini文件,用記事本或UE或notepad++工具打開(kāi),在[mysqld]節(jié)點(diǎn)中加入以下設(shè)置:
interactive_timeout=120
wait_timeout=120
設(shè)置為2分鐘,網(wǎng)上有些只提出了設(shè)置wait_timeout即可,但我在測(cè)試時(shí),這個(gè)設(shè)置無(wú)效,仍然是8小時(shí),我就將interactive_timeout也設(shè)置為2分鐘,我目前不知道為什么,希望有知道能分享一下。設(shè)置完畢,重啟MySQL服務(wù)。
打開(kāi)MySQL命了窗口,輸入show variables like '%timeout%';回車(chē),即可查看是否設(shè)置生效,如果未生效,還是28800,則檢查是否同時(shí)也設(shè)置了interactive_timeout。
第二、在C3P0的配置中,我們注釋掉上面貼出的配置代碼,啟動(dòng)程序,然后打開(kāi)自己編寫(xiě)的系統(tǒng)或網(wǎng)站等,2分鐘后隨便做一個(gè)與數(shù)據(jù)庫(kù)有關(guān)的操作,沒(méi)錯(cuò),就會(huì)出現(xiàn)下面的異常:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
The last packet successfully received from the server was 661,156 milliseconds ago. The last packet sent successfully to the server was 661,157 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.;
SQL []; The last packet successfully received from the server was 661,156 milliseconds ago. The last packet sent successfully to the server was 661,157 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.;
nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 661,156 milliseconds ago. The last packet sent successfully to the server was 661,157 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
這個(gè)異常是不是很熟悉,不解釋,你們懂的。第三、加上上面C3P0的配置,注意,由于設(shè)置了MySQL的空閑時(shí)間是2分鐘,因此我們要將配置文件中idleConnectionTestPeriod的value設(shè)置為1分鐘,即60,不管設(shè)置多長(zhǎng),總之要小于MySQL的空閑時(shí)間。最后重啟程序,再次測(cè)試,OK,問(wèn)題解決了。
大功告成!?。?!文字描述得不是很美,程序猿吧,懂的。
總結(jié):解決問(wèn)題的關(guān)鍵在于找到原因,只要了解其他連接池的配置,相信也能很快解決這個(gè)問(wèn)題。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com