0

I am using HttpClient 4.5.10 and below is the code snippet that is used to make a GET request:

public String executeRequest(HttpRequestBase request, Header... headers) {
    try (CloseableHttpResponse response = execute(request, headers)) {
        return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
    } catch (final IOException e) {
        throw new RuntimeException("Failed to execute request: " + request.toString(), e);
    }
}

I use lsof to monitor connections and this results in a new connection in ESTABLISHED state. Once the request gets executed, the connection moves into CLOSE_WAIT state but doesn't go away. Subsequent calls keep adding more connections to the list.

The question is, how do I make sure that the connection gets released back to pool (I use PoolingHttpClientConnectionManager)? Do I need to explicit close the request as well, e.g.:

public String executeRequest(HttpRequestBase request, Header... headers) {
    try (CloseableHttpResponse response = execute(request, headers)) {
        return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
    } catch (final IOException e) {
        throw new RuntimeException("Failed to execute request: " + request.toString(), e);
    } finally {
        request.releaseConnection();
    }
}

Update

I have applied the above approach (i.e. request.releaseConnection()). However, I still see the open connections usin lsof. Looks like the connections are still there.

Is there anything else I need to do to explicitly close the connections?

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • You are already using try-with-resources. That is sufficient. If you don't already know that it is difficult to see why you are using the construct at all. – user207421 Jul 15 '20 at 09:30
  • Yes, try-with-resources *should* be sufficient. However, despite of the above code, I am seeing open connection after each request, hence the question. – Darshan Mehta Jul 15 '20 at 09:32
  • Do you see more connections in lsof than your pool size? – tgdavies Jul 15 '20 at 09:44
  • Nope, once it hits the threshold, all the subsequent requests to acquire connection fail. The only way to fix it is to restart the process. – Darshan Mehta Jul 15 '20 at 09:48
  • It's almost certainly doing HTTP connection pooling. Wait for the pool timeout, or change it. – user207421 Jul 15 '20 at 09:51
  • Yes, it is indeed doing the connection pooling. However, each call of the above method seems to take one connection from pool and never releases it. This results in pool getting exhausted (eventually) and I then need to restart the process. My question is, what else should I do in the above code so that it **releases** the connection back to pool? – Darshan Mehta Jul 15 '20 at 09:54
  • What happened when you tried `releaseConnection()`? – user207421 Jul 15 '20 at 10:03
  • I haven't tried `releaseConnection()`. The question is about *should I try it*? The documentation for that says it's just a convenience method. – Darshan Mehta Jul 15 '20 at 10:07
  • So why *don't* you try it? You can't seriously believe that asking questions on the Internet is more efficient than conducting your own experiments. – user207421 Jul 15 '20 at 10:16
  • The reason is [this](https://stackoverflow.com/questions/30889984/whats-the-difference-between-closeablehttpresponse-close-and-httppost-release/31659073#31659073) answer which says it's not recommended to use this method. Of course I can try it but wanted to explore the alternatives if anyone faced this issue before. – Darshan Mehta Jul 15 '20 at 10:25
  • @MarquisofLorne I have updated the question with my findings. – Darshan Mehta Jul 16 '20 at 10:25

0 Answers0