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
0 commit comments