File tree Expand file tree Collapse file tree 2 files changed +21
-7
lines changed
driver/src/main/com/mongodb Expand file tree Collapse file tree 2 files changed +21
-7
lines changed Original file line number Diff line number Diff line change 36
36
import static java .util .concurrent .TimeUnit .MILLISECONDS ;
37
37
38
38
/**
39
- * <p>An iterator over database results. Doing a {@code find()} query on a collection returns a {@code DBCursor} thus</p>
40
- * <pre>
41
- * DBCursor cursor = collection.find(query);
42
- * if(cursor.hasNext()) {
43
- * DBObject obj = cursor.next();
39
+ * <p>An iterator over database results. Doing a {@code find()} query on a collection returns a {@code DBCursor}.</p>
40
+ * <p> An application should ensure that a cursor is closed in all circumstances, e.g. using a try-with-resources statement:</p>
41
+ * <blockquote><pre>
42
+ * try (DBCursor cursor = collection.find(query)) {
43
+ * while (cursor.hasNext()) {
44
+ * System.out.println(cursor.next();
45
+ * }
44
46
* }
45
- * </pre>
47
+ * </pre></blockquote>
48
+ *
46
49
* <p><b>Warning:</b> Calling {@code toArray} or {@code length} on a DBCursor will irrevocably turn it into an array. This means that, if
47
50
* the cursor was iterating over ten million results (which it was lazily fetching from the database), suddenly there will be a ten-million
48
51
* element array in memory. Before converting to an array, make sure that there are a reasonable number of results using {@code skip()} and
Original file line number Diff line number Diff line change 24
24
import java .util .Iterator ;
25
25
26
26
/**
27
- * The Mongo Cursor interface implementing the iterator protocol
27
+ * The Mongo Cursor interface implementing the iterator protocol.
28
+ * <p>
29
+ * An application should ensure that a cursor is closed in all circumstances, e.g. using a try-with-resources statement:
30
+ *
31
+ * <blockquote><pre>
32
+ * try (MongoCursor<Document> cursor = collection.find().iterator()) {
33
+ * while (cursor.hasNext()) {
34
+ * System.out.println(cursor.next());
35
+ * }
36
+ * }
37
+ * </pre></blockquote>
28
38
*
29
39
* @since 3.0
30
40
* @param <TResult> The type of documents the cursor contains
31
41
*/
32
42
@ NotThreadSafe
33
43
public interface MongoCursor <TResult > extends Iterator <TResult >, Closeable {
44
+
34
45
@ Override
35
46
void close ();
36
47
You can’t perform that action at this time.
0 commit comments