頻繁連接斷開mysql出現的問題

今天寫的程序需要頻繁執行連接斷開mysql這一操作,而且時間間隔又很短,於是乎出現了下面這個問題

The driver was unable to create a connection due to an inability to establish the client portion of a socket.
This is usually caused by a limit on the number of sockets imposed by the operating system. This limit is usually configurable.
For Unix-based platforms, see the manual page for the 'ulimit' command. Kernel or system reconfiguration may also be required.
For Windows-based platforms, see Microsoft Knowledge Base Article 196271 (Q196271).  

意思是說本地的套接字緩存不夠了,因此獲取不到數據庫的連接,可參考這篇文章:http://support2.microsoft.com/kb/196271。

爲什麼套接字會不夠了?因爲,我剛纔提到了,執行sql操作的時間間隔很短,但是在默認情況下,windows允許使用5000個臨時TCP端口,任何端口關閉後,它將在TIME_WAIT狀態下保持120秒,所以理論上講如果 時間間隔 < 120/5000 的話,就會出現套接字用光的問題。

我的解決方法是:

1.使用連接池

2.每次執行完一個操作後再執行下Thread.sleep(200) 即可

發佈了152 篇原創文章 · 獲贊 0 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章