Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38868

Re: Does this make sense?

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 <q85699$oq6$1@dont-email.me>
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 <f9818c55-5eb6-47f8-8ec9-d512861ddee7@googlegroups.com> <q84vam$lri$1@dont-email.me> <c001151d-044c-4d1b-ad88-0efd923e4433@googlegroups.com> <q85699$oq6$1@dont-email.me>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <b8961ec3-eac5-45da-aa5d-11144bcae219@googlegroups.com> (permalink)
Subject Re: Does this make sense?
From Eric Douglas <e.d.programmer@gmail.com>
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

Show key headers only | View raw


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 created up front, but in this simple block that does look better.
> 
>      I foolishly assumed that your example code resembled the problem
> you actually have.  I should have known better; sorry.
> 

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 following 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 String queryText) throws ClassNotFoundException {
          Class.forName(dbDriver);
          Connection conn = null;
          PreparedStatement ps = null;
          ResultSet rs = null;
          try {
               conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
               ps = conn.prepareStatement(queryText, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
               rs = ps.getResultSet();
          } catch (final SQLException e) {
               e.printStackTrace();
               try {
                    if (ps != null) {
                         ps.close();
                    }
                    if (conn != null) {
                         conn.close();
                    }
                    conn = DriverManager.getConnection("alternate connect string", db_userid, db_password);
                    ps = conn.prepareStatement(queryText, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                    rs = 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 != null) {
               try {
                    ps.close();
               } catch (final SQLException e) {
                    e.printStackTrace();
                    if (conn != null) {
                         try {
                              conn.close();
                         } catch (final SQLException e1) {
                              e1.printStackTrace();
                              throw new Error("SQL issue");
                         }
                    }
                    throw new Error("SQL issue");
               }
          }
          if (conn != null) {
               try {
                    conn.close();
               } catch (final SQLException e) {
                    e.printStackTrace();
                    throw new Error("SQL issue");
               }
          }
          return null;
     }

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2019-04-04 05:22 -0700
  Re: Does this make sense? Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-04-04 09:03 -0400
    Re: Does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2019-04-04 07:02 -0700
      Re: Does this make sense? Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-04-04 11:01 -0400
        Re: Does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2019-04-04 08:21 -0700
        Re: Does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2019-04-04 11:45 -0700
          Re: Does this make sense? Arne Vajhøj <arne@vajhoej.dk> - 2019-04-04 20:33 -0400
            Re: Does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2019-04-05 08:39 -0700
              Re: Does this make sense? Arne Vajhøj <arne@vajhoej.dk> - 2019-04-05 12:31 -0400
              Re: Does this make sense? bursejan@gmail.com - 2019-04-05 10:53 -0700
                Re: Does this make sense? bursejan@gmail.com - 2019-04-05 10:57 -0700
                Re: Does this make sense? bursejan@gmail.com - 2019-04-05 11:02 -0700
                Re: Does this make sense? bursejan@gmail.com - 2019-04-05 11:13 -0700
          Re: Does this make sense? Patrick Roemer <sangamon@netcologne.de> - 2019-04-05 17:43 +0200
            Re: Does this make sense? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-04-07 22:14 +0200
      Re: Does this make sense? Andreas Leitgeb <avl@logic.at> - 2019-04-04 15:15 +0000

csiph-web