java.net.SocketException: No buffer space available (maximum connections reached?): connect

最近在项目中使用HttpClient时,遇到这样的问题,异常信息如下:

信息: Retrying connect
2013-1-23 16:36:31 org.apache.http.impl.client.DefaultRequestDirector tryConnect
信息: I/O exception (java.net.SocketException) caught when connecting to the target host: No buffer space available (maximum connections reached?): connect
2013-1-23 16:36:31 org.apache.http.impl.client.DefaultRequestDirector tryConnect
信息: Retrying connect
java.net.SocketException: No buffer space available (maximum connections reached?): connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:127)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:645)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at com.yulore.json.MysqlService.parseJsonShop(MysqlService.java:118)
at com.yulore.json.MysqlService.writeToMySql(MysqlService.java:68)
at com.yulore.json.MysqlMainTest.main(MysqlMainTest.java:13)
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 


** BEGIN NESTED EXCEPTION ** 


java.net.SocketException
MESSAGE: No buffer space available (maximum connections reached?): connect


STACKTRACE:


java.net.SocketException: No buffer space available (maximum connections reached?): connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:218)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at com.yulore.json.db.DBManager.getConnectionWithJDBC(DBManager.java:39)
at com.yulore.json.db.impl.GlossaryDaoImpl.insert(GlossaryDaoImpl.java:20)
at com.yulore.json.MysqlService.writeToMySql(MysqlService.java:89)
at com.yulore.json.MysqlMainTest.main(MysqlMainTest.java:13)




** END NESTED EXCEPTION **


最后Google、Baidu发现是自己创建了HttpGet发送Get请求,最后没有释放连接,更正后的代码如下

private List<Shop> parseJsonShop(String url) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);
		HttpResponse response;
		try {
			response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();

			if (entity != null) {

				InputStream in = entity.getContent();

				String result = convertStreamToString(in);
				in.close();

				JSONObject obj = new JSONObject(result);

				JSONObject resp = obj.getJSONObject("response");
				numFound = resp.getLong("numFound");
				// System.out.println("numFound="+numFound);

				// JSONArray docs = resp.getJSONArray("docs");
				String docs = resp.getString("docs");
				// System.out.println("docs="+docs);

				List<Shop> shopList = JSON.parseArray(docs, Shop.class);
				return shopList;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpget.releaseConnection(); // 释放连接
		}
		return null;
	}


使用完HttpGet之后记得要释放掉连接,否则它会继续占用资源。为了保证连接一定会被释放掉建议 把它放到 finally字句中。


在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.


更多此问题的解答请看这里:
http://forum.java.sun.com/thread.jspa?threadID=556382



你可能感兴趣的:(java.net.SocketException: No buffer space available (maximum connections reached?): connect)