Példa
protected void finalize() throws Throwable {
Transaction trans = DBUtility.getTransaction();
try {
trans.commit();
}
catch (RollbackException exc) {
LogUtility.log(exc);
}
catch (HeuristicMixedException exc) {
LogUtility.log(exc);
}
catch (SystemException exc) {
LogUtility.log(exc);
}
catch (HeuristicRollbackException exc) {
LogUtility.log(exc);
}
finally {
super .finalize();
}
}
Megoldás
A finalize() metódus helyett inkább javasolt egy külön, explicit módon meghívott metódus írása, ami elvégzi takarítást.
public void free() {
Transaction trans = DBUtility.getTransaction();
try {
trans.commit();
}
catch (RollbackException exc) {
LogUtility.log(exc);
}
catch (HeuristicMixedException exc) {
LogUtility.log(exc);
}
catch (SystemException exc) {
LogUtility.log(exc);
}
catch (HeuristicRollbackException exc) {
LogUtility.log(exc);
}
}