JDBC garbage collection

前端 未结 5 802
傲寒
傲寒 2020-12-19 20:26

What happens if i don\'t close resultset or preparedstatements.

Will they be closed and released by the garbage collector.

I\'m asking this for local variabl

5条回答
  •  攒了一身酷
    2020-12-19 21:05

    -> What happens if i don't close resultset or preparedstatements? Will they be closed and released by the garbage collector?.

    That way you are degrading the database and application performance. You must have to close or dispose JDBC resource properly.

    To close or dispose (JDBC resource) objects means these objects are now available for garbage collection and GC will releases any JDBC resources which had been acquired.

    In case of ResultSet object, it will be closed automatically when Statement.close() method is called. You may call ResultSet.close() method if you want to close ResultSet object explicitly (Read Article - 5.1.20 Closing a ResultSet Object).

    Have a look at articles - Best Practice: Closing and releasing JDBC resources and Enhancements in Java SE 7 and JDBC 4.1 (Text from this article - Feature: The ability to use a try-with-resources statement to automatically close resources of type Connection, ResultSet, and Statement)

提交回复
热议问题