Ajeet Ganga
Ajeet Ganga

Reputation: 8653

Do the java.sql.Connection objects get automatically closed when they get garbage collected?

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.

Upvotes: 3

Views: 3341

Answers (4)

Thorn
Thorn

Reputation: 4057

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.

Upvotes: 2

prunge
prunge

Reputation: 23268

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.

Upvotes: 1

user207421
user207421

Reputation: 311050

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."

Upvotes: 7

user1596371
user1596371

Reputation:

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.

Upvotes: 1

Related Questions