Path: csiph.com!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!.POSTED.xdsl-87-79-180-106.netcologne.de!not-for-mail From: Patrick Roemer Newsgroups: comp.lang.java.programmer Subject: Re: Is this the way to work with try with resources Date: Wed, 9 Aug 2017 22:08:00 +0200 Organization: news.netcologne.de Distribution: world Message-ID: References: <87poc6wb9n.fsf@munus.decebal.nl> <87lgmtwels.fsf@munus.decebal.nl> <8760dxuhzl.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Wed, 9 Aug 2017 20:08:00 +0000 (UTC) Injection-Info: newsreader4.netcologne.de; posting-host="xdsl-87-79-180-106.netcologne.de:87.79.180.106"; logging-data="937"; mail-complaints-to="abuse@netcologne.de" User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.24) Gecko/20100411 Thunderbird/2.0.0.24 Mnenhy/0.7.6.0 In-Reply-To: Content-Language: en-US Xref: csiph.com comp.lang.java.programmer:37115 Responding to Andreas Leitgeb: > Cecil Westerhof wrote: >> Andreas Leitgeb writes: >>> Cecil Westerhof wrote: >>>> private static void statistics() { >>>> try (ResultSet rsTotal = executeQuery(COUNT_TOTAL_PROV); >>>> ResultSet rsUnused = executeQuery(COUNT_UNUSED_PROV); >>>> ResultSet rsUsed = executeQuery(COUNT_USED_PROV)) { >>> and >>>> private static ResultSet executeQuery(String query) throws SQLException { >>>> return conn.createStatement().executeQuery(query); >>>> } [...] > Now, in that snippet above, it would (3 times) create a statement, then execute > a query to get a resultset. If one of the queries went wrong, then the > try would auto-close any previously obtained resultsets, but it would not > do anything on the statements - in particular: not clean them up. The statements won't be closed, no matter whether the queries succeed or fail. The try-with-resources will only take care of closing the result sets. (And it will do so independent of any exceptions triggered in the process.) Best regards, Patrick