Skip to content

Commit 35c6def

Browse files
authored
DOCSP-47006 Retryable Writes and Reads (#518)
1 parent cde5481 commit 35c6def

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

source/crud/configure.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,25 @@ Configure CRUD Operations
1919

2020
.. TODO
2121

22+
Retryable Reads and Writes
23+
--------------------------
24+
25+
The {+driver-short+} automatically retries certain read and write operations a single time
26+
if they fail due to a network or server error.
27+
28+
You can explicitly disable retryable reads or retryable writes by setting the
29+
``RetryReads`` or ``RetryWrites`` option to ``False`` when creating a new client
30+
by using the ``options.Client`` struct.
31+
32+
The following example disables retryable reads and writes for a client by using
33+
the ``ClientOptions`` setter functions:
34+
35+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go
36+
:start-after: start-retryable-example
37+
:end-before: end-retryable-example
38+
:language: go
39+
:dedent:
40+
41+
To learn more about supported retryable read operations, see :manual:`Retryable Reads </core/retryable-reads/>`
42+
in the {+mdb-server+} manual. To learn more about supported retryable write
43+
operations, see :manual:`Retryable Writes </core/retryable-writes/>` in the {+mdb-server+} manual.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"os"
7+
8+
"go.mongodb.org/mongo-driver/v2/mongo"
9+
"go.mongodb.org/mongo-driver/v2/mongo/options"
10+
)
11+
12+
func main() {
13+
var uri string
14+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
15+
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")
16+
}
17+
18+
// start-retryable-example
19+
// Defines the client options
20+
clientOps := options.Client().
21+
ApplyURI(uri).
22+
SetRetryWrites(false).
23+
SetRetryReads(false)
24+
25+
// Creates a new client using the specified options
26+
client, err := mongo.Connect(clientOps)
27+
if err != nil {
28+
panic(err)
29+
}
30+
// end-retryable-example
31+
32+
defer func() {
33+
if err = client.Disconnect(context.TODO()); err != nil {
34+
panic(err)
35+
}
36+
}()
37+
}

0 commit comments

Comments
 (0)