Within our code we usually make use of the following pattern:

Connection conn;
try{
    conn = getConnection();
    //Do databasey stuff
}catch(Exceptions that get thrown){
}finally{
    try{
        conn.close();
    }catch(SQLException ex){
        logger.error("Failed to cleanup database connection",ex);
    }
}

However findbugs does not such as this. Since conn.close() can throw the best then your connection is not certain to be closed. Is findbugs being too pedantic or it is possible to better method to close database connections.

Edit: Added removed try catch around close.

There's already a software application that does what @duffymo pointed out: DbUtils from Apache.

  • DbUtils.close(ResultSet);
  • DbUtils.close(Statement);
  • DbUtils.close(Connection);

The APIDocs shows all available techniques.


Update

Here's a good example:

import org.apache.commons.dbutils;


Connection conn;
try{
    conn = getConnection();
    //Do databasey stuff
} catch(Exception e){
    //throw a glorious exception....
} finally{
    DbUtils.closeQuietly(conn); //This hides the SQLException thrown by conn.close();
    //or 
    //DbUtils.close(conn);
}

Update: As recommended by ArtB, if you are finally closing assets and connections and findBugs has been a nagger, you can include the next annotation (on the top from the method).

@edu.umd.cs.findbugs.annotations.SuppressWarnings("OBL_UNSATISFIED_OBLIGATION")

Yes, there's an easy method.

Produce a static method that systems the near the coast an attemptOrcapture:

public class DatabaseUtils
{
    public static void close(Connection c)
    {
        try
        {
            if (c != null)
            {
                c.close();
            }
        }
        catch (SQLException e)
        {
            // print or log stack trace
        }
    }

    // same for Statement and ResultSet
}

What you want to do would be to mix "The Elite Gentleman"'s answer using the @edu.umd.cs.findbugs.annotations.SuppressWarnings( "OBL_UNSATISFIED_OBLIGATION" ) annotation. FindBugs appears to simply be at liberty should you the entire closing in method within the following manner (that is btw the most well-liked sequence for doing this):

...
}finally{
    try{ 
       resultSet.close();
    }catch( SqlException e ){
       //log error
    }finally{
       try{
          statement.close();
       }catch( SqlException e ){
          //log error
       }finally{
          try{
              connection.close();
          }catch( SqlException e ){
              //log error
          }
       }
    }
}

That is very verbose and also you most likely don't wish to do if for not one other reason compared to passion for your carpal tunnel, thus you need to use the DBUtils.closeQuietly() method (or make your own, your call). However, FindBugs does not recognise this (i.e. while using library or perhaps your own method) as correctly closing the assets and items you an alert. Within this situation it's clearly a false positive. Therefore must make certain it is the only warning you are receiving after which disable that warning for your method.

@edu.umd.cs.findbugs.annotations.SuppressWarnings( "OBL_UNSATISFIED_OBLIGATION" )
public void doStuff( final Connection connection ){
    try{
        //Do databasey stuff
    }catch( SqlException e ){
        //throw a glorious exception....
    }finally{
        DbUtils.closeQuietly( resultSet  );
        DbUtils.closeQuietly( statement  );
        DbUtils.closeQuietly( connection );
}

By doing this you cleanup your assets using the couple of lines of code and steer clear of a FindBugs warning.

Yes, you need to encapsulate your near the coast an attempt block, but there's a cleverer way.

try {
    Connection c = getConnection();
    try {
        //do stuff
    } finally {
        c.close();
    }
} catch (SQLException e) {
    //Catch exceptions
}