I recently started with project and I see following pattern is used a lot in several classes to close connection, statement and result set.
finally
{
result.close();
close();
}
result.close() invokes following method.
public void close() throws SQLException
{
SQLException resultException = null;
if (resultSet != null)
{
try
{
resultSet.close();
}
catch (SQLException e)
{
resultException = e;
}
}
if (statement != null)
{
try
{
statement.close();
}
catch (SQLException e)
{
resultException = e;
}
}
resultSet = null;
statement = null;
if (resultException != null)
{
throw resultException;
}
}
close() invokes following method.
public void close() throws SQLException
{
if (connection != null && !connection.isClosed())
{
connection.close();
}
connection = null;
}
I think there is a possibility of connection leak, if an exception is thrown while closing statement and resultset. Can I just catch it and log and move to closing connecton? Or what should I do if it throw SQLException?