This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes When to close Statement & ResultSet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "When to close Statement & ResultSet?" Watch "When to close Statement & ResultSet?" New topic

When to close Statement & ResultSet?

Stefan Elfvinge
Ranch Hand

Joined: Oct 29, 2001
Posts: 52
Hallo, is it a general solution to always close
the Statement- and ResultSetobjects after you get what you want(executing the statement and iterating through the resultset)? What would happen if I "fail" to close either of them?
In my webapp I execute a number of statements and I just discovered I never close them or the resultsets. BUT, it works fine testing it(only one user), would problems appear when several users are involved?
Should the connectionobject be closed as well after executing just ONE statement, or how to decide when to close the connection?

Vels Manian

Joined: Jun 28, 2001
Posts: 22
The resultset is automatically closed if u execute the execute() in Statement or if u close the statement and statement is closed when it is garbage collected. But its a good programming practice to close these objects after use.
Stefan Elfvinge
Ranch Hand

Joined: Oct 29, 2001
Posts: 52
but what about the Connectionobject???
When to dispose of db-connection?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You can close the Db connection whneever you are finished with it. Since your other objects, such as, statement, Resultset, etc. depend on the DB connection, clossing the DB connection will automatically release thos objects too.

So much trouble in the world -- Bob Marley
I agree. Here's the link:
subject: When to close Statement & ResultSet?
Similar Threads
Connection, Resultset and statement
Invalid state for getResultSet
A[nother] question about tracking down memory leaks
How many Statements/ResultSets should be used per one Connection