X-Received: by 2002:aed:3542:: with SMTP id b2mr102285qte.40.1551373248991; Thu, 28 Feb 2019 09:00:48 -0800 (PST) X-Received: by 2002:a5b:10c:: with SMTP id 12mr304776ybx.323.1551373248775; Thu, 28 Feb 2019 09:00:48 -0800 (PST) Path: csiph.com!goblin1!goblin.stu.neva.ru!m21no4873990qta.0!news-out.google.com!y15ni2185qta.0!nntp.google.com!m21no4873986qta.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Thu, 28 Feb 2019 09:00:48 -0800 (PST) In-Reply-To: <692da128-9302-41cf-a8f7-2a2a5903bf82@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=50.78.95.177; posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H NNTP-Posting-Host: 50.78.95.177 References: <709a1f3b-fc99-4bd0-a8b8-866092bb7ae9@googlegroups.com> <8e1597a2-5c18-4fd1-83a4-7667e4d11e77@googlegroups.com> <23a8128b-5c54-45ba-bfd0-2f7dda078517@googlegroups.com> <692da128-9302-41cf-a8f7-2a2a5903bf82@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <3a4ed199-10a5-4ea3-882e-8d598a82dccf@googlegroups.com> Subject: Re: it's Closeable, but I don't want to close() it yet. From: Eric Douglas Injection-Date: Thu, 28 Feb 2019 17:00:48 +0000 Content-Type: text/plain; charset="UTF-8" Xref: csiph.com comp.lang.java.programmer:38751 On Thursday, February 28, 2019 at 11:34:22 AM UTC-5, burs...@gmail.com wrote: > See also: > > Must JDBC Resultsets and Statements be closed > separately although the Connection is closed afterwards? > https://stackoverflow.com/q/4507440/502187 > > The clean OO-solution, in case you dont need > the result set later, is to use try-with-resources statement. > I dont recommend using result sets later, since > > database systems usually have a dead man's handling. > There can be timeouts on all kind of layer levels. > Usually these tomeouts have a special error code, > > and you should then do retry. It could be that your > connection pooling product does this already for you. > But it might not apply to result sets. > > So somehow the eclipse warning makes sense. > > On Thursday, February 28, 2019 at 5:24:29 PM UTC+1, burs...@gmail.com wrote: > > Actually you use try (resource), which does automatically > > always a close. Its internally implemented with try finally > > and does exceptiong piggy packing. > > > > But JDBC says, if you close a statement, I guess result > > sets are also closed. And if you close a connection, statements > > are also closed. So what is this fuzz about? > > > > What would be helpful, if the warning from the IDE would > > be shown as well. Could the OP please give the full test case, > > including its outcome? > > > > On Thursday, February 28, 2019 at 3:52:24 PM UTC+1, Eric Douglas wrote: > > > On Thursday, February 28, 2019 at 9:19:24 AM UTC-5, Eric Douglas wrote: > > > > On Thursday, February 28, 2019 at 7:20:35 AM UTC-5, Andreas Leitgeb wrote: > > > > > The Closeable interface does not provide any concept of opening. > > > > > Eclipse just sees a local variable of a Closeable type that > > > > > goes out of scope without .close() being called on it. > > > > > > Now, it is possible to confuse Eclipse. I tried this method and got a warning. Is there a better way to do this? > > > > > > public void testQuery(final String db_connect_string) { > > > try (Connection conn = DriverManager.getConnection(db_connect_string)) { > > > final String queryString = "select * from Log limit 10000 offset ?"; > > > try (PreparedStatement statement = conn.prepareStatement(queryString, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) { > > > int r = 0; > > > int r2 = -1; > > > statement.setInt(1, 0); > > > ResultSet rs = statement.executeQuery(); > > > while (r != r2) { > > > r2 = r; > > > while (rs.next()) { > > > r++; > > > } > > > rs.close(); > > > statement.setInt(1, r); > > > rs = statement.executeQuery(); > > > } > > > rs.close(); > > > } > > > } catch (SQLException e) { > > > e.printStackTrace(); > > > } > > > } While my ugly loop to reuse the ResultSet variable should work properly, Eclipse didn't recognize that it closed properly. That error message is "Potential resource leak: 'rs' may not be closed" I'm guessing the clean way to paginate the table (my while loop uses POI to write the records to Excel, the r variable is actually the row number there) could look like this? public void testQuery(final String db_connect_string) { final String queryString = "select * from Log limit 10000 offset ?"; try (Connection conn = DriverManager.getConnection(db_connect_string);PreparedStatement statement = conn.prepareStatement(queryString, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) { int r = 0; int r2 = -1; while (r != r2) { r2 = r; statement.setInt(1, r); try (ResultSet rs = statement.executeQuery()) { while (rs.next()) { r++; } } } } catch (SQLException e) { e.printStackTrace(); } }