Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: Not closing a SQL(ite3) connection Date: Tue, 15 Aug 2017 08:19:31 +0200 Organization: Decebal Computing Lines: 58 Message-ID: <87tw19nyj0.fsf@munus.decebal.nl> References: <87a839um2q.fsf@munus.decebal.nl> <874lte1pdj.fsf@munus.decebal.nl> <87k229z041.fsf@munus.decebal.nl> <877ey8zwtt.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="72fb8bea0235c6b94245dccb186de60d"; logging-data="21168"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194X2PfB8KQBXqmkK7BtZiwTgaYDXGLr2E=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Cancel-Lock: sha1:9pAP5tymkIqFuMaF6Ri2MhVuesg= sha1:4mZlTuH0Ohh5yA7v9gIy15vtNDg= X-Homepage: http://www.decebal.nl/ Xref: csiph.com comp.lang.java.programmer:37230 Arne Vajhøj 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