Are java.sql.Connection objects automatically closed when they are garbage collected?

I follow the practice of placing close()

in the last block:

void foo() {
    Connection conn;
    try {
        conn = getConnection();
        // ..
    } final {
        try {
            conn.close()
        } catch(Exception e) {
        }
    }
}

      

Is it really necessary to call close()

on the connection, or will the garbage collector do it automatically?

I'm fine with the extra latency caused by garbage collection, I just don't want connections to stay open forever.

+3


source to share


4 answers


is it really necessary to call Close () on the connection

Yes.

or will the garbage collector do it automatically?

Not chosen. Perhaps some of them will. You cannot rely on this.



I agree with the additional delay caused by the GC.

GC can never happen at all. Are you really ok with infinity?

I just don't want the connections to stay open forever.

You don't want them to stay open for a moment longer than necessary. It is a scarce resource. Don't waste them. "It is recommended that programmers explicitly close all connections (using the Connection.close method) and statements (using the Statement.close method) as soon as they are no longer needed, thereby freeing the DBMS resources as soon as possible."

+5


source


Yes, you need to call close()

. There are some JDBC wrappers out there that can do this automatically (like Tomcat connection pooling), but in general it's a very good idea to clean up after itself wherever resources are involved.



+1


source


Some implementations of JDBC connections will be close()

when they are garbage collected. An example of this is the Postgres JDBC driver in the finalize () method (code here ). However, there is no guarantee that this is the case.

But...

There is no way to know when the connection objects are garbage collected. The garbage collector is not aware of JDBC resources, namely memory. This means GC can only happen when memory needs to be freed. If you have 60 connections but have free memory, the GC won't work and you end up with a connection.

It is also possible that some JDBC connection pooling is going on. This means that the object Connection

you are getting is not a real join, but a complete one in the join logic. If you don't make close()

these connections, the pool doesn't know you're done with them and won't be able to reuse them for other requests and the connection pool will be exhausted.

Therefore, always close()

your connections are explicit if you create them.

+1


source


Since the DB connection should not remain open and garbage collection might not close it, it is helpful to use the Java 7 project coin syntax with Try With Resources. Anything that implements AutoCloseable can be used as shown below.

void foo() {
   try( Connection conn = getConnection() ) {
      // Do your Database code
   } catch(SQLException e) {
      //Handle the exception
   }
}  //conn is closed now since it implements AutoCloseable.

      

+1


source







All Articles