in the Spring Data JPA docs it says regarding streams:
A Stream potentially wraps underlying data store specific resources and must therefore be closed after usage. You can either manually close the Stream using the close() method or by using a Java 7 try-with-resources block.
If I process a stream with forEach
, a count or another terminal operation, it should already be closed (and not be reused again) and I wouldn't have to wrap the stream in additional try-resources-block (given that my blocks don't throw any exception), or am I wrong here?
The Java APIs describe this topic as follows:
Also note the API for
Files.lines(Path, Charset))
:Bottom line is: if the stream corresponds to a resource that, in normal scenarios need to be closed after use (like IO), use it in a try-with-resources statement.