Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37114
| From | Andreas Leitgeb <avl@logic.at> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Is this the way to work with try with resources |
| Date | 2017-08-09 19:51 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrnoomq5s.o2q.avl@logic.at> (permalink) |
| References | (1 earlier) <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> |
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.
> By the way is there a way to ‘find’ all open Statements? That could be
> used to verify if a long running application is leaking resources.
I don't know, but I don't see any good usecase for it.
Any piece of code ought to know only about its own Statements.
Once you close the connection, you don't need to care about
any left-overs, anyway, and before you close the connection,
any of the existing Statements might be actively used in other
parts/threads of the code.
I think, after all is said and done, your code may look like this:
try (
Statement stmnt1 = conn.createStatement();
ResultSet rs1 = stmnt1.executeQuery(...);
Statement stmnt2 = conn.createStatement();
ResultSet rs2 = stmnt1.executeQuery(...);
Statement stmnt3 = conn.createStatement();
ResultSet rs3 = stmnt1.executeQuery(...);
) {
// use *ALL the RS's*!
}
(except for more expressive variable names ;-) )
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