From 642dc13b5f3600568545825414eb5dba12f4e184 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Thu, 12 Jun 2025 12:37:21 -0700 Subject: [PATCH 1/3] add section to configure crud page --- source/crud/configure.txt | 21 +++++++++++ .../CRUD/retryableReadsWrites.go | 37 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go diff --git a/source/crud/configure.txt b/source/crud/configure.txt index 398b62d4..8bce2688 100644 --- a/source/crud/configure.txt +++ b/source/crud/configure.txt @@ -19,3 +19,24 @@ Configure CRUD Operations .. TODO +Retryable Reads and Writes +-------------------------- + +{+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: + +.. 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 ` +in the {+mdb-server+} manual. To learn more about supported retryable write +operations, see :manual:`Retryable Writes ` in the {+mdb-server+} manual. \ No newline at end of file diff --git a/source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go b/source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go new file mode 100644 index 00000000..274a323c --- /dev/null +++ b/source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go @@ -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 and connects to the server + 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) + } + }() +} From bb9a33194fb992f0bcc8e7151789024ab91c86ec Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Thu, 12 Jun 2025 14:14:14 -0700 Subject: [PATCH 2/3] edits --- source/crud/configure.txt | 7 ++++--- .../code-snippets/CRUD/retryableReadsWrites.go | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/source/crud/configure.txt b/source/crud/configure.txt index 8bce2688..b2f14b9c 100644 --- a/source/crud/configure.txt +++ b/source/crud/configure.txt @@ -22,14 +22,15 @@ Configure CRUD Operations Retryable Reads and Writes -------------------------- -{+driver-short+} automatically retries certain read and write operations a single time +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 +``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: +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 diff --git a/source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go b/source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go index 274a323c..29baf8e6 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go +++ b/source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go @@ -22,7 +22,7 @@ func main() { SetRetryWrites(false). SetRetryReads(false) - // Creates a new client and connects to the server + // Creates a new client using the specified options client, err := mongo.Connect(clientOps) if err != nil { panic(err) From 064ebfb03e38c641babbf8da00fa6aefb194cbcd Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Thu, 12 Jun 2025 16:00:46 -0700 Subject: [PATCH 3/3] fix typo --- source/crud/configure.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/crud/configure.txt b/source/crud/configure.txt index b2f14b9c..63233a50 100644 --- a/source/crud/configure.txt +++ b/source/crud/configure.txt @@ -22,7 +22,7 @@ Configure CRUD Operations Retryable Reads and Writes -------------------------- -The +driver-short+} automatically retries certain read and write operations a single time +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