Connection Pooling with Spring RestTemplate
If you are using Spring MVC's RestTemplate to make REST calls, it is important to realize that it doesn't use HTTP connection pooling of any kind, and will establish and close a connection every time you make a REST call.
If you want to use connection pooling, you would need to provide another implementation of ClientHttpRequestFactory. A good option is to use the org.springframework.http.client.HttpComponentsClientHttpRequestFactory() which is provided with Spring.
new org.springframework.web.client.RestTemplate(new HttpComponentsClientHttpRequestFactory())
Of course, if you look up the documentation for HttpComponentsClientHttpRequestFactory, you can configure a lot of the connection pooling parameters.
Written by Zoheb Sait
Related protips
3 Responses
Thank you very much for the info, great help.
What is the performance increase by using connection pool? When does it make sense to use it?
Thanks
Thanks for this tip, just what I was looking for. Sadly this doesn't seem to be part of the official documentation.