Skip to content

Commit e54b4de

Browse files
committed
DOCSP-42774: transactions
1 parent 27873d2 commit e54b4de

File tree

3 files changed

+416
-0
lines changed

3 files changed

+416
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# start-example-models
2+
class Book
3+
include Mongoid::Document
4+
5+
field :title, type: String
6+
field :author, type: String
7+
field :length, type: Integer
8+
end
9+
10+
class Film
11+
include Mongoid::Document
12+
13+
field :title, type: String
14+
field :year, type: Integer
15+
end
16+
# end-example-models
17+
18+
# start-txn-operations
19+
# Starts a transaction from the model class
20+
Book.transaction do
21+
Book.create(title: 'Covert Joy', author: 'Clarice Lispector')
22+
Film.create(title: 'Nostalgia', year: 1983)
23+
end
24+
25+
# Starts a transaction from an instance of Book
26+
book = Book.create(title: 'Sula', author: 'Toni Morrison')
27+
book.transaction do
28+
book.length = 192
29+
book.save!
30+
end
31+
32+
# Starts a transaction from the Mongoid instance
33+
Mongoid.transaction do
34+
book.destroy
35+
end
36+
# end-txn-operations
37+
38+
# start-different-clients
39+
# Defines a class by using the :default client
40+
class Post
41+
include Mongoid::Document
42+
end
43+
44+
# Defines a class by using the :encrypted_client
45+
class User
46+
include Mongoid::Document
47+
48+
store_in client: :encrypted_client
49+
end
50+
51+
# Starts a transaction on the :encrypted_client
52+
User.transaction do
53+
# Uses the same client, so the operation is in the transaction
54+
User.create!
55+
# Uses a different client, so it is *not* in the transaction
56+
Post.create!
57+
end
58+
# end-different-clients
59+
60+
# start-lower-lvl-api
61+
# Starts a session from the model class
62+
Book.with_session do |session|
63+
# Starts the transaction in the session
64+
session.start_transaction
65+
end
66+
67+
book = Book.new
68+
# Starts a session from an instance of Book
69+
book.with_session do |session|
70+
# Starts the transaction in the session
71+
session.start_transaction
72+
end
73+
# end-lower-lvl-api
74+
75+
# start-commit-abort
76+
Book.with_session do |session|
77+
session.commit_transaction
78+
end
79+
80+
Book.with_session do |session|
81+
session.abort_transaction
82+
end
83+
# end-commit-abort
84+
85+
# start-commit-retry
86+
begin
87+
session.commit_transaction
88+
rescue Mongo::Error => e
89+
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
90+
retry
91+
else
92+
raise
93+
end
94+
end
95+
# end-commit-retry
96+
97+
# start-other-client
98+
# Specifies that the operation should use the "other" client instead of
99+
# the default client
100+
User.with(client: :other) do
101+
Post.with(client: :other) do
102+
Post.with_session do |session|
103+
session.start_transaction
104+
Post.create!
105+
Post.create!
106+
User.create!
107+
session.commit_transaction
108+
end
109+
end
110+
end
111+
# end-other-client
112+
113+
# start-model-session
114+
Book.with_session(causal_consistency: true) do
115+
Book.create!
116+
book = Person.first
117+
book.title = "Swann's Way"
118+
book.save
119+
end
120+
# end-model-session
121+
122+
# start-instance-session
123+
book = Book.new
124+
book.with_session(causal_consistency: true) do
125+
book.title = 'Catch-22'
126+
book.save
127+
book.sellers << Shop.create!
128+
end
129+
# end-instance-session

source/interact-data.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Interact with Data
1616

1717
/interact-data/specify-query
1818
/interact-data/modify-results
19+
/interact-data/transaction
1920

2021
In this section, you can learn how to use {+odm+} to interact with your
2122
MongoDB data.
@@ -25,3 +26,6 @@ MongoDB data.
2526

2627
- :ref:`mongoid-data-modify-results`: Learn how to modify the way that
2728
{+odm+} returns results from queries.
29+
30+
- :ref:`mongoid-data-txn`: Learn how to perform multi-document
31+
transactions to make atomic data changes.

0 commit comments

Comments
 (0)