Hi,
I'm trying to return an Iterator<> using the fluent interface so that I can process large amounts of data in chunks. I'm using PostgreSQL. I know you need to set AutoCommit to false to use a cursor or use a transaction (which sets AutoCommit to false internally) but I don't want this to effect all connections in my application and therefore want to reset it afterwards. If I try to reset AutoCommit in a finally (or use a transaction) I get an error such as:
PSQLException: ERROR: portal C_2 does not exist
This is due to the connection being closed before the Iterator has finished fetching. The basic code looks like:
public Iterator<Stuff> getAll() {
try (Handle handler = jdbi.open()) {
return handler.createQuery("SELECT stuff FROM thing")
.setFetchSize(200000)
.map(new StuffMapper())
.iterator();
}
}
How can I use a Cursor with an Iterator using the fluent interface?
I'm sure i'm missing something simple. I've tried:
- Setting AutoCommit to false and then setting it to true in a finally
- Wrapping this in an inTransaction
- Returning a Stream instead of an Iterator and using StreamSupport to hook into onClose and trying to set it there. Connection was already closed :(
- Using a StatementCustomizer and trying to do it on cleanUp
As you can see, my efforts are getting more and more desperate :)
Thanks if anyone can help,