Reputation: 1482
What happens with the connection established with database if a instance of java.sql.Connection
as con
is nullified.
con = null;
Is the connection closed or remains open till the application is running.
2nd Question :
Similarly if have an instance of javax.mail.Store
as store
and nullify it ,
store = null;
will the smtp connection be closed or remains unclosed. Thanks.
Upvotes: 2
Views: 814
Reputation: 12326
There's finalize method and it's very likely that concrete implementation of the Connection or Store will override this method and call close() there. For example here's snippet from PostgreSQL JDBC Driver:
protected void finalize() throws Throwable
{
if (openStackTrace != null)
logger.log(GT.tr("Finalizing a Connection that was never closed:"), openStackTrace);
close();
}
This method will be called when garbage collector decides that it's time to collect given object.
But it's recommended to always close connections and similar resources manually, because garbage collector is unpredictable. You can hit connection limit faster than garbage collector will start and free resources, for example.
Upvotes: 1
Reputation: 5334
If you set your Connection
to null, this connection will remain "open" and the code associated with close will not be run. Hence in order to close it you must call the close()
method.
If the connection is not closed, other resources that might be held by the connection won't be released either
Upvotes: 3
Reputation: 240890
If you make your connection
instance null without calling close()
then,
connection
would be ready to be garbage collected
and it will not invoke the code that is required to do with close()
method's implementation
Upvotes: 2