Skip to content

Commit

Permalink
work around the new exception class not being present in older driver…
Browse files Browse the repository at this point in the history
… versions
  • Loading branch information
jamis committed Mar 29, 2024
1 parent f6e3283 commit 7d1310d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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
18 changes: 17 additions & 1 deletion lib/mongoid/clients/sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def transaction(options = {}, session_options: {})
session.start_transaction(options)
yield
commit_transaction(session)
rescue Mongo::Error::TransactionsNotSupported
rescue *transactions_not_supported_exceptions
raise Mongoid::Errors::TransactionsNotSupported
rescue Mongoid::Errors::Rollback
abort_transaction(session)
Expand Down Expand Up @@ -152,6 +152,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

0 comments on commit 7d1310d

Please sign in to comment.