Skip to content
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

MONGOID-5754 Making sure Mongoid works right with latest driver #5806

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gemfiles/driver_master.gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# rubocop:todo all
source "https://rubygems.org"

gem 'bson', git: "https://github.com/mongodb/bson-ruby", branch: '4-stable'
gem 'bson', git: "https://github.com/mongodb/bson-ruby"
gem 'mongo', git: "https://github.com/mongodb/mongo-ruby-driver"

gem 'actionpack'
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/driver_stable.gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# rubocop:todo all
source "https://rubygems.org"

gem 'mongo', git: "https://github.com/mongodb/mongo-ruby-driver", branch: '2.18-stable'
gem 'mongo', git: "https://github.com/mongodb/mongo-ruby-driver", branch: '2.19-stable'

gem 'actionpack'

Expand Down
20 changes: 20 additions & 0 deletions lib/mongoid/clients/sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def with_session(options = {})
else
raise ex
end
rescue *transactions_not_supported_exceptions
raise Mongoid::Errors::TransactionsNotSupported
ensure
Threaded.clear_session(client: persistence_context.client)
end
Expand Down Expand Up @@ -90,6 +92,8 @@ def transaction(options = {}, session_options: {})
session.start_transaction(options)
yield
commit_transaction(session)
rescue *transactions_not_supported_exceptions
raise Mongoid::Errors::TransactionsNotSupported
rescue Mongoid::Errors::Rollback
abort_transaction(session)
rescue Mongoid::Errors::InvalidSessionNesting
Expand Down Expand Up @@ -150,6 +154,22 @@ def after_rollback(*args, &block)

private

# Driver version 2.20 introduced a new exception for reporting that
# transactions are not supported. Prior to that, the condition was
# discovered by the rescue clause falling through to a different
# exception.
#
# This method ensures that Mongoid continues to work with older driver
# versions, by only returning the new exception.
#
# Once support is removed for all versions prior to 2.20.0, we can
# replace this method.
def transactions_not_supported_exceptions
return nil unless defined? Mongo::Error::TransactionsNotSupported

Mongo::Error::TransactionsNotSupported
end

# @return [ Mongo::Session ] Session for the current client.
def _session
Threaded.get_session(client: persistence_context.client)
Expand Down
2 changes: 1 addition & 1 deletion spec/shared
Loading