-
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
Merged
rustagir
merged 12 commits into
mongodb:standardized
from
rustagir:DOCSP-42774-transaction
Nov 20, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e54b4de
DOCSP-42774: transactions
rustagir 2d23fe4
vale
rustagir b68d13e
link text
rustagir 6a11623
MR PR fixes 1
rustagir c0eda4b
MR PR fixes 2
rustagir f5392b7
Merge branch 'standardized' into DOCSP-42774-transaction
rustagir d1e3bf3
try using roles
rustagir b43406d
Merge branch 'DOCSP-42774-transaction' of github.com:rustagir/docs-mo…
rustagir d6eb221
DR tech review 1
rustagir 24e68c4
page fmt
rustagir d747851
page fmt
rustagir e0aae73
DR small fix
rustagir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# 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| | ||
session.start_transaction | ||
# Creates a Book | ||
Book.create(title: 'Siddhartha', author: 'Hermann Hesse') | ||
|
||
# Commits the transaction | ||
session.commit_transaction | ||
rescue StandardError | ||
# Ends the transaction if there is an error | ||
session.abort_transaction | ||
end | ||
# end-lower-lvl-api | ||
|
||
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.