X-Received: by 2002:a37:6442:: with SMTP id y63mr1093957qkb.12.1554403533009; Thu, 04 Apr 2019 11:45:33 -0700 (PDT) X-Received: by 2002:a25:2184:: with SMTP id h126mr7369305ybh.6.1554403532798; Thu, 04 Apr 2019 11:45:32 -0700 (PDT) Path: csiph.com!3.us.feeder.erje.net!feeder.erje.net!news.linkpendium.com!news.linkpendium.com!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!t9no5231134qtn.0!news-out.google.com!i8ni1319qtr.1!nntp.google.com!t9no5231130qtn.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Thu, 4 Apr 2019 11:45:32 -0700 (PDT) In-Reply-To: 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: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Does this make sense? From: Eric Douglas Injection-Date: Thu, 04 Apr 2019 18:45:33 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Lines: 85 Xref: csiph.com comp.lang.java.programmer:38868 On Thursday, April 4, 2019 at 11:02:14 AM UTC-4, Eric Sosman wrote: > > Ah right, resources doesn't always work because in some cases I'm using= Closeable objects instead of Autocloseable, and in some cases I'm creating= the the object (in the case the java.io.File) in the same try block as the= object (java.io.FileOutputStream) needing to be closed so they can't be cr= eated up front, but in this simple block that does look better. >=20 > I foolishly assumed that your example code resembled the problem > you actually have. I should have known better; sorry. >=20 So for example an Autocloseable can't be used in try-with-resources because= it has 2 places to instantiate. I keep messing with code like the followi= ng which keeps giving different errors on what may not be closed. public static String testMethod(final String db_connect_string, final = String db_userid, final String db_password, final String dbDriver, final St= ring queryText) throws ClassNotFoundException { Class.forName(dbDriver); Connection conn =3D null; PreparedStatement ps =3D null; ResultSet rs =3D null; try { conn =3D DriverManager.getConnection(db_connect_string, db_u= serid, db_password); ps =3D conn.prepareStatement(queryText, ResultSet.TYPE_SCROL= L_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs =3D ps.getResultSet(); } catch (final SQLException e) { e.printStackTrace(); try { if (ps !=3D null) { ps.close(); } if (conn !=3D null) { conn.close(); } conn =3D DriverManager.getConnection("alternate connect= string", db_userid, db_password); ps =3D conn.prepareStatement(queryText, ResultSet.TYPE_= SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs =3D ps.getResultSet(); } catch (final SQLException e1) { e1.printStackTrace(); } } try { while (rs.next()) { System.out.println(rs.getRow()); } } catch (final SQLException e1) { e1.printStackTrace(); } finally { try { rs.close(); } catch (final SQLException e) { e.printStackTrace(); throw new Error("SQL issue"); } } if (ps !=3D null) { try { ps.close(); } catch (final SQLException e) { e.printStackTrace(); if (conn !=3D null) { try { conn.close(); } catch (final SQLException e1) { e1.printStackTrace(); throw new Error("SQL issue"); } } throw new Error("SQL issue"); } } if (conn !=3D null) { try { conn.close(); } catch (final SQLException e) { e.printStackTrace(); throw new Error("SQL issue"); } } return null; }