I tried my luck to find a way to register a reliable thread "termination", which is triggered at full flow rate or in any exception, but this is not possible, unfortunately: Register Stream "completion" hook
So, really, the code you showed is incorrect. The right way to work with lazy threads (or Cursor ) is to use the try-with-resources statement. The following two equivalents:
// Using a Stream try (Stream<CustomerRecord> stream = dslContext.selectFrom(CUSTOMER).stream()) { return stream.map(CustomerInfo::new).collect(Collectors.toList()); } // Using a Cursor try (Cursor<CustomerRecord> cursor = dslContext.selectFrom(CUSTOMER).fetchLazy()) { return cursor.stream().map(CustomerInfo::new).collect(Collectors.toList()); }
Also note the ResultQuery.stream() javadoc:
This is essentially the same as fetchLazy (), but instead of returning a cursor, a Java 8 stream is returned. Clients must ensure that the stream closes correctly. in a try-with-resources statement
source share