java.net.SocketException異常的可能性分析
近來在做一個MQ的項目,當部署完畢,運行後每隔兩三天就會down機一次,軟體死了。通過系統日誌分析:
InforSuiteV6.0.0 Build2009042101 started
2010-3-29 15:58:36 ClientCommunicatorAdmin restart
警告: Failed to restart: java.io.IOException: Failed to get a RMI stub: javax.na
ming.CommunicationException [Root exception is java.rmi.ConnectIOException: Exce
ption creating connection to: 127.0.0.1; nested exception is:
java.net.SocketException: No buffer space available (maximum connections
reached?): JVM_Bind]
2010-3-29 15:58:36 RMIConnector RMIClientCommunicatorAdmin-doStop
警告: Failed to call the method close():java.rmi.ConnectIOException: Exception c
reating connection to: 10.0.0.2; nested exception is:
java.net.SocketException: No buffer space available (maximum connections
reached?): JVM_Bind
2010-3-29 15:58:36 ClientCommunicatorAdmin Checker-run
警告: Failed to check connection: java.net.SocketException: No buffer space avai
lable (maximum connections reached?): JVM_Bind
2010-3-29 15:58:36 ClientCommunicatorAdmin Checker-run
警告: stopping
2010-3-29 15:58:52 ClientCommunicatorAdmin restart
警告: Failed to restart: java.io.IOException: Failed to get a RMI stub: javax.na
ming.CommunicationException [Root exception is java.rmi.ConnectIOException: Exce
ption creating connection to: 127.0.0.1; nested exception is:
java.net.SocketException: No buffer space available (maximum connections
reached?): JVM_Bind]
發現拋出了一些Socket異常,這些異常均是由檔案控制代碼使用完畢而引起的。也就是說程式裡面開啟的socket,沒有及時關閉掉。為了進一步尋求支援人員,確認分析是否合理,尋找sun的網站資料:
Chances are you are forgetting to close a socket, a database connection, or some other connection that uses sockets internally. See example program below.
The alternative is that your program (or the sum of all programs running on your computer) really needs a lot of connections. In this case you'll need to find out how to increase the amount of socket buffer space that your operating system allocates. I'd start by googling for instructions. Or maybe you could redesign your program so that it doesn't use so much resources.
Sun的支援人員確認了之前分析的有理。java.net.SocketException異常與socket沒有關閉有關,作業系統有它允許持有的最大檔案控制代碼數,而在網路連接的過程中,每個socket請求都要佔用一個檔案控制代碼資源,如果沒有及時釋放,則可能會耗盡檔案控制代碼資源.
所以,及時關閉那些不再使用的socket,能夠合理的解決拋出的java.net.SocketException異常。