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?
Asked
Active
Viewed 3,627 times
1 Answers
2
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

Lokesh
- 1,144
- 1
- 10
- 18
-
Thanks Lokesh. I am trying to emulate stale connection behavior. I am calling an api through connection pooling. My setValidateAfterInactivity is 100 sec. A thread is printing poolstats after every 2 seconds. I am putting debug point on the api code which i am calling so that requests wait there and timeout . But as soon as it timeout my poolstats changes from [leased: 1; pending: 0; available: 0; max: 1000] to [leased: 0; pending: 0; available: 0; max: 1000]. ,releasing leased conn instantly and not considering the setValidateAfterInactivity(). So how can emulate a stale conn behavior. – Sukh Jan 12 '21 at 15:08
-
Did you try to restart (or bring it down) the dependent service after your client service is up? That should technically mark the connection as stale. – Lokesh Jan 13 '21 at 07:03
-
Yes Lokesh, i also tried this one. But unfortunately it is not resulting into the stale connection. However, following comment is written on the isStale() in HttpConnection.java : Checks whether this connection has gone down. Network connections may get closed during some time of inactivity for several reasons. The next time a read is attempted on such a connection it will throw an IOExc. This method tries to alleviate this inconvenience by trying to find out if a connection is still usable. Implementations may do that by attempting a read with a very small timeout. – Sukh Jan 13 '21 at 18:44
-
Regarding the behavior you mentioned seems to be valid. ValidateAfterInactivity validates the connection after the set interval if there is no connection is stale. If it is stale then it evicts from the connection pool. In your scenario connection was stale due to timeout so it was cleared. not sure if I am missing anything. – Lokesh Jan 14 '21 at 17:47
-
I have added the most recent documentation. Please check your version of the library as well. Seems like the behavior is changed a bit with the recent version. If you want to have more control and existing handlers do not work for you then you may need to write a custom clean-up handler. see this https://stackoverflow.com/questions/42438970/connection-eviction-strategy-in-http-connection-pooling-in-java – Lokesh Jan 14 '21 at 17:53
-
Thanks Lokesh, although i could not resolve how can i make a connection stable, i am marking this question as resolved. Thanks for discussion. Also, can you please have a look at this : https://stackoverflow.com/questions/65636256/what-can-cause-stale-connection-in-spring-boot-restapi-using-resttemplate – Sukh Jan 15 '21 at 18:31