Reputation: 540
I read that "stale connections are a result of the server disconnecting the connection but the client not knowing." But i am trying to find how it is possible in case I am using my application based on SpringBoot RestTemplate (further using PoolConnectionManager from Apache) and calling another API from my application? In this case, my application is a client and the application i am calling is acting as a server. If i hit that application, and the api i am calling receives the request but somehow breaks down before full filling the request. In this case, i will surely get the exception at my end. And i am pretty sure that in case PoolConnectionManager must be closing that connection. Then how can i ever have stale connection?
Upvotes: 3
Views: 6547
Reputation: 1303
By default, PoolConnectionManager does not close the staled connection unless you configure it to do that. Method setValidateAfterInactivity()
is used to configure that time period.
PoolingHttpClientConnectionManager connManager
= new PoolingHttpClientConnectionManager();
connManager.setValidateAfterInactivity(20);
HttpClient httpClient = HttpClients.custom().setConnectionManager(connManager).build();
You can find a similar example in the StackOverflow here
** Update after following up questions **
Based on the documentation behavior is changed a bit from version 4.4.
The handling of stale connections was changed in version 4.4. Previously, the code would check every connection by default before re-using it. The code now only checks the connection if the elapsed time since the last use of the connection exceeds the timeout that has been set. The default timeout is set to 2000ms
Upvotes: 2