Skip to content

Commit f5c484b

Browse files
committed
JAVA-2338: Ensure that the reference to the current batch of results in a cursor is removed before fetching the next batch.
1 parent 6cc3b48 commit f5c484b

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

driver/src/main/com/mongodb/MongoBatchCursorAdapter.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void close() {
4343

4444
@Override
4545
public boolean hasNext() {
46-
return !needsNewBatch() || batchCursor.hasNext();
46+
return curBatch != null || batchCursor.hasNext();
4747
}
4848

4949
@Override
@@ -52,22 +52,20 @@ public T next() {
5252
throw new NoSuchElementException();
5353
}
5454

55-
if (needsNewBatch()) {
55+
if (curBatch == null) {
5656
curBatch = batchCursor.next();
57-
curPos = 0;
5857
}
5958

60-
return curBatch.get(curPos++);
59+
return getNextInBatch();
6160
}
6261

6362
@Override
6463
public T tryNext() {
65-
if (needsNewBatch()) {
64+
if (curBatch == null) {
6665
curBatch = batchCursor.tryNext();
67-
curPos = 0;
6866
}
6967

70-
return curBatch == null ? null : curBatch.get(curPos++);
68+
return curBatch == null ? null : getNextInBatch();
7169
}
7270

7371
@Override
@@ -80,7 +78,14 @@ public ServerAddress getServerAddress() {
8078
return batchCursor.getServerAddress();
8179
}
8280

83-
private boolean needsNewBatch() {
84-
return curBatch == null || curPos == curBatch.size();
81+
private T getNextInBatch() {
82+
T nextInBatch = curBatch.get(curPos);
83+
if (curPos < curBatch.size() - 1) {
84+
curPos++;
85+
} else {
86+
curBatch = null;
87+
curPos = 0;
88+
}
89+
return nextInBatch;
8590
}
8691
}

0 commit comments

Comments
 (0)