Skip to content

Commit 062e9b5

Browse files
committed
docs(session): document read_concern option in session creation
1 parent 99cad7f commit 062e9b5

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

lib/mongo.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,21 @@ defmodule Mongo do
10371037

10381038
@doc """
10391039
Start new session for given `topology_pid`.
1040+
1041+
## Options
1042+
1043+
- `:causal_consistency` - whether the causal consistency should be persisted within
1044+
session. Default to `true`.
1045+
- `:read_concern` - what should be the level for read consistency in session. Should
1046+
be map with value `:level` that is one of the described in [*Read Concern*][rc]
1047+
documentation. Applied only when `:casual_consistency` is set to `true`. Bu default
1048+
uses cluster configuration.
1049+
- `:retry_writes` - whether retryable faliures should be retried. Defaults to `true`.
1050+
1051+
`:causal_consistency` can be set only during the session creation, but the `:read_concern`
1052+
can be set for each transaction independently.
1053+
1054+
[rc]: https://docs.mongodb.com/manual/reference/read-concern/index.html
10401055
"""
10411056
@spec start_session(GenServer.server(), keyword()) ::
10421057
{:ok, Mongo.Session.session()} | {:error, term()}

lib/mongo/session.ex

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule Mongo.Session do
88
:read_preference,
99
operation_time: nil,
1010
causal_consistency: true,
11-
retry_writes: false,
11+
retry_writes: true,
1212
active_txn: nil
1313
]
1414

@@ -187,7 +187,7 @@ defmodule Mongo.Session do
187187
read_concern: read_concern
188188
}
189189

190-
session = Map.update!(session, :txn, & &1 + 1)
190+
session = Map.update!(session, :txn, &(&1 + 1))
191191

192192
{:next_state, :transaction_started, struct(data, session: session, active_txn: txn),
193193
{:reply, from, :ok}}
@@ -344,19 +344,19 @@ defmodule Mongo.Session do
344344
:ok
345345

346346
{:error, error} = val ->
347-
if Mongo.Error.retryable(error) do
348-
new_data =
349-
Map.update!(data, :write_concern, fn
350-
nil ->
351-
%{w: :majority, wtimeout: 10_000}
352-
353-
map when is_map(map) ->
354-
map
355-
|> Map.put(:w, :majority)
356-
|> Map.put_new(:wtimeout, 10_000)
357-
end)
358-
359-
try_run_txn_command(new_data, command)
347+
if Mongo.Error.retryable(error) && data.retry_writes do
348+
data
349+
|> struct(retry_writes: false)
350+
|> Map.update!(:write_concern, fn
351+
nil ->
352+
%{w: :majority, wtimeout: 10_000}
353+
354+
map when is_map(map) ->
355+
map
356+
|> Map.put(:w, :majority)
357+
|> Map.put_new(:wtimeout, 10_000)
358+
end)
359+
|> try_run_txn_command(command)
360360
else
361361
val
362362
end

0 commit comments

Comments
 (0)