What is considered best practices when cleaning up JDBC resources and why? I kept the example short, thus just the cleaning up of the ResultSet.
finally
{
As others have pointed out, JDBC resources (statements, result sets, etc...) are rarely null
. If they are, you have bigger issues on your hands than NullPointerException
s. In that regard, the NullPointerException
s will help alert you to severe problems with your JDBC driver. The typical checking for null
before calling close()
would silently hide the problem if your JDBC driver was, in fact, providing you with null
references.
As well, not all JDBC drivers follow the specification precisely. For example, some drivers will not automatically close a ResultSet
when it's associated Statement
is closed. Therefore, you have to ensure that you explicitly close both the ResultSet
and its Statement
(sigh).
In practice, I have found this technique useful (although its not the prettiest):
PreparedStatement statement = connection.prepareStatement("...");
try {
ResultSet results = statement.executeQuery();
try {
while (results.next()) {
// ...
}
} finally {
results.close();
}
} finally {
statement.close();
}
This technique guarantees that every close()
statement is executed, starting with the ResultSet
and working its way outward. NullPointerException
s are still thrown should the driver provide you with null
references, but I allow this for the reasons explained at the beginning. SQLException
s are still thrown if any of the close()
statements fail (I consider this a good thing - I want to know if something is going wrong).
I tend to use the following approach. I think it is good to check for null
because it shows your intent i.e. that you do realise that these objects could be null in rare cases. (A null check is also faster than the creation of a NullPointerException
.) I also think it is good to log the exceptions, instead of swallowing them. In the cases where close
fails, I want to know about it and have it in my log files.
finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
LOG.warn("Failed to close rs", e);
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
LOG.warn("Failed to close st", e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
LOG.warn("Failed to close conn", e);
}
}
}
If you are going to be doing this frequently, instead of copying and pasting this code over and over again, create a utility class with static methods to close the ResultSet, Statement and Connection.
With DBUtils you can perform this cleanup quite concisely as follows:
finally {
DBUtils.closeQuietly(rs);
DBUtils.closeQuietly(st);
DBUtils.closeQuietly(conn);
}
I see no problem with your second (uncommon) version.
null
, so an NPE will occur in rare cases. So I see no performance problem here.rs = null
The only disadvantage - if we have more then one resource to close, then we'd have to add one try/catch for each resource, if we want to close as many resources as possible. Otherwise, we'd enter the catch clause with the first null
and that could cause undiscored leaks.
So it would look like that:
finally {
try{rs.close(); }catch(Exception ignored){}
try{stmt.close();}catch(Exception ignored){}
try{conn.close();}catch(Exception ignored){}
}
... which is still readable and understandable. But, according to never change a common pattern - I'd stick to the old-fashioned way of testing null
first and catching SQLException
while closing.