When I don\'t need to use instances of those ResultSet and Connection anymore in my program, why should I call the .close() method on both of them ?
What are the dan
*.close()
allows the object instance to release references to resources it may hold.
In the case of a ResultSet
this could be a fairly significant number of other objects. The GC will get around to cleaning them up eventually - if you "let go of" the ResultSet
. It's a good practice to get into.
A Connection
object will be holding resources associated with the connection. In this case the GC will never directly recover those resources (possibly indirectly if the Connection
implements a finalize
method). You want to *.close()
anything that might be holding up resources of any limited nature, so those resource can be reused.
It's not uncommon to have some cap on the # of connections, for example. You'll have better response & less potential for failure if you release those resources as soon as you can.
In BOTH cases, closing may improve overall responsiveness of your code; it's a good bet to make.