Drain JDK HTTP client response body in all cases

Prior to this commit, when using the `SimpleClientHttpRequestFactory`
as a driver for `RestTemplate`, the HTTP response body would only be
drained if there was an attempt to read it in the first place.

This commit ensures that, even if there's no attempt at reading the
response body, it is properly drained when the response is closed to
make sure that the connection is released in a proper state and can be
put back in the connection pool for reuse.

Issue: SPR-17181
This commit is contained in:
Brian Clozel
2018-08-15 20:52:40 +02:00
parent 432cdd7802
commit 23fc6f6b1d
3 changed files with 21 additions and 8 deletions

View File

@@ -91,14 +91,15 @@ final class SimpleClientHttpResponse extends AbstractClientHttpResponse {
@Override
public void close() {
if (this.responseStream != null) {
try {
StreamUtils.drain(this.responseStream);
this.responseStream.close();
}
catch (Exception ex) {
// ignore
try {
if (this.responseStream == null) {
getBody();
}
StreamUtils.drain(this.responseStream);
this.responseStream.close();
}
catch (Exception ex) {
// ignore
}
}