Skip to content

Commit c0ae698

Browse files
authored
DOCSP-49095: Retryable Reads and Writes (#585)
1 parent dae52bf commit c0ae698

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

source/crud/configure.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,47 @@ The following example sets the read preference to ``ReadPreference.Secondary`` f
191191
For more information about read preference, see :manual:`Read Preference
192192
</core/read-preference/>` in the {+mdb-server+} manual.
193193

194+
Retryable Reads and Writes
195+
--------------------------
196+
197+
The {+driver-short+} automatically retries certain read and write operations a single time
198+
if they fail due to a network or server error.
199+
200+
You can explicitly disable retryable reads or retryable writes by setting the ``RetryReads``
201+
or ``RetryWrites`` options on a ``MongoClientSettings`` object and passing this object to the
202+
``MongoClient`` constructor. You can also set the ``retryReads`` or ``retryWrites`` options
203+
in a connection string.
204+
205+
The following example disables retryable reads and writes for
206+
a client. Select the :guilabel:`MongoClientSettings` or :guilabel:`Connection String`
207+
tab to see the corresponding code.
208+
209+
.. tabs::
210+
211+
.. tab:: MongoClientSettings
212+
:tabid: mongoclientsettings
213+
214+
.. literalinclude:: /includes/fundamentals/code-examples/ReplicaSetConfigs.cs
215+
:start-after: start-retry-reads-writes
216+
:end-before: end-retry-reads-writes
217+
:emphasize-lines: 2-3
218+
:language: csharp
219+
:dedent:
220+
221+
.. tab:: Connection String
222+
:tabid: connectionstring
223+
224+
.. literalinclude:: /includes/fundamentals/code-examples/ReplicaSetConfigs.cs
225+
:start-after: start-retry-reads-writes-connection-string
226+
:end-before: end-retry-reads-writes-connection-string
227+
:emphasize-lines: 1
228+
:language: csharp
229+
:dedent:
230+
231+
To learn more about supported retryable read and retryable write operations, see
232+
:manual:`Retryable Reads </core/retryable-reads/>`
233+
and :manual:`Retryable Writes </core/retryable-writes/>` in the {+mdb-server+} manual.
234+
194235
API Documentation
195236
-----------------
196237

source/includes/fundamentals/code-examples/ReplicaSetConfigs.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static void Main(string[] args)
88
var client = new MongoClient("mongodb://localhost:27017");
99
{
1010
// start-write-concern-client
11-
var mongoClientSettings = MongoClientSettings.FromConnectionString("<connection string URI>");
11+
var mongoClientSettings = MongoClientSettings.FromConnectionString("<connection URI>");
1212
mongoClientSettings.WriteConcern = WriteConcern.WMajority;
1313
var mongoClient = new MongoClient(mongoClientSettings);
1414
// end-write-concern-client
@@ -24,7 +24,7 @@ public static void Main(string[] args)
2424

2525
{
2626
// start-read-concern-client
27-
var mongoClientSettings = MongoClientSettings.FromConnectionString("<connection string URI>");
27+
var mongoClientSettings = MongoClientSettings.FromConnectionString("<connection URI>");
2828
mongoClientSettings.ReadConcern = ReadConcern.Majority;
2929
var mongoClient = new MongoClient(mongoClientSettings);
3030
// end-read-concern-client
@@ -40,7 +40,7 @@ public static void Main(string[] args)
4040

4141
{
4242
// start-read-preference-client
43-
var mongoClientSettings = MongoClientSettings.FromConnectionString("<connection string URI>");
43+
var mongoClientSettings = MongoClientSettings.FromConnectionString("<connection URI>");
4444
mongoClientSettings.ReadPreference = ReadPreference.Secondary;
4545
var mongoClient = new MongoClient(mongoClientSettings);
4646
// end-read-preference-client
@@ -53,5 +53,22 @@ public static void Main(string[] args)
5353
.WithReadPreference(ReadPreference.Secondary);
5454
// end-read-preference-collection
5555
}
56+
57+
{
58+
// start-retry-reads-writes
59+
var mongoClientSettings = MongoClientSettings.FromConnectionString("<connection URI>");
60+
mongoClientSettings.RetryReads = false;
61+
mongoClientSettings.RetryWrites = false;
62+
var mongoClient = new MongoClient(mongoClientSettings);
63+
// end-retry-reads-writes
64+
}
65+
66+
{
67+
// start-retry-reads-writes-connection-string
68+
var connectionString = "mongodb://localhost:27017/?retryReads=false&retryWrites=false";
69+
var mongoClientSettings = MongoClientSettings.FromConnectionString(connectionString);
70+
var mongoClient = new MongoClient(mongoClientSettings);
71+
// end-retry-reads-writes-connection-string
72+
}
5673
}
5774
}

0 commit comments

Comments
 (0)