Reputation: 4882
HI: We have some pure JDBC preparedstatement, after it execute query, we process resultset. But we never close resultset though we closed statement. When execute this preparedstatement again and again, million times for example, would be memory leak occured?
Upvotes: 1
Views: 855
Reputation: 338396
In theory, closing the Statement should close the ResultSet. But real-world practice is another matter.
Yes there are reasons to explicitly close your ResultSet objects (or use the new try-with-resources syntax to close them). See this answer for more info. Design limitations and bugs all too commonly found in JDBC drivers, connection pools, and database can cause problems.
Upvotes: 0
Reputation: 2072
Closing the statement should allow the resultset to get garbage collected. however, closing the statement will implicitly close the resultset, so how can you continue to use it? Is the whole thing wrapped in a try-catch-finally block, which sets the statement and resultset to null in the finally block?
Otherwise if you throw an exception you may chew up connections, and the statement and resultset may not get connected. I.e. as long as you stay with the happy day scenario everything will be okay, but it could bring everything down if one thing goes wrong as the lack of the finally block resetting to null may prevent garbage collection and chew up all your resources (e.g. connections).
Upvotes: 1