I know that a connection can be a network connection (tcp) between Java application process and our Database application process, so when we're finished with a connection it's needed to close the connection, If we're using a single connection then we'll remove the network connection after we're finished and when we're using a connection pool using a library like HikariCP, when we close the connection basically I think we're releasing the connection so it can be used by other threads. So it's completely logical to release the connection resource. What I don't understand is why we need to close the statement and why is it a resource at all ? and what about the ResultSet object ? What should be closed and what not and exactly why? Is it really needed to close the statement as well? why ? what happens if we don't ? can't we use the same connection object and have different statement objects ? close statement objects aren't removed by GC after a while ? Is it just to remove those objects from the memory ?
Asked
Active
Viewed 117 times
0
-
that is not a duplicate. – Andrey B. Panfilov Jul 23 '22 at 14:41
-
These questions might sound related but it's not a duplicate because I've read the other question and I couldn't find my answer. – Majid Abdolhosseini Jul 23 '22 at 15:24
-
@AndreyB.Panfilov could I ask you to read the question again and if you think these two are not duplicates then undo what you did so I can get the answer to my question ? – Majid Abdolhosseini Jul 23 '22 at 15:32
-
I believe you need to edit Q and tick checkbox that your edits have resolved duplication – Andrey B. Panfilov Jul 23 '22 at 15:34
-
The duplicate does answer your question. – Mark Rotteveel Jul 23 '22 at 16:41
-
1@MarkRotteveel definitely not, TC is asking about reasons and consequences, but duplicates answer is "because it is recommended". java.util.stream.Stream also implements AutoCloseable but nobody closes it. – Andrey B. Panfilov Jul 23 '22 at 16:49
-
@AndreyB.Panfilov And what do you think [this answer](https://stackoverflow.com/a/4507507/466862) gives. Sure, the example is for Oracle, but it applies broader. Statements are resources on the database side, if you don't close them, you consume more memory, and run the chance of "running out". – Mark Rotteveel Jul 24 '22 at 07:26
-
@MarkRotteveel actually that answer provides nothing. Where can I find information, for example, that single statement may hold multiple resultsets and closing the first one is not enough to release resources? – Andrey B. Panfilov Jul 24 '22 at 07:32
-
@AndreyB.Panfilov That isn't even asked in the question, but if you read the JDBC API, you'll see that normally you can't have multiple open result sets, unless you use `getMoreResults(Statement.KEEP_CURRENT_RESULT)` when moving to the next. Other details will vary by driver. – Mark Rotteveel Jul 24 '22 at 07:36
-
@MarkRotteveel that was actually asked: `What I don't understand is why we need to close the statement and why is it a resource at all`. `normally you can't have multiple open result sets` - that is not true, what do you think about out parameters, or "**boolean** execute(String sql)"? – Andrey B. Panfilov Jul 24 '22 at 07:50
-
@AndreyB.Panfilov The text you quote only asks about statement being a resource, which it is. As to *your* question, from the perspective of the JDBC API, there is by default only one result set open at a time, though you can have multiple open when you use `getMoreResults(Statement.KEEP_CURRENT_RESULT)` when moving to the next result (what happens below the surface for the specific driver or database, is database-specific). – Mark Rotteveel Jul 24 '22 at 07:53
-
1@MarkRotteveel can't get what you are trying to prove. You can't definitely say what resources are associated with the statement on DB side (yours: "specific driver or database"), that in turn means client ought to close statement as well as resultset (here I'm actually not sure about resultset) - where is it written in duplicates answer? nowhere! once again: java.util.stream.Stream also implements AutoCloseable but nobody closes it, the TC question was about reasons and consequences. – Andrey B. Panfilov Jul 24 '22 at 08:02