title: Unable to get Connection when doing multiple calls with jersey ApacheHttpClient
body: <p>I'm having trouble with not getting connections when calling a rest api with com.sun.jersey.client.apache.ApacheHttpClient and Spring. A couple of calls get's through but suddenly I get a: </p>
  
  <pre><code>DEBUG (org.apache.commons.httpclient.MultiThreadedHttpConnectionManager:501) : - Unable to get a connection, waiting..., hostConfig=HostConfiguration[host=www.dummyapi.com]`
  </code></pre>
  
  <p>thats it. Nothing more!</p>
  
  <p><strong>This is my applicationContext.xml:</strong></p>
  
  <p>
          
          
      </p>
  
  <pre><code>&lt;bean id="customHttpClient" class="com.sun.jersey.client.apache.ApacheHttpClient"&gt;
      &lt;constructor-arg&gt;
          &lt;bean class="com.sun.jersey.client.apache.ApacheHttpClientHandler" &gt;
              &lt;constructor-arg&gt;
                  &lt;bean class="org.apache.commons.httpclient.HttpClient" &gt;
                      &lt;constructor-arg&gt;
                          &lt;bean class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager" &gt;
                              &lt;property name="params"&gt;
                                  &lt;bean class="org.apache.commons.httpclient.params.HttpConnectionManagerParams"&gt;
                                      &lt;property name="defaultMaxConnectionsPerHost" value="10" /&gt;
                                  &lt;/bean&gt;
                              &lt;/property&gt;
                          &lt;/bean&gt;
                      &lt;/constructor-arg&gt;
                  &lt;/bean&gt;
              &lt;/constructor-arg&gt;
          &lt;/bean&gt;
      &lt;/constructor-arg&gt;
      &lt;constructor-arg ref="customClientConfiguration" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="customRestClient" class="com.custom.web.service.common.RestClient" &gt;
      &lt;constructor-arg ref="customHttpClient" /&gt;
      &lt;constructor-arg value="${my.service.url}" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>These are my classes:</strong></p>
  
  <pre><code>import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;
  import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
  
  public class CustomApacheHttpClientConfig extends DefaultApacheHttpClientConfig {
  
  /**
   * Constructor with username and password.
   * @param username
   * @param password
   */
  public CustomApacheHttpClientConfig(final String username, final String password) {
      getClasses().add(JacksonJsonProvider.class);
      getProperties().put(PROPERTY_PREEMPTIVE_AUTHENTICATION, Boolean.TRUE);
      getState().setCredentials(null, null, -1, username, password);
  }
  }
  
  package com.custom.web.service.common;
  
  import com.sun.jersey.api.client.Client;
  import com.sun.jersey.api.client.ClientResponse;
  import org.apache.log4j.Logger;
  import org.codehaus.jackson.map.DeserializationConfig;
  import org.codehaus.jackson.map.ObjectMapper;
  
  import javax.ws.rs.core.MediaType;
  import java.io.IOException;
  import java.net.URI;
  
  public class RestClient {
  
  private static final Logger LOG = Logger.getLogger(RestClient.class);
  
  private final String baseUrl;
  private final Client client;
  private final ObjectMapper objectMapper;
  
  public RestClient(final String baseUrl, final Client client) {
      this.baseUrl = baseUrl;
      this.client = client;
      this.objectMapper = new ObjectMapper();
      this.objectMapper.getDeserializationConfig().set(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  }
  
  public RestClient(final String baseUrl, final Client client, final ObjectMapper objectMapper) {
      this.baseUrl = baseUrl;
      this.client = client;
      this.objectMapper = objectMapper;
  }
  
  public String getBaseUrl() {
      return baseUrl;
  }
  
  public Client getClient() {
      return client;
  }
  
  public ObjectMapper getObjectMapper() {
      return objectMapper;
  }
  }
  </code></pre>
  
  <p><strong>The call:</strong></p>
  
  <pre><code>public void do() {
  UriBuilder uriBuilder = UriBuilder.fromUri(
          String.format(
                  this.customRestClient.getBaseUrl(),
                 "SE")).
              path(SOME_UNIT_PATH_PARAM).
              path(id).
              path(SOME_OTHER_PATH_PARAM);
  
  ClientResponse response = this.customRestClient.getClient().resource(uriBuilder.build()).get(ClientResponse.class);
  
      if (response.getClientResponseStatus().getStatusCode() != 200) {
          throw new CustomRestClientException(String.format("Something went wrong when getting stuff from rest api for id: %s %d %s ",
                  id, response.getClientResponseStatus().getStatusCode(),
                  " reasonPhrase: " + response.getClientResponseStatus().getReasonPhrase()));
      }
  
      String json = response.getEntity(String.class);
  
      Stuff[] stuff = null;
  
      try {
          stuff = this.customRestClient.getObjectMapper().readValue(json, Stuff[].class);
      } catch (IOException e) {
          throw new CustomRestClientException(
                  String.format("Something went wrong when reading json value for stuff from api: %s %s %d %s ",
                          id, json, response.getClientResponseStatus().getStatusCode(), " reasonPhrase: " + e.getMessage()));
      }
  
      List&lt;Stuff&gt; stuffList = new ArrayList&lt;Stuff&gt;();
      stuffList.addAll(Arrays.asList(stuff));
  </code></pre>
  
  <p>}</p>
  
  <p>As you can see I do not explicitly close the connection after each request, I leave that to the ApacheHttpClientHandler. I tried to increase defaultMaxConnectionsPerHost but no luck there. I have also browsed around the web and only found examples where people instantiate their client in code and then destroys it there as well. Please help me figure this out because I have hit a brick wall on this.</p>
  
  <p>With regards 
  Jakob!      </p>