Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff between con=null and con.close()

 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
what is diff between
1. con=null ;
2. con.close();
in jdbc.
Where object removed from memory in 2nd case?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second cleans the connection, the first just orphans the reference, making it eligible for garbage collection. The second is always better.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the second one the memory will be collected when the reference goes out of scope. If it never goes out of scope it will never be collected.

Memory leaking is bad, but so is resource leaking.
 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this means that in second case
i.e
con.close();
object con is removed from memory?
It did not require to garbage collect.
Am I correct?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ConnectionStaticLeak.conn will not be garbage collected unless it is given a null reference. Otherwise it will leak.

ConnectionInstanceLeak.conn will not be garbage collected unless the instance is garbage collected. If the instance leaks, so will the connection.


The connection is local to method, and so gets garbage collected when the instance finishes. The connection resource can still leak if the connection is not closed first.

Hence: Keep Connections local and don't pass them around unless necessary. Always close connections when you're done, and always in a finally block.
 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a David O'Meara Sir

Regards,
Vilas Lawande
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic