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:20:34 +0200 Organization: Decebal Computing Lines: 76 Message-ID: <87shh1uxjx.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/fmrPmbC3fthaIdd5ZzXA8VI0rhGz01F8=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Cancel-Lock: sha1:cdrvgNVOgeMzN7PCQqtWiwYbHJs= sha1:9TLOsq9QY32KR87F3EEVckvYByI= X-Homepage: http://www.decebal.nl/ Xref: csiph.com comp.lang.java.programmer:37100 ram@zedat.fu-berlin.de (Stefan Ram) writes: > Patrick Roemer writes: >>| 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 > > Ok. As far as I understand it, the refactoring posted by me > does not suffer from violations against this restriction. > > Still, I have revised my code so as to make the try regions > for the statements as small as possible in order to make it > readably clear that there is no such violation: > > (Note that especially »makeFormatAndContinue()« now is > evaluated outside of the scope of »rsTotal«, so now there > are no result sets that even have overlapping lifetimes.) > > private static java.sql.Statement statement; > private static int count; > private static java.lang.String format; > > private static void printTotal() > { System.out.printf( format, count, "total" ); } > > private static void printUnused() > throws java.sql.SQLException > { int countUnused = 0; > try( final java.sql.ResultSet rsUnused = statement.executeQuery( COUNT_UNUSED_PROV )) > { countUnused = rsUnused.getInt( "no" ); } > if( countUnused != 0 )java.lang.System.out.printf( format, count, "unused" ); } > > private static void printUsed() > throws java.sql.SQLException > { int countUsed = -1; > try( final java.sql.ResultSet rsUsed = statement.executeQuery( COUNT_USED_PROV )) > { countUsed = rsUsed.getInt( "no" ); } > java.lang.System.out.printf( format, countUsed, "used" ); } > > private static void printAllStatistics() > throws java.sql.SQLException > { printTotal(); > printUnused(); > printUsed(); } > > private static void makeFormatAndContinue() > throws java.sql.SQLException > { format = java.lang.String.format > ( "There are %%,%dd %%6s proverbs.%%n", java.lang.String.format( "%,d", count ).length() ); > printAllStatistics(); } > > private static void makeCountAndContinue() > throws java.sql.SQLException > { try( final java.sql.ResultSet rsTotal = statement.executeQuery( COUNT_TOTAL_PROV )) > { count = rsTotal.getInt( "no" ); } > makeFormatAndContinue(); } > > private static void makeStatementAndContinue() > { try( final java.sql.Statement s = conn.createStatement() ) > { statement = s; makeCountAndContinue(); } > catch( final java.sql.SQLException exception ) > { java.lang.System.err.println( "Error in statistics" ); > exception.printStackTrace(); > fatalError( "" ); }} > > private static void printStatistics() > { makeStatementAndContinue(); } > > public static void main( final java.lang.String[] args ) > { java.lang.System.out.println( "OK" ); }} Looks interesting, but personally I find it to complex. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof