Skip to content

DOCSP-49095: Retryable Reads and Writes #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions source/crud/configure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,47 @@ The following example sets the read preference to ``ReadPreference.Secondary`` f
For more information about read preference, see :manual:`Read Preference
</core/read-preference/>` in the {+mdb-server+} manual.

Retryable Reads and Writes
--------------------------

The {+driver-short+} automatically retries certain read and write operations a single time
if they fail due to a network or server error.

You can explicitly disable retryable reads or retryable writes by setting the ``RetryReads``
or ``RetryWrites`` options on a ``MongoClientSettings`` object and passing this object to the
``MongoClient`` constructor. You can also set the ``retryReads`` or ``retryWrites`` options
in a connection string.

The following example disables retryable reads and writes for
a client. Select the :guilabel:`MongoClientSettings` or :guilabel:`Connection String`
tab to see the corresponding code.

.. tabs::

.. tab:: MongoClientSettings
:tabid: mongoclientsettings

.. literalinclude:: /includes/fundamentals/code-examples/ReplicaSetConfigs.cs
:start-after: start-retry-reads-writes
:end-before: end-retry-reads-writes
:emphasize-lines: 2-3
:language: csharp
:dedent:

.. tab:: Connection String
:tabid: connectionstring

.. literalinclude:: /includes/fundamentals/code-examples/ReplicaSetConfigs.cs
:start-after: start-retry-reads-writes-connection-string
:end-before: end-retry-reads-writes-connection-string
:emphasize-lines: 1
:language: csharp
:dedent:

To learn more about supported retryable read and retryable write operations, see
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it would be worth mentioning here or if there is a more appropriate place, but should also say that those can be set already in the connection string?

:manual:`Retryable Reads </core/retryable-reads/>`
and :manual:`Retryable Writes </core/retryable-writes/>` in the {+mdb-server+} manual.

API Documentation
-----------------

Expand Down
23 changes: 20 additions & 3 deletions source/includes/fundamentals/code-examples/ReplicaSetConfigs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static void Main(string[] args)
var client = new MongoClient("mongodb://localhost:27017");
{
// start-write-concern-client
var mongoClientSettings = MongoClientSettings.FromConnectionString("<connection string URI>");
var mongoClientSettings = MongoClientSettings.FromConnectionString("<connection URI>");
mongoClientSettings.WriteConcern = WriteConcern.WMajority;
var mongoClient = new MongoClient(mongoClientSettings);
// end-write-concern-client
Expand All @@ -24,7 +24,7 @@ public static void Main(string[] args)

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

{
// start-read-preference-client
var mongoClientSettings = MongoClientSettings.FromConnectionString("<connection string URI>");
var mongoClientSettings = MongoClientSettings.FromConnectionString("<connection URI>");
mongoClientSettings.ReadPreference = ReadPreference.Secondary;
var mongoClient = new MongoClient(mongoClientSettings);
// end-read-preference-client
Expand All @@ -53,5 +53,22 @@ public static void Main(string[] args)
.WithReadPreference(ReadPreference.Secondary);
// end-read-preference-collection
}

{
// start-retry-reads-writes
var mongoClientSettings = MongoClientSettings.FromConnectionString("<connection URI>");
mongoClientSettings.RetryReads = false;
mongoClientSettings.RetryWrites = false;
var mongoClient = new MongoClient(mongoClientSettings);
// end-retry-reads-writes
}

{
// start-retry-reads-writes-connection-string
var connectionString = "mongodb://localhost:27017/?retryReads=false&retryWrites=false";
var mongoClientSettings = MongoClientSettings.FromConnectionString(connectionString);
var mongoClient = new MongoClient(mongoClientSettings);
// end-retry-reads-writes-connection-string
}
}
}
Loading