Java Reference
In-Depth Information
float cost = rs.getFloat(Taxer.COST_COLUMN);
sum = sum + cost * 8 / 100;
}
con.close();
o Closing the connection
(the split cleaner!)
} catch (Throwable theException) {
theException.printStackTrace();
}
return sum;
}
}
A good clue that this application is poorly designed is that the objects are
named after processes instead of entities. Other forms of the Split Cleaner are
subtler, spreading connection management across different well-formed meth-
ods. When connection management is distributed, it's easier to lose the con-
nection cleanup and create another kind of leak. For this example, if it was
determined that tax may or may not be charged, then a condition could be
added to the Adder class to add tax only if the condition is met. In this case,
we'd leave an open connection. In fact, as our application grows in complex-
ity, we'll have to add cleanup to every branch that uses the resource.
7.3.1
Exceptions can lead to Split Cleaners
Another variation of the Split Cleaner is exception processing. In our pro-
grams so far, cleanup has occurred at the end of a command's execute
method, like this:
public void execute()
throws
IOException,
DataException {
try {
// retrieve data from the database
Statement statement = connection.createStatement();
result =
statement.executeQuery("SELECT subject, author, board from posts");
while (result.next()) {
subject.addElement(result.getString(SUBJECT_COLUMN));
author.addElement(result.getString(AUTHOR_COLUMN));
board.addElement(result.getString(BOARD_COLUMN));
}
result.close();
statement.close();
connection.close();
} catch (Throwable theException) {
Search WWH ::




Custom Search