Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change the Cursor to auto close the database cursor after a documents…

… where enumerated. This should prevent a lot open database cursors when people are forgetting to dispose the cursor.

Add KeepCursor method to the Cursor which can disable the above behavior.
  • Loading branch information...
commit 64c782d06420310081585ad9f9027a04ce0ddd4b 1 parent 148d713
Steve Wagner lanwin authored
23 source/MongoDB/Cursor_1.cs
View
@@ -22,6 +22,7 @@ public class Cursor<T> : ICursor<T> where T : class
private QueryOptions _options;
private ReplyMessage<T> _reply;
private int _skip;
+ private bool _keepCursor;
private readonly ISerializationFactory _serializationFactory;
/// <summary>
@@ -164,7 +165,22 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
TryModify();
AddOrRemoveSpecOpt("$hint", index);
return this;
- }
+ }
+
+ /// <summary>
+ /// Keeps the cursor open.
+ /// </summary>
+ /// <param name="value">if set to <c>true</c> [value].</param>
+ /// <returns></returns>
+ /// <remarks>
+ /// By default cursors are closed automaticly after documents
+ /// are Enumerated.
+ /// </remarks>
+ public ICursor<T> KeepCursor(bool value)
+ {
+ _keepCursor = value;
+ return this;
+ }
/// <summary>
/// Snapshots the specified index.
@@ -173,7 +189,7 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
TryModify();
AddOrRemoveSpecOpt("$snapshot", true);
return this;
- }
+ }
/// <summary>
/// Explains this instance.
@@ -223,6 +239,9 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
yield return document;
}
while(Id > 0 && _limit<CursorPosition);
+
+ if(!_keepCursor)
+ Dispose(true);
}
}
11 source/MongoDB/ICursor_1.cs
View
@@ -79,6 +79,17 @@ public interface ICursor<T> : IDisposable
ICursor<T> Hint(object index);
/// <summary>
+ /// Keeps the cursor open.
+ /// </summary>
+ /// <param name="value">if set to <c>true</c> [value].</param>
+ /// <returns></returns>
+ /// <remarks>
+ /// By default cursors are closed automaticly after documents
+ /// are Enumerated.
+ /// </remarks>
+ ICursor<T> KeepCursor(bool value);
+
+ /// <summary>
/// Snapshots this instance.
/// </summary>
/// <returns></returns>
16 source/MongoDB/Obsolete/Cursor.cs
View
@@ -1,3 +1,4 @@
+using System;
using System.Collections.Generic;
namespace MongoDB
@@ -117,6 +118,21 @@ public long Id
}
/// <summary>
+ /// Keeps the cursor open.
+ /// </summary>
+ /// <param name="value">if set to <c>true</c> [value].</param>
+ /// <returns></returns>
+ /// <remarks>
+ /// By default cursors are closed automaticly after documents
+ /// are Enumerated.
+ /// </remarks>
+ public ICursor KeepCursor(bool value)
+ {
+ _cursor.KeepCursor(value);
+ return this;
+ }
+
+ /// <summary>
/// Snapshots this instance.
/// </summary>
/// <returns></returns>
15 source/MongoDB/Obsolete/ICursor.cs
View
@@ -74,9 +74,20 @@ public interface ICursor : IDisposable
/// Hints the specified index.
/// </summary>
/// <param name="index">The index.</param>
- /// <returns></returns>
+ /// <returns></returns>
ICursor Hint(Document index);
-
+
+ /// <summary>
+ /// Keeps the cursor open.
+ /// </summary>
+ /// <param name="value">if set to <c>true</c> [value].</param>
+ /// <returns></returns>
+ /// <remarks>
+ /// By default cursors are closed automaticly after documents
+ /// are Enumerated.
+ /// </remarks>
+ ICursor KeepCursor(bool value);
+
/// <summary>
/// Snapshots this instance.
/// </summary>
Please sign in to comment.
Something went wrong with that request. Please try again.