Fix iterating over buffered ResultSet

Test case and fix for issue #6846