Skip to content

Commit 95545a2

Browse files
authored
Merge pull request #63 from rustagir/DOCSP-42774-transaction
DOCSP-42774: transactions
2 parents 4d87c40 + e0aae73 commit 95545a2

File tree

4 files changed

+461
-0
lines changed

4 files changed

+461
-0
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ feedback-widget-title = "Feedback"
2828
server-manual = "Server manual"
2929
api-root = "https://www.mongodb.com/docs/mongoid/master/api/Mongoid"
3030
api = "https://www.mongodb.com/docs/mongoid/master/api"
31+
ruby-api = "https://www.mongodb.com/docs/ruby-driver/current/api/Mongo"
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
# Saves new Book and Film instances to MongoDB
22+
Book.create(title: 'Covert Joy', author: 'Clarice Lispector')
23+
Film.create(title: 'Nostalgia', year: 1983)
24+
end
25+
26+
# Starts a transaction from an instance of Book
27+
book = Book.create(title: 'Sula', author: 'Toni Morrison')
28+
book.transaction do
29+
# Saves a new field value to the Book instance
30+
book.length = 192
31+
book.save!
32+
end
33+
34+
# Starts a transaction from the Mongoid instance
35+
Mongoid.transaction do
36+
# Deletes the Book instance in MongoDB
37+
book.destroy
38+
end
39+
# end-txn-operations
40+
41+
# start-different-clients
42+
# Defines a class by using the :default client
43+
class Post
44+
include Mongoid::Document
45+
end
46+
47+
# Defines a class by using the :encrypted_client
48+
class User
49+
include Mongoid::Document
50+
51+
store_in client: :encrypted_client
52+
end
53+
54+
# Starts a transaction on the :encrypted_client
55+
User.transaction do
56+
# Uses the same client, so the operation is in the transaction
57+
User.create!
58+
# Uses a different client, so it is *not* in the transaction
59+
Post.create!
60+
end
61+
# end-different-clients
62+
63+
# start-lower-lvl-api
64+
# Starts a session from the model class
65+
Book.with_session do |session|
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
75+
end
76+
# end-lower-lvl-api
77+
78+
# start-commit-retry
79+
begin
80+
session.commit_transaction
81+
rescue Mongo::Error => e
82+
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
83+
retry
84+
else
85+
raise
86+
end
87+
end
88+
# end-commit-retry
89+
90+
# start-other-client
91+
# Specifies that the operation should use the "other" client instead of
92+
# the default client
93+
User.with(client: :other) do
94+
Post.with(client: :other) do
95+
Post.with_session do |session|
96+
session.start_transaction
97+
Post.create!
98+
Post.create!
99+
User.create!
100+
session.commit_transaction
101+
end
102+
end
103+
end
104+
# end-other-client
105+
106+
# start-model-session
107+
Book.with_session(causal_consistency: true) do
108+
Book.create!
109+
book = Person.first
110+
book.title = "Swann's Way"
111+
book.save
112+
end
113+
# end-model-session
114+
115+
# start-instance-session
116+
book = Book.new
117+
book.with_session(causal_consistency: true) do
118+
book.title = 'Catch-22'
119+
book.save
120+
book.sellers << Shop.create!
121+
end
122+
# 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
Specify a Query </interact-data/specify-query>
1818
Modify Query Results </interact-data/modify-results>
19+
Transactions and Sessions </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)