Should a database connection stay open all the time or only be opened when needed?

The database connection must be opened only when its needed and closed after doing all the necessary job with it. Code sample:

  • Prior to Java 7:

      Connection con = null;
      try {
          con = ... //retrieve the database connection
          //do your work...
      } catch (SQLException e) {
          //handle the exception
      } finally {
          try {
              if (con != null) {
                  con.close();
              }
          } catch (SQLException shouldNotHandleMe) {
              //...
          }
      }
    
  • Java 7:

      try (Connection con = ...) {
      } catch (SQLException e) {
      }
      //no need to call Connection#close since now Connection interface extends Autocloseable
    

But since manually opening a database connection is too expensive, it is highly recommended to use a database connection pool, represented in Java with DataSource interface. This will handle the physical database connections for you and when you close it (i.e. calling Connection#close), the physical database connection will just be in SLEEP mode and still be open.

Related Q/A:

  • Java Connection Pooling

Some tools to handle database connection pooling:

  • BoneCP
  • c3po
  • Apache Commons DBCP
  • HikariCP

Depends on what are your needs.

Creating a connection takes some time, so if you need to access database frequently it's better to keep the connection open. Also it's better to create a pool, so that many users can access database simultaneously(if it's needed).

If you need to use this connection only few times you may not keep it open, but you will have delay when you would like to access database. So i suggest you to make a timer that will keep connection open for some time(connection timeout).


You need to close your connections after each query executions.Sometimes you need to execute multiple queries at the same time because the queries are hanging from each other.Such as "first insert task then assign it to the employees".At this time execute your queries on the same transaction and commit it, if some errors occur then rollback.By default autocommit is disabled in JDBC. Example

Use connection pooling.If you are developing a webapplication then use App Server connection pooling.App server will use the same pooling for each of your applications so you can control the connection count from the one point.Highly recommend the Apache Tomcat Connection pooling.Example

As an additional info: Connection, Statement and ResultSet.

1.If you close connection you don't need close statement or resultset.Both of them will be closed automatically

2.If you close Statement it will close ResultSet also

3.if you use try-with-resources like this:

try (Connection con = ...) {
} catch (SQLException e) {
}

it will close the connection automatically.Because try-with-resources require autoclosable objects and Connection is autocloseable.You can see the details about try-with-resources here