Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer Subject: Re: Is this the way to work with try with resources Date: Tue, 8 Aug 2017 12:51:57 -0000 (UTC) Organization: A noiseless patient Spider Lines: 101 Message-ID: References: <87poc6wb9n.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Tue, 8 Aug 2017 12:51:57 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="11a971b7626ac1cfc1a8c8339de20ac6"; logging-data="31369"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1MkLPmqbbWglugOoo4LpcKcUPOFMRjXs=" User-Agent: Pan/0.141 (Tarzan's Death; 168b179 git.gnome.org/pan2) Cancel-Lock: sha1:7MYFBnIZx/v3OJ3VwyCeG0JOaL0= Xref: csiph.com comp.lang.java.programmer:37079 On Tue, 08 Aug 2017 13:26:44 +0200, Cecil Westerhof wrote: > When using with resources you ‘should’ always work with try with > resources. At the moment I have something like the following code: > private static void statistics() { > try (Statement stmt = conn.createStatement()) { > String format; > > try (ResultSet rsTotal = > stmt.executeQuery(COUNT_TOTAL_PROV)) { > final int count = rsTotal.getInt("no"); > > format = String.format("There are %%,%dd %%6s > proverbs.%%n", String.format("%,d", count).length()); > System.out.printf(format, count, "total"); > } > try (ResultSet rsUnused = > stmt.executeQuery(COUNT_UNUSED_PROV)) { > final int count = rsUnused.getInt("no"); > > if (count != 0) { > System.out.printf(format, count, "unused"); > } > } > try (ResultSet rsUsed = stmt.executeQuery(COUNT_USED_PROV)) > { > final int count = rsUsed.getInt("no"); > > System.out.printf(format, count, "used"); > } > } catch (SQLException e) { > System.err.printf("Error in statistics\n"); > e.printStackTrace(); > fatalError(""); > } > } > > It feels a bit long-winded. Is this the way to go, or is there a better > way? > > > 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)) { > int count; > String format; > > count = rsTotal.getInt("no"); > format = String.format("There are %%,%dd %%6s proverbs.%%n", > String.format("%,d", count).length()); > System.out.printf(format, count, "total"); > count = rsUnused.getInt("no"); > if (count != 0) { > System.out.printf(format, count, "unused"); > } > count = rsUsed.getInt("no"); > System.out.printf(format, count, "used"); > } catch (SQLException e) { > System.err.printf("Error in statistics\n"); > e.printStackTrace(); > fatalError(""); > } > } > In the absence of any other information I prefer the second organisation. In general, if you have a set of queries that are related so that if any of them fail, the results returned by the others are useless, then its sensible to put the lot in a single try...catch block. If the results should be treated as independent of whether any of the others fail, then they should be in separate try...catch blocks. If the queries update the database then all the queries that must either all succeed or all fail should be grouped into a commitment unit and this should by included in the one try...catch block, since, if there is any failure you'll want to roll back the whole commitment unit. > But there is a problem with that. If I do that, all three result-sets > have the same query: the last one. > > What is the reason for this? I am using SQLite3. > Thats difficult to answer knowing how ACID compliant SQLite3 is and whether it is assuming that each query is being run in an automatic commitment unit: reuse of "no", presumably a column name, in all three statements bothers me a bit and so does the fact that you've executed all three queries before asking for any results. As I result of both these factors I think you've ended up retrieving the result from the LAST query three times. It should work as expected if each query is executed immediately before calling getInt() to retrieve and print its result. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |