Shivkumar Mallesappa
Shivkumar Mallesappa

Reputation: 3077

PreparedStatement.close() vs PreparedStatement = null;

I have a simple question. I have some memory leakage in my code, since I think it is because of the PreparedStatement and ResultSet I want to know that what is the best,safe and correct way to free up the resource and its memory.

PreparedStatement.close() or PreparedStatement = null?

ResultSet.close() or ResultSet = null?

Thank you

Upvotes: 4

Views: 1103

Answers (1)

glee8e
glee8e

Reputation: 6419

call close() method on every used Closable object. Clearing the reference isn't enough.

If you just clear the reference, those Statement and ResultSet objects will be closed only when your Connection is closed, and if a ConnectionPool or something alike is used, they will (most probably) never be closed.

EDIT: Closing a connection cause all Statements and ResultSets created by this connection to be closed. Closing a Statement cause all ResultSets created by this Statement to be closed.

Upvotes: 6

Related Questions