Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: Is this the way to work with try with resources Date: Wed, 09 Aug 2017 07:24:24 +0200 Organization: Decebal Computing Lines: 28 Message-ID: <87o9rpuxdj.fsf@munus.decebal.nl> References: <87poc6wb9n.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="75662a87a83d79e99d1b79ff65a19ab5"; logging-data="17918"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+mf8bRynjhIUwVIf/GjBm5WsiENaGFOuA=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Cancel-Lock: sha1:B+ciGAmlaLfQIIhj2/PZWwAiEvs= sha1:WUvdOsdALUYpIFa4Iqz0snWIMuc= X-Homepage: http://www.decebal.nl/ Xref: csiph.com comp.lang.java.programmer:37103 Arne Vajhøj writes: >> I thought that I could rewrite it to: >> private static void statistics() { >> try (Statement stmt = conn.createStatement(); >> ResultSet rsTotal = stmt.executeQuery(COUNT_TOTAL_PROV); >> ResultSet rsUnused = stmt.executeQuery(COUNT_UNUSED_PROV); >> ResultSet rsUsed = stmt.executeQuery(COUNT_USED_PROV)) { > > As already mentioned by somebody else then executing multiple queries > on the same connection and statement is not safe. > > And even if you modified the code to work on different connections and > statements, then it would sort of violate good database programming > practice by opening more stuff and keeping stuff open longer than > needed. > > It would also require all readers of your code to know that close > is called in reverse order for the code to look good in their eyes. > > Bottom line: bad! I made it work, but that is a big no-no? -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof