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


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

Re: Is this the way to work with try with resources

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Is this the way to work with try with resources
Date 2017-08-09 18:05 -0400
Organization Aioe.org NNTP Server
Message-ID <omg0vf$1r4b$1@gioia.aioe.org> (permalink)
References <87poc6wb9n.fsf@munus.decebal.nl> <87lgmtwels.fsf@munus.decebal.nl>

Show all headers | View raw


On 8/9/2017 12:26 AM, Cecil Westerhof wrote:
> Cecil Westerhof <Cecil@decebal.nl> writes:
>> I thought that I could rewrite it to:
>>      private static void statistics() {
>>          try (Statement stmt     = conn.createStatement();
>>               ResultSet rsTotal  = stmt.executeQuery(COUNT_TOTAL_PROV);
>>               ResultSet rsUnused = stmt.executeQuery(COUNT_UNUSED_PROV);
>>               ResultSet rsUsed   = stmt.executeQuery(COUNT_USED_PROV)) {

> I do not know enough about the JDBC. When looking at:
>      https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html
> 
> It clearly states that you normally only can have one current
> result-set pro statement. It says there:
>      All execution methods in the Statement interface implicitly close
>      a current ResultSet object of the statement if an open one exists.
> 
> But that is clearly not true, because the other ones still work, but
> are replaced with the other one.

You can somewhat expects drivers to work properly in normal
scenarios, but in more exotic scenarios there can be bugs.

> The solution is to have a statement pro result-set, but than this
> statement is only necessary for getting the result-set. So I rewrote
> statistics to:
>      private static void statistics() {
>          try (ResultSet rsTotal  = executeQuery(COUNT_TOTAL_PROV);
>               ResultSet rsUnused = executeQuery(COUNT_UNUSED_PROV);
>               ResultSet rsUsed   = executeQuery(COUNT_USED_PROV)) {

> And wrote the function executeQuery to make the code a little more
> compact:
>      private static ResultSet executeQuery(String query) throws SQLException {
>          return conn.createStatement().executeQuery(query);
>      }
> 

Still bad. Maybe even worse.

It is not in general safe to non-serial execute multiple
SELECT's on a single connection.

You do not get the statement objects closed. The temporary
objects in the convenience method does not get handled by
the try statement.

Arne

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


Thread

Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-08 13:26 +0200
  Re: Is this the way to work with try with resources Martin Gregorie <martin@address-in-sig.invalid> - 2017-08-08 12:51 +0000
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:10 +0200
  Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-08 18:29 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:20 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:16 +0200
  Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-08 19:30 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 06:49 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:01 +0200
    Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-09 15:39 +0200
  Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-08 21:25 -0400
    Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-08 21:36 -0400
      Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:29 +0200
        Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 20:53 -0400
          Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 23:03 -0400
    Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-08 21:48 -0400
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:24 +0200
  Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 06:26 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 08:30 +0200
    Re: Is this the way to work with try with resources Andreas Leitgeb <avl@logic.at> - 2017-08-09 09:40 +0000
      Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 12:56 +0200
        Re: Is this the way to work with try with resources Andreas Leitgeb <avl@logic.at> - 2017-08-09 16:51 +0000
          Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 19:37 +0200
            Re: Is this the way to work with try with resources Andreas Leitgeb <avl@logic.at> - 2017-08-09 19:51 +0000
              Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 22:20 +0200
                Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 23:17 +0200
          Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-09 22:08 +0200
        Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 18:07 -0400
    Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 18:05 -0400
  Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 06:39 +0200

csiph-web