The right way to lazily consume a non-thread-safe source

I need to fetch from the database a large amount of data and make a stream from it. But ScrollableResult from Hibernate and Cursor from MyBatis bounded to a session that is not thread-safe and must be closed. How to do such things right?

Source.unfoldResource should be your friend for such tasks, see docs: Source.unfoldResource • Akka Documentation

1 Like

Many thanks! This is exactly what I need.