I have a Spring data
repository:
@Repository
interface SomeRepository extends CrudRepository {
Stream streamBy
As the reference documentation clearly states, Stream
s need to be used with a try-with-resources block.
Also, make sure you keep a (read-only) transaction open for the time of the consumption of the stream by annotating the surrounding method with @Transactional
. Otherwise the default settings apply and the resources are attempted to be freed on repository method return.
@Transactional
public void someMethod() {
try (Stream stream = repository.findAllByCustomQueryAndStream()) {
stream.forEach(…);
}
}