私たちのコードでは、通常、次のパターンを使用します。
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);
}
}
ただし、findbugs はこれを好みません。conn.close() は例外をスローする可能性があるため、接続が閉じられる保証はありません。findbugs があまりにも衒学的であるか、またはデータベース接続を閉じるためのより良い方法があります。
編集: try catch を close から削除しました。