JDBC garbage collection

What happens if I don't close the results or prepared states.

They will be closed and released by the garbage collector.

I ask this for local variables inside a function.

Do you know any documentation about this?

+1


source to share


5 answers


If your code does not close, ResultSet

or PreparedStatement

when done with this application, your application will handle scarce resources - like cursors - in the database. See for example:



The garbage collector doesn't know anything about closures ResultSet

or PreparedStatement

s, so the GC won't automatically do this for you. What will happen? Java 7 try

-with-resources statement
!

+7


source


I'm not an expert here, but I don't think the GC can collect this since PSs are tied to database connections, so there won't be garbage collection. You can take a look here: http://www.theserverside.com/news/1365244/Why-Prepared-Statements-are-important-and-how-to-use-them-properly



+1


source


You must explicitly close the ResultSet and Statement objects after using them. This applies to all ResultSet and Statement objects that you create when using JDBC drivers. Drivers do not have finalizer methods; cleanup procedures are performed by the close () method of the ResultSet and Statement classes. Serious memory leaks can occur if you do not explicitly close the ResultSet and Statement objects. Cursors may also be missing from the database. The result set is closed and the statement releases the corresponding cursor in the database; if you only close the result set, the cursor is not released

+1


source


unless I close the result set or prepared statements. Will be closed and released by the garbage collector.

resultset

and preparedstatment

are closed by explicitly calling the method close

. The garbage collector does not close them. I do not name close

, then the oracle cursor is not released at the end of the oracle. In the meantime, there is no need to worry about it. ”

Will they be released by the garbage collector.

      

Typically, an object becomes garbage collection in Java in the following cases:

  • All references of this object are explicitly set to zero, for example. object = null
  • An object is created inside a block, and the link goes out of scope after the control leaves that block.
  • The parent object is null if the object contains a reference to another object, and when you set the container object reference to null, the child or containing object is automatically garbage collected.
  • If the object only has live references through the WeakHashMap, it will be eligible for garbage collection.

for your question: I am asking this for local variables inside a function.

A ResultSet that was created inside a method, not private, and the reference goes out of scope after the control finishes that method. then the reference is null and the object is eligible for garbage collection. I said that the right is not guaranteed. A matching oracle cursor is still present in the database. Because you didn't call close.

+1


source


-> What happens if I don't close the results or prepared data? Will they be closed and released by the garbage collector?

Thus, you degrade database and application performance. You need to close or dispose of the JDBC resource correctly .

Resources

To close or dispose

(JDBC resource) means that these objects are now available for garbage collection and the GC will release all the JDBC

resources it got.

In the case of an object, ResultSet

it will be automatically closed when the method is called Statement.close()

. You can call the ResultSet.close () method if you want to explicitly close the object ResultSet

(Read the article - 5.1.20 Closing a ResultSet object) ,

Take a look at the articles - Best Practice: Closing and Freeing JDBC Resources and Improvements in Java SE 7 and JDBC 4.1 (Text from this article - Feature: Ability to use try-with-resources statement to automatically close resources like Connection, ResultSet and Statement)

0


source







All Articles