Path: csiph.com!news.redatomik.org!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!2a00:1d38:fa:105::183.MISMATCH!feed.usenet.farm!feeder3.usenet.farm!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:16:01 +0200 Organization: Decebal Computing Lines: 33 Message-ID: <87wp6duxri.fsf@munus.decebal.nl> References: <87poc6wb9n.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="75662a87a83d79e99d1b79ff65a19ab5"; logging-data="17918"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19wCnwInG7fU+3/+/P5iBvtsTih6jm2OQw=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:hjp4yH522s9X1bUDBTmxd442c04= sha1:SWMdxE94GVZXCPsRwwhK6Ok8rqo= Xref: csiph.com comp.lang.java.programmer:37102 Patrick Roemer writes: > Responding to Cecil Westerhof: >> 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)) { > [...] >> } catch (SQLException e) { > [...] >> } > > | By default, only one ResultSet object per Statement object can be open > | at the same time. Therefore, if the reading of one ResultSet object is > | interleaved with the reading of another, each must have been generated > | by different Statement objects. All execution methods in the Statement > | interface implicitly close a current ResultSet object of the statement > | if an open one exists. > https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html Yes, I found that out. I now do not generate a explicit Statement anymore and do something like: try (ResultSet rsTotal = conn.createStatement().executeQuery(COUNT_TOTAL_PROV); ResultSet rsUnused = conn.createStatement().executeQuery(COUNT_UNUSED_PROV); ResultSet rsUsed = conn.createStatement().executeQuery(COUNT_USED_PROV)) { It generates three ResultSet and three implicit Statement, but that is probably no problem. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof