Reputation: 877
We have a java application which we use more then one statement variable. The problem why need more then one statement is that at time while runnning a loop for one result inside the loop we need some other query operation to be done. Most of the places the single stmt is used many times and finally we close. What we would like to confirm now is that we are not closing the resultset variables and we notice the usage of memory fluctuates.So what is the best mechanism to close the resultset immediately after we got the results or towards the end just before stmt is being closed?
Upvotes: 1
Views: 714
Reputation: 109255
According to the JDBC Specification and the Statement.close()
API doc it should be sufficient:
Note:When a Statement object is closed, its current ResultSet object, if one exists, is also closed.
Based on that you should be able to assume that you only need to close a statement. However as a Statement can have a longer lifetime than the use of a single ResultSet obtained from it, it is a good idea in general to close the ResultSet as soon as possible.
A Statement can have a longer lifetime because for example you use it again to execute another query, or in case of a PreparedStatement to execute the query again with different parameters. In the case you execute another query, the previously obtained ResultSet will be closed.
Upvotes: 4
Reputation: 311048
Close it when you are finished with it, same as any other resource. Mo point in holding on to it for longer, especially if you are down to measuring memory, which implies a memory concern.
Upvotes: 1