java.net.SocketException: No buffer space available

  • It certainly sounds like you are leaking Sockets somehow in your app. 这句话意思就是开了socket,没有关

  • Check that your code always closes the Sockets it opens … even in the event of some exception; i.e. do the close in a finally block.

state 介绍

  • TIME_WAIT:表示主动关闭,通过优化系统内核参数可容易解决。
  • CLOSE_WAIT:表示被动关闭,需要从程序本身出发。
  • ESTABLISHED:表示正在通信

解决办法

下次出现这种情况的时候,
netstat -apn | grep java
netstat -apn | grep 8983
netstat -nap | grep pid
检查一下本机java程序占用了多少端口,主要是跟8983的连接还是5000的连接?

端口占用状态

某次的结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
tcp        0      0 127.0.0.1:63342         0.0.0.0:*               LISTEN      8959/java
tcp 0 0 127.0.0.1:6942 0.0.0.0:* LISTEN 8959/java
tcp 0 0 0.0.0.0:54152 0.0.0.0:* LISTEN 8959/java
tcp6 0 0 127.0.0.1:7983 :::* LISTEN 23705/java
tcp6 0 0 :::8080 :::* LISTEN 32668/java listen状态,是本地tomcat启动的项目,监听8080
tcp6 0 0 :::8983 :::* LISTEN 23705/java listen状态,是本地jetty启动的solr,监听8983端口
tcp6 0 0 :::53056 :::* LISTEN 9342/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 32668/java
tcp6 0 0 :::8009 :::* LISTEN 32668/java
tcp6 1 0 127.0.0.1:36061 127.0.0.1:8983 CLOSE_WAIT 32668/java tomcat启动的项目,向solr发送socket连接
tcp6 1 0 127.0.0.1:36060 127.0.0.1:8983 CLOSE_WAIT 32668/java
tcp6 1 0 127.0.0.1:36062 127.0.0.1:8983 CLOSE_WAIT 32668/java
tcp6 0 0 9.186.100.11:57211 151.101.40.215:80 ESTABLISHED 9342/java
tcp6 0 0 9.186.100.11:46481 151.101.44.215:80 ESTABLISHED 9342/java
tcp6 0 0 9.186.100.11:46483 151.101.44.215:80 ESTABLISHED 9342/java

tcp6 0 0 9.186.100.11:46482 151.101.44.215:80 ESTABLISHED 9342/java

tcp6 0 0 9.186.100.11:46484 151.101.44.215:80 ESTABLISHED 9342/java

某次的结果

1
2
3
4
5
6
7
8
9
10
11
12
13
tcp6       0      0 :::8080                 :::*                    LISTEN      75106/java
tcp6 0 0 :::8983 :::* LISTEN 74867/java

tomcat启动的项目,向solr发送socket连接,怎么变这么多了?
会存在多个线程采用同一个单例,向solr发送socket连接
tcp6 1 0 127.0.0.1:48876 127.0.0.1:8983 CLOSE_WAIT 75106/java close的socket连接
tcp6 1 0 127.0.0.1:45752 127.0.0.1:8983 CLOSE_WAIT 75106/java close的socket连接
tcp6 1 0 127.0.0.1:48925 127.0.0.1:8983 CLOSE_WAIT 75106/java
tcp6 1 0 127.0.0.1:48729 127.0.0.1:8983 CLOSE_WAIT 75106/java
tcp6 1 0 127.0.0.1:48714 127.0.0.1:8983 CLOSE_WAIT 75106/java
tcp6 1 0 127.0.0.1:48924 127.0.0.1:8983 CLOSE_WAIT 75106/java
tcp6 1 0 127.0.0.1:48763 127.0.0.1:8983 CLOSE_WAIT 75106/java
tcp6 1 0 127.0.0.1:48764 127.0.0.1:8983 CLOSE_WAIT 75106/java

过一段时间会有变化,下面标红色的,是新增的两个socket连接,

1
2
3
4
5
6
7
8
tcp6       1      0 127.0.0.1:48925         127.0.0.1:8983          CLOSE_WAIT  75106/java
tcp6 1 0 127.0.0.1:48729 127.0.0.1:8983 CLOSE_WAIT 75106/java
tcp6 1 0 127.0.0.1:56303 127.0.0.1:8983 CLOSE_WAIT 75106/java 新增的socket连接
tcp6 1 0 127.0.0.1:48714 127.0.0.1:8983 CLOSE_WAIT 75106/java
tcp6 1 0 127.0.0.1:48924 127.0.0.1:8983 CLOSE_WAIT 75106/java
tcp6 1 0 127.0.0.1:48763 127.0.0.1:8983 CLOSE_WAIT 75106/java
tcp6 1 0 127.0.0.1:56302 127.0.0.1:8983 CLOSE_WAIT 75106/java 新增的socket连接
tcp6 1 0 127.0.0.1:48764 127.0.0.1:8983 CLOSE_WAIT 75106/java

solrSearch.query(queryParameters); 会建立连接 默认采用GET方式发送请求
还是 new HttpSolrClient(URL) 的尚未建立连接,只是做了一下config。

update的时候
solr.add(doc) send the SolrInputDocument to the Solr update request handler over HTTP.