开发者

JDBC result set is not closed. consequence?

开发者 https://www.devze.com 2023-04-06 03:24 出处:网络
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

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 exampl开发者_C百科e, would be memory leak occured?


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号