oneiros
oneiros

Reputation: 3568

Should I close ResultSet after each query or close it after doing all my queries?

Let us say that I want to perform 10 queries. I create a ResultSet rs1 like this:

ResultSet rs1 = MyStatement.executeQuery(QUERY_1);
while (rs1.next()){
    // do something
}
rs1.close();

rs1 = MyStatement.executeQuery(QUERY_2);
while (rs1.next()){
    // do something
}
rs1.close();

After I execute QUERY_1 I close rs1, then I assign it to execute QUERY_2 and then I close it again. Now I was wondering - Can I execute all 10 queries and close rs1 just once right after executing the 10th, or do I have to open and close it after each execution of a query?

Thank you in advance.

Upvotes: 1

Views: 247

Answers (1)

Milan Jaric
Milan Jaric

Reputation: 5646

You should close each INSTANCE to avoid memory leaks.

Try to load several huge result sets, you''ll see, until garbage collector figures which instance is released you will suffer from issues which comes with to much memory usage.

Upvotes: 1

Related Questions