title: RESTEasy client connection pool is not pooling and "unable to invoke" occasionally
body: <p>I have a project that uses JAX-WS, and the implementation is RESTEasy. I know the http connection is an expensive resource and we have heavy web service traffic. so I tried to reuse the connection and promote the performance.</p>
  
  <p>The following is my client side code, and I set the pool size to 20.</p>
  
  <pre><code>public abstract class BaseService{
      protected static ResteasyClientBuilder clientBuilder = new ResteasyClientBuilder();
      static {
              clientBuilder.connectionPoolSize( 20 )
                  .establishConnectionTimeout(5000, TimeUnit.MILLISECONDS)
                  .socketTimeout(5000, TimeUnit.MILLISECONDS);
      }
  
      public Client getClient() {
          Client client = clientBuilder.build();
          ...
          return client;
      }
  }
  </code></pre>
  
  <p>A sample service class</p>
  
  <pre><code>public class MyService extends BaseService implements IMyService {
      public Something getSomething(long id){
          return  getClient().target(uri).path("getSomethingById")
                  .queryParam("id", id)
                  .request().get(Something.class);
      }
  }
  </code></pre>
  
  <p>First, it seemed that RESTEasy didn't really pool the connections and reuse them. I used the netstat to monitor the connections, no matter what number I set for the connectionPoolSize, the real connections always exceed the number. 
  <a href="https://i.stack.imgur.com/39Qnq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/39Qnq.png" alt="enter image description here"></a></p>
  
  <p>The network communication in RESTEasy is handled by Apache HttpClient, which does support pooling connection by PoolingHttpClientConnectionManager. So my question is there a way to make RESTEasy client to really make use of the pooling?</p>
  
  <p>And another very annoying issue is sometimes the web service call failed because of the connection cann't be allocated, especially when access volume is high.</p>
  
  <pre><code>javax.ws.rs.ProcessingException: Unable to invoke request
      at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:287)
  </code></pre>
  
  <p>The exception stack trace was truncated in the log because of space limit, but I believed the error is caused by <strong><em>IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated</em></strong>. I know that error might happen when javax.ws.rs.client.Client object is shared by multi threads, but that's not applicable in my case. How could this happen and what's the solution?</p>
  
  <p>Thanks in advance.</p>