Return-Path: X-Original-To: apmail-hc-dev-archive@www.apache.org Delivered-To: apmail-hc-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B231278E2 for ; Tue, 1 Nov 2011 14:03:39 +0000 (UTC) Received: (qmail 75385 invoked by uid 500); 1 Nov 2011 14:03:39 -0000 Delivered-To: apmail-hc-dev-archive@hc.apache.org Received: (qmail 75357 invoked by uid 500); 1 Nov 2011 14:03:39 -0000 Mailing-List: contact dev-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list dev@hc.apache.org Received: (qmail 75349 invoked by uid 99); 1 Nov 2011 14:03:39 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Nov 2011 14:03:39 +0000 X-ASF-Spam-Status: No, hits=0.7 required=5.0 tests=SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [92.42.190.144] (HELO ok2cons2.nine.ch) (92.42.190.144) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Nov 2011 14:03:33 +0000 Received: by ok2cons2.nine.ch (Postfix, from userid 1000) id 8FDCA245E4BC; Tue, 1 Nov 2011 15:03:12 +0100 (CET) Date: Tue, 1 Nov 2011 15:03:12 +0100 From: Oleg Kalnichevski To: HttpComponents Project Subject: Re: Too Many Open Files Exception Message-ID: <20111101140312.GA16761@ok2cons2.nine.ch> References: <1320096651.3612.16.camel@ubuntu> <1320100109.3612.23.camel@ubuntu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.18 (2008-05-17) On Tue, Nov 01, 2011 at 09:29:57AM -0400, Bill Speirs wrote: > > Correct. The difference is that EntityUtils#consume will try to salvage > > the underlying connection, whereas HttpUriRequest#abort will not. > > > > The important point is to use try-finally to ensure connection release > > in all cases. > > The problem is that I have two methods to "close" the connection: one > if everything is fine, another if there is an error/exception. So > try-catch-finally doesn't really help. I think what I want is the > following. In the normal case (the try block), I simply get the entity > and close the input stream on the content. However, if something goes > wrong I should simply call abort() on the request. > > try { > response = client.execute(request); > response.getEntity().getContent().close(); > } catch( ... ) { > request.abort(); > } > > Does this make sense? > Yes, it does, as long as you also catch runtime exceptions. > I suppose moving response.getEntity().getContent().close(); to a > finally block couldn't hurt, as I don't expect the input stream would > throw an exception after abort() was called... but it seems messy. > > I guess I'm not sure why there isn't simply a close() method for the > request which can be called in either case. > Good ol' #releaseConnection() method will be back in HttpClient 4.2 > > HttpClient automatically shuts down the underlying connection in case of > > an I/O exception including a timeout. The connection manager will evict > > closed connection immediately upon release. > > That isn't what I'm seeing, although it's tough to reproduce. On two > separate machines running the same code I'm seeing the following: > > try { > HttpResponse response = client.execute(httpHost, request); // throws > a java.net.SocketTimeoutException > } catch(IOException e) { > request.abort(); > } > This code is likely to leak connections in case of a runtime (unchecked) exception. Oleg --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org For additional commands, e-mail: dev-help@hc.apache.org