Skip to content

Missing cleanup contract (ARCH4082)

Key java.performance.dispose-pattern · Severity warning · Category performance

If a class holds an AutoCloseable field but does not itself implement AutoCloseable, there is no way to release the resource it owns, so it leaks.

public class Repository {
private final Connection conn = openConnection();
// no way to close conn
}
public class Repository implements AutoCloseable {
private final Connection conn = openConnection();
@Override
public void close() throws SQLException {
conn.close();
}
}

Implement AutoCloseable on classes owning closeable fields and close those fields in close().