Skip to content

Commit d6eb221

Browse files
committed
DR tech review 1
1 parent b43406d commit d6eb221

File tree

2 files changed

+46
-48
lines changed

2 files changed

+46
-48
lines changed

source/includes/interact-data/transaction.rb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,25 @@ class User
6363
# start-lower-lvl-api
6464
# Starts a session from the model class
6565
Book.with_session do |session|
66-
# Starts the transaction in the session
67-
session.start_transaction
66+
session.start_transaction
67+
# Creates a Book
68+
Book.create(title: 'Siddhartha', author: 'Hermann Hesse')
69+
70+
# Commits the transaction
71+
session.commit_transaction
72+
rescue StandardError
73+
# Ends the transaction if there is an error
74+
session.abort_transaction
6875
end
6976

7077
book = Book.new
71-
# Starts a session from an instance of Book
78+
# Starts a session from an *instance* of Book
7279
book.with_session do |session|
7380
# Starts the transaction in the session
7481
session.start_transaction
7582
end
7683
# end-lower-lvl-api
7784

78-
# start-commit-abort
79-
Book.with_session do |session|
80-
session.commit_transaction
81-
end
82-
83-
Book.with_session do |session|
84-
session.abort_transaction
85-
end
86-
# end-commit-abort
87-
8885
# start-commit-retry
8986
begin
9087
session.commit_transaction

source/interact-data/transaction.txt

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,21 @@ encounter unexpected errors.
4242
In {+odm+}, you can perform transactions by using either of the
4343
following APIs:
4444

45-
- :ref:`mongoid-txn-convenient`: {+odm+} manages the life cycle of the
45+
- :ref:`mongoid-txn-high-level`: {+odm+} manages the life cycle of the
4646
transaction. You can use this API in {+odm+} v9.0 and later.
4747

48-
- :ref:`mongoid-txn-core`: You must manage the life cycle of the
48+
- :ref:`mongoid-txn-low-level`: You must manage the life cycle of the
4949
transaction. You can use this API in {+odm+} v6.4 and later.
5050

5151
The :ref:`mongoid-txn-session` section describes how to make changes to
5252
your data from within a session without performing a transaction.
5353

54-
.. _mongoid-txn-convenient:
54+
.. _mongoid-txn-high-level:
5555

56-
Convenient Transaction API
56+
High-Level Transaction API
5757
--------------------------
5858

59-
You can use the Convenient Transaction API to internally manage the
59+
You can use the High-Level Transaction API to internally manage the
6060
lifecycle of your transaction. This API either commits your transaction
6161
or ends it and incorporates error handling logic.
6262

@@ -170,24 +170,50 @@ unsuccessful and changes were rolled back. {+odm+} never triggers
170170

171171
.. TODO link to callbacks guide.
172172

173-
.. _mongoid-txn-core:
173+
.. _mongoid-txn-low-level:
174174

175-
Core Transaction API
176-
--------------------
175+
Low-Level Transaction API
176+
-------------------------
177177

178-
When using the Core API, you must create a session before
178+
When using the low-level API, you must create a session before
179179
starting a transaction. You can create a session by calling the
180180
``with_session()`` method on a model class or an instance of a model.
181181

182182
Then, you can start a transaction by calling the ``start_transaction()``
183-
method on a session, as shown in the following code:
183+
method on a session. When using this API, you must manually commit or
184+
end the transaction. You can use the ``commit_transaction()`` and
185+
``abort_transaction()`` methods on the session instance to manage the
186+
transaction lifecycle.
187+
188+
This example demonstrates how to use the low-level transaction API
189+
to perform the following actions:
190+
191+
- Create a session
192+
- Start a transaction
193+
- Perform data operations
194+
- Commit the transaction, or end it if there are errors
184195

185196
.. literalinclude:: /includes/interact-data/transaction.rb
186197
:start-after: start-lower-lvl-api
187198
:end-before: end-lower-lvl-api
188199
:language: ruby
189200
:dedent:
190201

202+
.. note::
203+
204+
If a session ends and includes an open transaction, the transaction is
205+
automatically ended.
206+
207+
You can retry the transaction commit if it fails initially. The
208+
following example demonstrates how to retry the transaction when {+odm+}
209+
raises the ``UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL`` exception:
210+
211+
.. literalinclude:: /includes/interact-data/transaction.rb
212+
:start-after: start-commit-retry
213+
:end-before: end-commit-retry
214+
:language: ruby
215+
:dedent:
216+
191217
You can specify a read concern, write concern or read
192218
preference when starting a transaction by passing options to the
193219
``start_transaction()`` method:
@@ -205,31 +231,6 @@ To learn more about the available transaction options, see
205231
</Session.html#start_transaction-instance_method>` in the
206232
{+ruby-driver+} API documentation.
207233

208-
When using this API, you must manually commit or end the transaction.
209-
You can use the ``commit_transaction()`` and ``abort_transaction()``
210-
methods on the session instance to manage the transaction lifecycle:
211-
212-
.. literalinclude:: /includes/interact-data/transaction.rb
213-
:start-after: start-commit-abort
214-
:end-before: end-commit-abort
215-
:language: ruby
216-
:dedent:
217-
218-
.. note::
219-
220-
If a session ends and includes an open transaction, the transaction is
221-
automatically ended.
222-
223-
You can retry the transaction commit if it fails initially. The
224-
following example demonstrates how to retry the transaction when {+odm+}
225-
raises the ``UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL`` exception:
226-
227-
.. literalinclude:: /includes/interact-data/transaction.rb
228-
:start-after: start-commit-retry
229-
:end-before: end-commit-retry
230-
:language: ruby
231-
:dedent:
232-
233234
Client Behavior
234235
~~~~~~~~~~~~~~~
235236

0 commit comments

Comments
 (0)