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


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

Re: Not closing a SQL(ite3) connection

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Not closing a SQL(ite3) connection
Date 2017-08-12 09:52 -0400
Organization Aioe.org NNTP Server
Message-ID <omn168$mgs$1@gioia.aioe.org> (permalink)
References <87a839um2q.fsf@munus.decebal.nl> <omg0la$1qe3$1@gioia.aioe.org> <874lte1pdj.fsf@munus.decebal.nl> <omli1h$jt7$1@gioia.aioe.org> <87k229z041.fsf@munus.decebal.nl>

Show all headers | View raw


On 8/12/2017 4:00 AM, Cecil Westerhof wrote:
> Arne Vajhøj <arne@vajhoej.dk> writes:
>> On 8/11/2017 4:26 AM, Cecil Westerhof wrote:
>>> I have a strange problem. I made a class SQLite3ResultSet. There is a
>>> lot of stuff to be removed, but that is just to find out if it is
>>> doing what I think it is doing.
> 
> You did not read this part?

I did.

>>> And the close method is (for now, because I want to test it):
> 
> And also not this?

I did.

>>>       @Override
>>>               stmt.close();
>>>               resultSet.next();
>>>               resultSet.close();
>>
>> resultSet.close();
>> stmt.close();
>>
>> was rather obvious.
> 
> If you read my message a bit better, you would have seen that it is
> not real code, but that I do things that are wrong to verify that the
> good code does what I think it does.

I got that this was test code.

But it was not obvious to me exactly what was the test and what
was context.

>>> In the first try I close the stmt and then I do a resultSet.next(). I
>>> would expect an exception, because I closed the stmt, but I do not get
>>> an exception. Why not?
>>
>> There is nothing in documentation for next() requiring an exception to
>> be thrown (ResultSet is not closed and it may not get a database error -
>> maybe it does not even talk to the database).
> 
> That was the problem. When using:
>      resultSet.getInt("Count");
> instead of:
>      resultSet.next();
> 
> I get the error I expected.

Calling a getXxxx before next should give an error even if
statement was not closed.

> Now I can remove the stmt field out of my class.
> 
> With this I verified that I did not get a Statement leak:
>      @Override
>      public void close() throws SQLite3Exception {
>          System.out.println("SQLite3ResultSet: closing open resources.");
>          try {
>              Statement stmt = resultSet.getStatement();
> 
>              stmt.close();
>              System.out.println("Closed stmt.");
>              resultSet.getInt("Count");
>              System.out.println("Did getInt");
>              resultSet.close();
>          } catch (SQLException e) {
>              throw new SQLite3Exception("Could not close ResultSet.");
>          }
>          System.out.println("Closed resultSet.");
>      }
> 
> I do get ‘Closed stmt, but I do not get ‘Did getInt’. So the closing
> of stmt works.

Unusual way to test that. But yes.

> The real method is now:
>      @Override
>      public void close() throws SQLite3Exception {
>          boolean closedResultSet = false;
> 
>          try {
>              Statement stmt = resultSet.getStatement();
> 
>              resultSet.close();

There mys be missing an assignment here.

>              stmt.close();
>          } catch (SQLException e) {
>              if (closedResultSet) {
>                  throw new SQLite3Exception("Could not close Statement after closing ResultSet.");
>              }
>              throw new SQLite3Exception("Could not close ResultSet.");

I don't like this conditional here.

Either separate catch or just let the SQLException propagate information
about what the problem was.

>          }
>          System.out.println("Closed resultSet.");
>      }
> 
> The reason that I wrote this code was that I do not want to have to
> put a Statement and a ResultSet into my try with resources.

I think it is common to use one try for Statement and one
for ResultSet.

Arne

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


Thread

Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 11:28 +0200
  Re: Not closing a SQL(ite3) connection Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 18:00 -0400
    Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-11 10:26 +0200
      Re: Not closing a SQL(ite3) connection Arne Vajhøj <arne@vajhoej.dk> - 2017-08-11 20:27 -0400
        Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-12 10:00 +0200
          Re: Not closing a SQL(ite3) connection Andreas Leitgeb <avl@logic.at> - 2017-08-12 08:51 +0000
            Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-12 11:05 +0200
          Re: Not closing a SQL(ite3) connection Arne Vajhøj <arne@vajhoej.dk> - 2017-08-12 09:52 -0400
            Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-12 16:26 +0200
              Re: Not closing a SQL(ite3) connection Arne Vajhøj <arne@vajhoej.dk> - 2017-08-12 21:43 -0400
                Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-15 08:19 +0200
          Re: Not closing a SQL(ite3) connection lewbloch@gmail.com - 2017-08-12 10:14 -0700
        Re: Not closing a SQL(ite3) connection Andreas Leitgeb <avl@logic.at> - 2017-08-12 09:33 +0000
          Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-12 12:57 +0200
            Re: Not closing a SQL(ite3) connection Andreas Leitgeb <avl@logic.at> - 2017-08-12 13:27 +0000
              Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-12 16:37 +0200
            Re: Not closing a SQL(ite3) connection Arne Vajhøj <arne@vajhoej.dk> - 2017-08-12 09:55 -0400
              Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-12 16:44 +0200
                Re: Not closing a SQL(ite3) connection Arne Vajhøj <arne@vajhoej.dk> - 2017-08-12 21:45 -0400
            Re: Not closing a SQL(ite3) connection Martin Gregorie <martin@address-in-sig.invalid> - 2017-08-12 21:04 +0000
              Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-13 00:30 +0200
                Re: Not closing a SQL(ite3) connection Martin Gregorie <martin@address-in-sig.invalid> - 2017-08-13 11:34 +0000
                Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-13 15:27 +0200
          Re: Not closing a SQL(ite3) connection Andreas Leitgeb <avl@logic.at> - 2017-08-12 13:06 +0000

csiph-web