Skip to content

Commit

Permalink
MONGOID-5754 Making sure Mongoid works right with latest driver (#5806)
Browse files Browse the repository at this point in the history
* driver master relies on bson 5 (master)

* make sure and recognize Mongo::Error::TransactionsNotSupported

* bump spec/shared to latest (for jdk17)

* work around the new exception class not being present in older driver versions

* transaction check in with_sessions for server 3.6
  • Loading branch information
jamis authored Apr 2, 2024
1 parent 7d752dc commit 3963eba
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
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

0 comments on commit 3963eba

Please sign in to comment.