Skip to content

Commit 45d8419

Browse files
authored
fix: iterate over async result set in sync (#416)
Iterating over an AsyncResultSet using a standard while (rs.next()) loop would fail after the last row was consumed.
1 parent af95630 commit 45d8419

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AsyncResultSetImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ public boolean next() throws SpannerException {
559559
this.state = State.SYNC;
560560
}
561561
boolean res = delegateResultSet.next();
562-
currentRow = delegateResultSet.getCurrentRowAsStruct();
562+
currentRow = res ? delegateResultSet.getCurrentRowAsStruct() : null;
563563
return res;
564564
}
565565

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,19 @@ public CallbackResponse cursorReady(AsyncResultSet resultSet) {
225225
assertThat(rowCount.get()).isEqualTo(1);
226226
}
227227

228+
@Test
229+
public void singleUseAsyncWithoutCallback() throws Exception {
230+
DatabaseClient client =
231+
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
232+
int rowCount = 0;
233+
try (AsyncResultSet rs = client.singleUse().executeQueryAsync(SELECT1)) {
234+
while (rs.next()) {
235+
rowCount++;
236+
}
237+
}
238+
assertThat(rowCount).isEqualTo(1);
239+
}
240+
228241
@Test
229242
public void singleUseBound() {
230243
DatabaseClient client =

0 commit comments

Comments
 (0)