Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37117
| Path | csiph.com!news.redatomik.org!aioe.org!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Cecil Westerhof <Cecil@decebal.nl> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Is this the way to work with try with resources |
| Date | Wed, 09 Aug 2017 23:17:11 +0200 |
| Organization | Decebal Computing |
| Lines | 86 |
| Message-ID | <87mv78tp9k.fsf@munus.decebal.nl> (permalink) |
| References | <87poc6wb9n.fsf@munus.decebal.nl> <87lgmtwels.fsf@munus.decebal.nl> <slrnoolmbl.o2q.avl@logic.at> <8760dxuhzl.fsf@munus.decebal.nl> <slrnoomfkk.o2q.avl@logic.at> <87y3qstzey.fsf@munus.decebal.nl> <slrnoomq5s.o2q.avl@logic.at> <87tw1gtrw3.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="75662a87a83d79e99d1b79ff65a19ab5"; logging-data="31839"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19MQc9/j0sO2ROJuvNKMuw3UgcEYX7JZxo=" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) |
| Cancel-Lock | sha1:0U6hY7+F1hSVf9pp15uWg6RL068= sha1:+WlyB6inLyg6hGAvcllM5egMOPw= |
| X-Homepage | http://www.decebal.nl/ |
| Xref | csiph.com comp.lang.java.programmer:37117 |
Show key headers only | View raw
Cecil Westerhof <Cecil@decebal.nl> writes:
> Andreas Leitgeb <avl@logic.at> writes:
>
>> Cecil Westerhof <Cecil@decebal.nl> wrote:
>>> I had there the following function:
>>> public ResultSet executeQuery(String query) throws SQLException {
>>> return conn.createStatement().executeQuery(query);
>>> }
>>> but I now changed that to:
>>> public ResultSet executeQuery(String query) throws SQLException {
>>> try (Statement stmt = conn.createStatement()) {
>>> return stmt.executeQuery(query);
>>> }
>>> }
>>> This should take care of the leakage.
>>
>> I fear, next trouble is right ahead.
>>
>> try doesn't only close it's resource on failure, but also on completion!
>>
>> That means, it will (because closing the statement will also close the
>> ResultSet) return an already closed ResultSet.
>
> I have in my code:
> private static void displayOldestUsed() {
> try (ResultSet rsOldest = sqlite3Conn.executeQuery(OLDEST_USED)) {
> if (rsOldest.next()) {
> giveMessage("Oldest used: " + rsOldest.getString("used") + ".");
> }
> } catch (SQLException e) {
> giveError("Error in displayOldestUsed");
> e.printStackTrace();
> fatalError("");
> }
> }
>
> And rsOldest.getString("used") gives the result I would expect. Or am
> I just ‘lucky’?
Nope, I did something wrong. When doing it right I got:
java.sql.SQLException: ResultSet closed
So I know do it with Statement and ResultSet in the try with
resources. Makes my SQLite3 class a bit less useful.
But my statistics is now:
private static void statistics() {
try {
int count;
String format;
int maxLen;
count = sqlite3Conn.getCount(COUNT_TOTAL_PROV);
// First count is the largest
maxLen = String.format("%,d", count).length();
format = "There are %," + maxLen + "d %-6s proverbs.";
giveMessage(String.format(format, count, "total"));
count = sqlite3Conn.getCount(COUNT_UNUSED_PROV);
if (count != 0) {
giveMessage(String.format(format, count, "unused"));
}
count = sqlite3Conn.getCount(COUNT_USED_PROV);
giveMessage(String.format(format, count, "used"));
} catch (SQLException e) {
giveError("Error in statistics");
e.printStackTrace();
fatalError("");
}
}
Because I defined:
public int getCount(String query) throws SQLException {
int count;
try (Statement stmt = createStatement();
ResultSet rsCount = stmt.executeQuery(query)) {
count = rsCount.getInt("count");
}
return count;
}
--
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
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