You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This module implements the details of the transactions api ([see specs](https://github.com/mongodb/specifications/blob/master/source/transactions/transactions.rst#committransaction)).
5
-
It uses the `:gen_statem` behaviour ([A nice tutorial](https://andrealeopardi.com/posts/connection-managers-with-gen_statem/)) to manage the different states.
5
+
It uses the `:gen_statem` behaviour ([a nice introduction](https://andrealeopardi.com/posts/connection-managers-with-gen_statem/)) to manage the different states.
6
6
7
-
In case of MongoDB 3.6 or greater the driver uses sessions for each operation. If no session is created the driver will create a so-called implict session. A session is a UUID-Number which
8
-
is added to some operations. The sessions are used to manage the transaction state as well. In most situation you need not to create a session instance, so the interface of the driver is not changed.
7
+
In case of MongoDB 3.6 or greater the driver uses sessions for each operation. If no session is created the driver will create a so-called implicit session. A session is a UUID-Number which
8
+
is added to some operations. The sessions are used to manage the transaction state as well. In most situation you need not to create a session instance, so the api of the driver is not changed.
9
9
10
10
In case of multiple insert statemantes you can use transaction (MongoDB 4.x) to be sure that all operations are grouped like a single operation. Prerequisites for transactions are:
11
11
MongoDB 4.x must be used as replica set or cluster deployment. The collection used in the operations must already exist. Some operation are not allowed (For example: create index or call count).
@@ -24,7 +24,7 @@ defmodule Mongo.Session do
24
24
:ok = Session.commit_transaction(session)
25
25
:ok = Session.end_session(top, session)
26
26
27
-
First you start a explicit session and a transactions. Use need to use the session for each insert statement as an options with key `:session` otherwise the insert statement won't be
27
+
First you start a explicit session and a transactions. Use the session for each insert statement as an options with key `:session` otherwise the insert statement won't be
28
28
executed in the transaction. After that you commit the transaction and end the session by calling `end_session`.
29
29
30
30
## Convenient API for Transactions
@@ -42,7 +42,59 @@ defmodule Mongo.Session do
42
42
{:ok, [id1, id2, id3]}
43
43
end, w: 1)
44
44
45
-
If the callback is successfull then it returns a tupel with the keyword `:ok` and a used defined result like `{:ok, [id1, id2, id3]}`
45
+
If the callback is successfull then it returns a tupel with the keyword `:ok` and a used defined result like `{:ok, [id1, id2, id3]}`. In this example we use
46
+
the write concern `w: 1`. The write concern used in the insert operation will be removed by the driver. It is applied in the commit transaction command.
47
+
48
+
## Implicit vs explicit sessions
49
+
50
+
In most cases the driver will create implicit sessions for you. Each time when you run a query or a command the driver
51
+
executes the following functions:
52
+
53
+
with {:ok, session} <- Session.start_implicit_session(topology_pid, type, opts),
54
+
result <- exec_command_session(session, new_cmd, opts),
55
+
:ok <- Session.end_implict_session(topology_pid, session) do
56
+
...
57
+
58
+
This behaviour is specified by the mongodb specification for [drivers](https://github.com/mongodb/specifications/blob/master/source/sessions/driver-sessions.rst#explicit-vs-implicit-sessions).
59
+
60
+
If you use the `:causal_consistency` flag, then you need to create an explicit session:
For more information about causal consistency see the [officially documentation](https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#causal-consistency).
72
+
73
+
If you want to use transaction, then you need to create a session as well:
0 commit comments