-
Notifications
You must be signed in to change notification settings - Fork 29
DOCSP-42774: transactions #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
e54b4de
2d23fe4
b68d13e
6a11623
c0eda4b
f5392b7
d1e3bf3
b43406d
d6eb221
24e68c4
d747851
e0aae73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# start-example-models | ||
class Book | ||
include Mongoid::Document | ||
|
||
field :title, type: String | ||
field :author, type: String | ||
field :length, type: Integer | ||
end | ||
|
||
class Film | ||
include Mongoid::Document | ||
|
||
field :title, type: String | ||
field :year, type: Integer | ||
end | ||
# end-example-models | ||
|
||
# start-txn-operations | ||
# Starts a transaction from the model class | ||
Book.transaction do | ||
# Saves new Book and Film instances to MongoDB | ||
Book.create(title: 'Covert Joy', author: 'Clarice Lispector') | ||
Film.create(title: 'Nostalgia', year: 1983) | ||
end | ||
|
||
# Starts a transaction from an instance of Book | ||
book = Book.create(title: 'Sula', author: 'Toni Morrison') | ||
book.transaction do | ||
# Saves a new field value to the Book instance | ||
book.length = 192 | ||
book.save! | ||
end | ||
|
||
# Starts a transaction from the Mongoid instance | ||
Mongoid.transaction do | ||
# Deletes the Book instance in MongoDB | ||
book.destroy | ||
end | ||
# end-txn-operations | ||
|
||
# start-different-clients | ||
# Defines a class by using the :default client | ||
class Post | ||
include Mongoid::Document | ||
end | ||
|
||
# Defines a class by using the :encrypted_client | ||
class User | ||
include Mongoid::Document | ||
|
||
store_in client: :encrypted_client | ||
end | ||
|
||
# Starts a transaction on the :encrypted_client | ||
User.transaction do | ||
# Uses the same client, so the operation is in the transaction | ||
User.create! | ||
# Uses a different client, so it is *not* in the transaction | ||
Post.create! | ||
end | ||
# end-different-clients | ||
|
||
# start-lower-lvl-api | ||
# Starts a session from the model class | ||
Book.with_session do |session| | ||
# Starts the transaction in the session | ||
session.start_transaction | ||
end | ||
|
||
book = Book.new | ||
# Starts a session from an instance of Book | ||
book.with_session do |session| | ||
# Starts the transaction in the session | ||
session.start_transaction | ||
|
||
end | ||
# end-lower-lvl-api | ||
|
||
# start-commit-abort | ||
Book.with_session do |session| | ||
session.commit_transaction | ||
end | ||
|
||
Book.with_session do |session| | ||
session.abort_transaction | ||
end | ||
# end-commit-abort | ||
|
||
# start-commit-retry | ||
begin | ||
session.commit_transaction | ||
rescue Mongo::Error => e | ||
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL) | ||
retry | ||
else | ||
raise | ||
end | ||
end | ||
# end-commit-retry | ||
|
||
# start-other-client | ||
# Specifies that the operation should use the "other" client instead of | ||
# the default client | ||
User.with(client: :other) do | ||
Post.with(client: :other) do | ||
Post.with_session do |session| | ||
session.start_transaction | ||
Post.create! | ||
Post.create! | ||
User.create! | ||
session.commit_transaction | ||
end | ||
end | ||
end | ||
# end-other-client | ||
|
||
# start-model-session | ||
Book.with_session(causal_consistency: true) do | ||
Book.create! | ||
book = Person.first | ||
book.title = "Swann's Way" | ||
book.save | ||
end | ||
# end-model-session | ||
|
||
# start-instance-session | ||
book = Book.new | ||
book.with_session(causal_consistency: true) do | ||
book.title = 'Catch-22' | ||
book.save | ||
book.sellers << Shop.create! | ||
end | ||
# end-instance-session |
Uh oh!
There was an error while loading. Please reload this page.