Skip to content

DOCSP-47006 Retryable Writes and Reads #518

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
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
22 changes: 22 additions & 0 deletions source/crud/configure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,25 @@ Configure CRUD Operations

.. TODO

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`` option to ``False`` when creating a new client
by using the ``options.Client`` struct.

The following example disables retryable reads and writes for a client by using
the ``ClientOptions`` setter functions:

.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go
:start-after: start-retryable-example
:end-before: end-retryable-example
:language: go
:dedent:

To learn more about supported retryable read operations, see :manual:`Retryable Reads </core/retryable-reads/>`
in the {+mdb-server+} manual. To learn more about supported retryable write
operations, see :manual:`Retryable Writes </core/retryable-writes/>` in the {+mdb-server+} manual.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"context"
"log"
"os"

"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)

func main() {
var uri string
if uri = os.Getenv("MONGODB_URI"); uri == "" {
log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable")
}

// start-retryable-example
// Defines the client options
clientOps := options.Client().
ApplyURI(uri).
SetRetryWrites(false).
SetRetryReads(false)

// Creates a new client using the specified options
client, err := mongo.Connect(clientOps)
if err != nil {
panic(err)
}
// end-retryable-example

defer func() {
if err = client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()
}
Loading