There is a big full stop for the resource leak in Java.
Instead of breaking our heads trying to properly release used resources after their usage, from Java SE 7 onwards we just need to focus only on the business using those resources. For the past couple of decades, we followed the best practice of releasing the resource using the finally block. Many of the close() function implementations will close themselves, and leave no mark in the system.
However, few times it may do additional tasks to release its parent also. For instance, ResultSet.close() implementations. From JDK 7 onwards, any resource object which extends either java.lang.AutoCloseable or java.lang.Closeable can be instantiated in a try statement. Those resources automatically get closed after the try block execution.
For the resource implementor, it's recommended not to throw any exception while calling close().
And align the resource close operation such that it is closing itself.
Instead of breaking our heads trying to properly release used resources after their usage, from Java SE 7 onwards we just need to focus only on the business using those resources. For the past couple of decades, we followed the best practice of releasing the resource using the
finally block. close() function implementations will close themselves, and leave no mark in the system.However, few times it may do additional tasks to release its parent also. For instance,
ResultSet.close() implementations. java.lang.AutoCloseable or java.lang.Closeable can be instantiated in a try statement. Those resources automatically get closed after the try block execution.For the resource implementor, it's recommended not to throw any exception while calling
close().And align the resource close operation such that it is closing itself.