Skip to content

Commit 79ebd6b

Browse files
MONGOID-4766 Document new transactions API (#5532)
1 parent 95e82f1 commit 79ebd6b

File tree

2 files changed

+152
-2
lines changed

2 files changed

+152
-2
lines changed

docs/reference/transactions.txt

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,122 @@ Transactions
1515
Version 4.0 of the MongoDB server introduces
1616
`multi-document transactions <https://mongodb.com/docs/manual/core/transactions/>`_.
1717
(Updates to multiple fields within a single document are atomic in all
18-
versions of MongoDB). Transactions require Mongoid version 6.4 or higher and Ruby driver version
19-
2.6 or higher.
18+
versions of MongoDB). Transactions require a non-standalone MongoDB topology
19+
and Ruby driver version 2.6 or higher. A higher level transaction API requires
20+
Mongoid version 9.0 or higher, while a lower level API requires Mongoid
21+
version 6.4 or higher.
2022

2123
Using Transactions
2224
==================
2325

26+
Higher Level API
27+
----------------
28+
29+
A transaction can be started by calling the ``transaction`` method on an instance
30+
of a Mongoid document class, on a Mongoid document class, on or ``Mongoid`` module:
31+
32+
.. code-block:: ruby
33+
34+
Band.transaction do
35+
Band.create(title: 'Led Zeppelin')
36+
end
37+
38+
band = Band.create(title: 'Deep Purple')
39+
band.transaction do
40+
band.active = false
41+
band.save!
42+
end
43+
44+
Mongoid.transaction do
45+
band.destroy
46+
end
47+
48+
When the ``transaction`` method is called, Mongoid does the following:
49+
50+
* creates a session on a client that is used by the receiver of the
51+
``transaction`` method call;
52+
* starts a transaction on the session;
53+
* executes the given block;
54+
* commits the transaction if no exception raised in the block;
55+
56+
* calls ``after_commit`` callbacks for all objects modified inside the transaction
57+
* aborts the transaction if an exception is raised in the block;
58+
59+
* calls ``after_rollback`` callbacks for all objects modified inside the transaction
60+
* closes the session
61+
62+
.. note::
63+
64+
Since a transaction is tied to a particular client, _only_ operations on
65+
the same client will be in scope of the transaction. Therefore it
66+
is recommended that only objects that use the same client are used inside the
67+
``transaction`` method block.
68+
69+
.. code-block:: ruby
70+
71+
class Author
72+
include Mongoid::Document
73+
store_in client: :encrypted_client
74+
end
75+
76+
class User
77+
include Mongoid::Document
78+
store_in client: :encrypted_client
79+
end
80+
81+
class Article
82+
include Mongoid::Document
83+
# This class uses the :default client
84+
end
85+
86+
# Transaction is started on the :encrypted_client
87+
Author.transaction do
88+
# This operation uses the same client, so it is in the transaction
89+
Author.create!
90+
# This operation also uses the same client, so it is in the transaction
91+
User.create!
92+
# This operation uses a different client, so it is NOT in the transaction
93+
Article.create!
94+
end
95+
96+
.. note::
97+
When ``transaction`` method is called on ``Mongoid`` module, the transaction
98+
is created using the ``:default`` client.
99+
100+
Aborting Transaction
101+
~~~~~~~~~~~~~~~~~~~~
102+
103+
Any exception raised inside the ``transaction`` method block aborts the
104+
transaction. Normally the raised exception passed on, except for the
105+
``Mongoid::Errors::Rollback``. This error should be raised if you want to
106+
explicitly abort the transaction without passing on an exception.
107+
108+
Callbacks
109+
~~~~~~~~~
110+
111+
Transaction API introduces two new callbacks - ``after_commit`` and ``after_rollback``.
112+
113+
``after_commit`` callback is triggered for an object that was created, saved,
114+
or destroyed:
115+
116+
* after transaction is committed if the object was modified inside the transaction;
117+
* after the object was persisted if the object was modified outside a transaction.
118+
119+
.. note::
120+
In any case ``after_commit`` callback is triggered only after all other callbacks
121+
were executed successfully. Therefore, if the object is modified without a
122+
transaction, it is possible that the object was persisted, but ``after_commit``
123+
callback was not triggered (for example, an exception raised in ``after_save``
124+
callback).
125+
126+
``after_rollback`` callback is triggered for an object that was created, saved,
127+
or destroyed inside a transaction if the transaction was aborted. ``after_rollback``
128+
is never triggered without a transaction.
129+
130+
131+
Lower Level API
132+
---------------
133+
24134
In order to start a transaction, the application must have a :ref:`session <sessions>`.
25135

26136
A transaction can be started by calling the ``start_transaction`` method on a session, which can be

docs/release-notes/mongoid-9.0.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,46 @@ The complete list of releases is available `on GitHub
1717
please consult GitHub releases for detailed release notes and JIRA for
1818
the complete list of issues fixed in each release, including bug fixes.
1919

20+
Sandbox Mode for Rails Console
21+
------------------------------
22+
23+
Mongoid now supports Rails console sandbox mode. If the Rails console started
24+
with ``--sandbox`` flag, Mongoid starts a transaction on the ``:default`` client
25+
before opening the console. This transaction won't be committed; therefore, all
26+
the commands executed in the console using the ``:default`` client won't
27+
be persisted in the database.
28+
29+
.. note::
30+
If you execute commands in the sandbox mode *using any other client than default*,
31+
these changes will be persisted as usual.
32+
33+
New Transactions API
34+
--------------------
35+
36+
Mongoid 9.0 introduces new transactions API that is inspired by ActiveRecord:
37+
38+
.. code-block:: ruby
39+
40+
Band.transaction do
41+
Band.create(title: 'Led Zeppelin')
42+
end
43+
44+
band = Band.create(title: 'Deep Purple')
45+
band.transaction do
46+
band.active = false
47+
band.save!
48+
end
49+
50+
Please consult :ref:`transactions documentation <transactions>` for more details.
51+
52+
Embedded Documents Always Use Parent Persistence Context
53+
--------------------------------------------------------
54+
55+
Mongoid 8.x and older allows user to specify persistence context for an
56+
embedded document (using ``store_in`` macro). In Mongoid 9.0 these settings are
57+
ignored for embedded documents; an embedded document now always uses the persistence
58+
context of its parent.
59+
2060

2161
Raise AttributeNotLoaded error when accessing fields omitted from query projection
2262
----------------------------------------------------------------------------------

0 commit comments

Comments
 (0)