Példa
protected void finalize() throws Throwable {
Connection connection = null;
try {
Driver driver = DBUtility.getDriver();
connection = driver.connect(DBUtility.getDBURL(), DBUtility.getUserProperties());
// ...
connection.commit();
}
catch (SQLException exc) {
LogUtility.log(exc);
}
finally {
if (connection != null) {
try {
connection.close();
}
catch (SQLException exc) {
LogUtility.log(exc);
}
}
super .finalize();
}
}
Megoldás
A finalize() metódus helyett használjon egy kifejezetten meghívott takarító metódust.
public void free() {
Connection connection = null;
try {
Driver driver = DBUtility.getDriver();
connection = driver.connect(DBUtility.getDBURL(), DBUtility.getUserProperties());
// néhány művelet befejezése
connection.commit();
}
catch (SQLException exc) {
LogUtility.log(exc);
}
finally {
if (connection != null) {
try {
connection.close();
}
catch (SQLException exc) {
LogUtility.log(exc);
}
}
}
}