title: RestTemplate + ConnectionPoolTimeoutException: Timeout waiting for connection from pool
body: <p>I got this error all of a sudden in production while the application was not under any load.</p>
  
  <p>The issue happened when my code tries to send the PUT message using spring rest template</p>
  
  <p>Here is the code how I am initialing the restTemplate</p>
  
  <pre><code>private static final RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
  {
  
      List&lt;HttpMessageConverter&lt;?&gt;&gt; messageConverters = new ArrayList&lt;HttpMessageConverter&lt;?&gt;&gt;();
      Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
      marshaller.setClassesToBeBound(PaymentSession.class);
      MarshallingHttpMessageConverter marshallingHttpMessageConverter = new MarshallingHttpMessageConverter(marshaller, marshaller);
      marshallingHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_XML, MediaType.TEXT_HTML));
      messageConverters.add(marshallingHttpMessageConverter);
      restTemplate.setMessageConverters(messageConverters);
  }
  </code></pre>
  
  <p>Call to PUT</p>
  
  <pre><code>try {
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_XML);
      HttpEntity&lt;PaymentSession&gt; httpEntity = new HttpEntity&lt;PaymentSession&gt;(session, headers);
  
      restTemplate.exchange(baseUrl+"/v1/psps", HttpMethod.PUT, httpEntity, PaymentSession.class);
  
  }catch(HttpClientErrorException e){
          logger.error("Exception..!!",e)
  }
  </code></pre>
  
  <p>Exception stacktrace</p>
  
  <pre><code>Caused by: org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
  at org.apache.http.impl.conn.PoolingClientConnectionManager.leaseConnection(PoolingClientConnectionManager.java:232)
  at org.apache.http.impl.conn.PoolingClientConnectionManager$1.getConnection(PoolingClientConnectionManager.java:199)
  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:456)
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
  at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:88)
  at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:46)
  at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:49)
  at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:509)
  </code></pre>