Ejemplo
protected void finalize() throws Throwable {
Connection conn = null;
try {
DataSource ds = getDataSource();
conn = ds.getConnection();
// ...
conn.commit();
}
catch (SQLException exc) {
LogUtility.log(exc);
}
finally {
if (conn != null) {
try {
conn.close();
}
catch (SQLException exc) {
LogUtility.log(exc);
}
}
super .finalize();
}
}

private DataSource getDataSource() {
return _source;
}

private transient DataSource _source;
Solución
Utilice un método de borrado que se invoque explícitamente en lugar del método finalize().
public void free() {
Connection conn = null;
try {
DataSource ds = getDataSource();
conn = ds.getConnection();
// ...
conn.commit();
}
catch (SQLException exc) {
LogUtility.log(exc);
}
finally {
if (conn != null) {
try {
conn.close();
}
catch (SQLException exc) {
LogUtility.log(exc);
}
}
}
}

private DataSource getDataSource() {
return _source;
}

private transient DataSource _source;