I'm doing some refactoring for our application, and trying to reduce the number of issues reported in our ISO Scan (a static code analysis tool based on HP Fortify). Right now, what I'm trying to address is the "Unreleased Resource: Database" issues our application has built up. One of the most prominent reasons for this is constructs like this:
Connection conn = null;
Boolean myConn = false;
try{
if(conn == null){
conn = DatabaseUtil.getConnection();
myConn = true;
}
result = DbClass.getObject(conn, otherParameters);
}catch(DatabaseException de){
throw de;
}catch(SQLException sqle){
throw new DatabaseException("Error Message");
}finally{
if(myConn && conn != null){
try{
conn.rollback();
conn.close();
}catch(SQLException sqle){}
}
This is a fairly standard construct in our application to ensure the database connection is closed at the end of our Try block. Yet every time we use it, the tool reports it as an issue.
Is there anything incorrect about this construct, and is there anything we can do to fix it if it is an issue?
try
block, so execution is not guaranteed from a static code analysis perspective.