1

Statement statement; Connection connection;

I am closing connection after every database operation as below.

connection.close();

i created statement object as below.

connection.createStatement(....)

Do i need to close satement also similar to connection closing?

I mean do i need to call statement.close();?

What will happen if it is not called?

Thanks!

user1016403
  • 12,151
  • 35
  • 108
  • 137
  • Duplicate http://stackoverflow.com/questions/2225221/closing-database-connections-in-java – Bitmap May 09 '12 at 10:16
  • There is a simmilar post. I hope it helps. - http://stackoverflow.com/questions/4507440/must-jdbc-resultsets-and-statements-be-closed-separately-although-the-connection – bhdrkn May 09 '12 at 10:20
  • 1
    One problem with just closing the connection and not closing the result set (or `connection.createStatement()`), is that if your *connection management code* is using **connection pooling** then the `connection.close()` would just put the connection back in the *pool*. Additionally, some databases have a *cursor resource* on the server that will not be freed (released) properly unless it is **explicitly** closed. – Lion May 09 '12 at 10:27
  • @Lion, good comment. Also, if you are not using connection management, consider keeping the connection and re-using it when feasible to do so. It can take a long time to re-establish a connection. (Logging in in is the second slowest operation in databases. The slowest is logging out.) – Marlin Pierce May 10 '12 at 14:09
  • 1
    @Lion Any decent connection pool will close resources that were created for a logical connection when the physical connection is returned to the pool (maybe with the exception of a pooled Statement, but then that wouldn't be closed either when the logical Statement object is closed). – Mark Rotteveel May 12 '12 at 07:43

2 Answers2

5

The Javadoc says it all:

Statement.close()

Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. It is generally good practice to release resources as soon as you are finished with them to avoid tying up database resources.

One good way to ensure that close() is always called is by placing it inside a finally block:

Statement stmt = connection.createStatement();
try {
    // use `stmt'
} finally {
    stmt.close();
}

In Java 7, the above can be more concisely written as:

try (Statement stmt = connection.createStatement()) {
    // ...
}

This is called the try-with-resources statement.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Or, if using Java 7, to use the try-with-resources construct. See the end of this page: http://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html – JB Nizet May 09 '12 at 11:36
  • @JBNizet: Thanks for the suggestion! I've added this to the answer. – NPE May 09 '12 at 11:53
0

In general if you close a connection, all associated resources will be closed as well, provided the driver implementer did his work correctly. That said: it is better to close resources when your done with them, as that will free up resources both on your side and the database.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197