0

I am using jersey-client (1.9) for calling api and everytime when i need to call a webservice i create a new client instance :

Client client = Client.create();
WebResource webResource = client.resource(url);
ClientResponse response = webResource
                .queryParam(PARAM1, param1)
                .queryParam(PARAM2, param2)
                .type(MediaType.APPLICATION_JSON)
                .get(ClientResponse.class);

the problem is after a period of time i get this exception :

com.sun.jersey.api.client.ClientHandlerException: java.net.SocketException: No buffer space available (maximum connections reached?): connect

If anyone could help me figure it out I would be very grateful .

hamwac5
  • 99
  • 1
  • 2
  • 14

2 Answers2

1

You have a resource leak. (Connections and InputStreams backing ClientResponses aren't being closed.) You can read a ClientResponse entity so that its associated resources are automatically closed by calling ClientResponse.readEntity(), or you can manually close them via ClientResponse.close().

The following documentation may not exactly apply to the version of Jersey that you're using, but it's insightful:

Jersey Client API, Closing connections

Rafael
  • 7,605
  • 13
  • 31
  • 46
  • but i always read the entity in my code like this : final String responseFromWs = response.getEntity(String.class); so i dont need to close the response manually – hamwac5 Dec 29 '21 at 08:09
1

If you will provide additional details about the OS (windows/linux), more of the code or the type load your service is experiencing - all would help to provide a better answer.

A good guess though is that your OS is running out of sockets ports. Different OS's have different defaults and different ways of configuring how many sockets are available to establish TCP sockets.

though not exactly the same question, this one is similar can shed some light on your situation: java.net.SocketException: No buffer space available (maximum connections reached?): connect

There is also a write up about the same exception here: http://dbaktiar-on-java.blogspot.com/2010/03/hudson-shows-buffer-space-available.html

jason814
  • 61
  • 3
  • we use websphere in Windows server more details in this pic : https://www9.0zz0.com/2022/01/04/08/694665433.png – hamwac5 Jan 04 '22 at 08:49