• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collection of Statement object being unreferenced  RSS feed

 
vikasids sharma
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
I have created a Prepared Statement for selecting records from database.
After using the statement i have closed it in try catch block .
Outside the catch block , i marked statement as NULL.

Now lets say , statement.close() fails and exception is simply logged (havnt took any action on failure) .
Would that statement object be picked by Garbage collector for reclaiming memory ?

Thanks
Vikas
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends where else the Statement object is referred to from. The real problem is that you might not close the statement if it is not in a finally block.
 
vikasids sharma
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Depends where else the Statement object is referred to from. The real problem is that you might not close the statement if it is not in a finally block.


Hi Ritchie


Ok, i close the statement in finally block but could not ignore the fact that statement.close() actually failed in database , i am taking it as database driver failed to dis-associate cursor from statement . But marking that statement explicitly afterwards as NULL would help to reclaim memory?
Would there be any consequences in application from database point of view?

Thanks
Vikas
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. Nitesh may wish to transfer this thread to the JDBC forum because it appears database-specific.

Setting the Statement to null (not "NULL") will only make a difference if the Statement is a field, but if there are any references to that Statement object anywhere else, it won't be collected from memory. Local variabales become unreachable when they go out of scope, so the null assignment is unnecessary for a local variable. Remember: no other references.

I don't know whether there is any risk that your Statement, not being properly closed, will block the Connection from being used again, or gradually use all the available connections to that Database, thereby creating a resource leak. Don't know.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!