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


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

Re: Does this make sense?

Path csiph.com!aioe.org!.POSTED.O5hsyUvK01te0L/SZurVSQ.user.gioia.aioe.org!not-for-mail
From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Does this make sense?
Date Thu, 4 Apr 2019 20:33:33 -0400
Organization Aioe.org NNTP Server
Lines 111
Message-ID <q867p2$gv3$1@gioia.aioe.org> (permalink)
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> <b8961ec3-eac5-45da-aa5d-11144bcae219@googlegroups.com>
NNTP-Posting-Host O5hsyUvK01te0L/SZurVSQ.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1
Content-Language en-US
X-Notice Filtered by postfilter v. 0.9.2
Xref csiph.com comp.lang.java.programmer:38871

Show key headers only | View raw


On 4/4/2019 2:45 PM, Eric Douglas wrote:
> 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;
>       }

I would not duplicate functionality like that.

And I think it can be done without and still use resource try.

For inspiration:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class MultiTry {
     public static String test(List<String> constr, String un, String pw) {
         try(Connection con = DriverManager.getConnection(constr.get(0), 
un, pw)) {
             // do something with connection
             return constr.get(0) + " worked";
         } catch (SQLException e) {
             if(constr.size() > 1) {
                 return test(constr.subList(1, constr.size()) , un, pw);
             } else {
                 return "Houston we got a problem";
             }
         }
     }
     public static void main(String[] args) {
         String res = 
test(Arrays.asList("jdbc:mysql://localhost:3308/Test", 
"jdbc:mysql://localhost:3307/Test", "jdbc:mysql://localhost:3306/Test"), 
"root", "");
         System.out.println(res);
     }
}


Arne

PS: Class.forName for JDBC driver has not been necessary since Java 1.6 
from 2006 (13 years ago).

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