Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37230
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Not closing a SQL(ite3) connection |
| Date | 2017-08-15 08:19 +0200 |
| Organization | Decebal Computing |
| Message-ID | <87tw19nyj0.fsf@munus.decebal.nl> (permalink) |
| References | (3 earlier) <omli1h$jt7$1@gioia.aioe.org> <87k229z041.fsf@munus.decebal.nl> <omn168$mgs$1@gioia.aioe.org> <877ey8zwtt.fsf@munus.decebal.nl> <omoasj$lmg$2@gioia.aioe.org> |
Arne Vajhøj <arne@vajhoej.dk> writes:
>> I want it to be clear when the closing of the resultSet went OK, but
>> the closing of stmt not. I was thinking about doing it in two tries,
>> but did not do it. And I should do three tries, because the fetching
>> of stmt can also go wrong. So now I have:
>> @Override
>> public void close() throws SQLException {
>> Statement stmt;
>>
>> try {
>> stmt = resultSet.getStatement();
>> } catch (SQLException e) {
>> SQLException extException = new SQLException("Could not get Statement from ResultSet.");
>>
>> extException.addSuppressed(e);
>> e = extException;
>> throw e;
>> }
>> resultSet.close();
>> try {
>> stmt.close();
>> } catch (SQLException e) {
>> SQLException extException = new SQLException("Could not close Statement after closing ResultSet.");
>>
>> extException.addSuppressed(e);
>> e = extException;
>> throw e;
>> }
>> }
>
> I would somewhat expect that the cause of the problem be available in
> the Exception itself, so one could avoid all this. But if not then
> ...
You are right, I rewrote it:
@Override
public void close() throws SQLException {
Statement stmt;
if (isClosed) {
throw new SQLException("SQLResultSet is already closed.");
}
stmt = resultSet.getStatement();
resultSet.close();
stmt.close();
isClosed = true;
}
Much cleaner code.
I check for it being closed, because I think it is an error when you
try to close more as once.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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