3

I follow the practice of putting a close() in the final 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 will do that automatically?

I am OK with the extra delay that garbage-collection will induce, I just don't want the connections to remain open forever.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Ajeet Ganga
  • 8,353
  • 10
  • 56
  • 79
  • 4
    Its your responsibility to close the connection when you're done with it. The garbage collector will not do it for you. – Perception Feb 12 '13 at 23:01
  • Maybe this answers your question: http://stackoverflow.com/questions/8652336/jdbc-garbage-collection In summary: You need to close connections to prevent various leaks and can't rely on GC to do it for you. – Anurag Kapur Feb 12 '13 at 23:01
  • 1
    You may run out of connections before they are finalised. It's not going to be reliable without the close. You may like to try the Execute Around idiom. (As your code is apparently written, you appear to discard the close exception (possibly an NPE from because of failed acquire) - you are probably going to want to throw some kind of exception from there, similar to that thrown from acquiring the connection. For Java SE 7 you can use the new try-with-resource syntax to concisely do roughly the right thing, but Execute Around still wins.) – Tom Hawtin - tackline Feb 12 '13 at 23:02
  • A JVM relies on human to write good codes to close every unused db connection. That is is absurd IMHO. Shouldn't a computer help avoid making mistakes by human? – Scott Chu Jun 06 '18 at 07:36

4 Answers4

7

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

Yes.

or the garbage collector will do that automatically?

Unspecified. Maybe some implementations will do that. You can't rely on it.

I am OK with the extra delay that GC will induce.

GC may never happen at all. Are you really OK with infinity?

I just dont want the connections to remain open forever.

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

user207421
  • 305,947
  • 44
  • 307
  • 483
2

Since a DB connection should not be left open and Garbage Collecting may not close it, the Java 7 "project coin" syntax using Try with Resources is helpful. Anything that implements AutoCloseable can be used as demonstrated 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.
Thorn
  • 4,015
  • 4
  • 23
  • 42
1

Yes it is necessary to call close(). There are some JDBC wrappers that can do this type of thing automatically (e.g. Tomcat connection pooling) but in general it is a very good idea to tidy up after yourself wherever resources are involved.

1

Some JDBC connection implementations will close() when they are garbage collected. An example of this is in the Postgres JDBC driver in its finalize() method (code here). There is no guarantee this is the case, though.

But...

There is no way to know when a garbage collection of the connection objects will take place. The garbage collector has no knowledge about JDBC resources, just about memory. This means that a GC may only occur when there is a need to free up memory. If you have 60 connections sitting around but there is still free memory the GC wont run and you'll end up running out of connections.

It is also possible that some kind of JDBC connection pooling is happening. This means that the Connection object you get isn't the real connection but one that is wrapped up in pooling logic. If you don't close() these connections, the pool wont know you are finished with them and wont be able to reuse them for other requests, and the connection pool will be exhausted.

So always close() your connections explicitly if you create them.

prunge
  • 22,460
  • 3
  • 73
  • 80