Proper use of close () and = null for Closable objects

Let's say I'm using a database connection with a name con(or a socket or something else that is Closable). What happens after close()? Does the convalue increase nullor is it something else? And what is the difference between con.close()and con = null?

+4
source share
1 answer

When you call close, the object should free all the resources that it uses behind the scenes. In the case of an instance, java.sql.Connectionthis will be one of two things:

  • Release any physical database connection by freeing up resources. This happens when you open a database connection manually, for example. DriverManager.getConnection(...).
  • SLEEPING , . , Connection DataSource, .

con = null null , , . Connection null close, .

close Closeable try-with-resources ( Java 7), , ) .

+5

All Articles