Skip to content

Commit 3963eba

Browse files
authored
MONGOID-5754 Making sure Mongoid works right with latest driver (#5806)
* 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
1 parent 7d752dc commit 3963eba

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

gemfiles/driver_master.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# rubocop:todo all
22
source "https://rubygems.org"
33

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

77
gem 'actionpack'

gemfiles/driver_stable.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# rubocop:todo all
22
source "https://rubygems.org"
33

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

66
gem 'actionpack'
77

lib/mongoid/clients/sessions.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def with_session(options = {})
5959
else
6060
raise ex
6161
end
62+
rescue *transactions_not_supported_exceptions
63+
raise Mongoid::Errors::TransactionsNotSupported
6264
ensure
6365
Threaded.clear_session(client: persistence_context.client)
6466
end
@@ -90,6 +92,8 @@ def transaction(options = {}, session_options: {})
9092
session.start_transaction(options)
9193
yield
9294
commit_transaction(session)
95+
rescue *transactions_not_supported_exceptions
96+
raise Mongoid::Errors::TransactionsNotSupported
9397
rescue Mongoid::Errors::Rollback
9498
abort_transaction(session)
9599
rescue Mongoid::Errors::InvalidSessionNesting
@@ -150,6 +154,22 @@ def after_rollback(*args, &block)
150154

151155
private
152156

157+
# Driver version 2.20 introduced a new exception for reporting that
158+
# transactions are not supported. Prior to that, the condition was
159+
# discovered by the rescue clause falling through to a different
160+
# exception.
161+
#
162+
# This method ensures that Mongoid continues to work with older driver
163+
# versions, by only returning the new exception.
164+
#
165+
# Once support is removed for all versions prior to 2.20.0, we can
166+
# replace this method.
167+
def transactions_not_supported_exceptions
168+
return nil unless defined? Mongo::Error::TransactionsNotSupported
169+
170+
Mongo::Error::TransactionsNotSupported
171+
end
172+
153173
# @return [ Mongo::Session ] Session for the current client.
154174
def _session
155175
Threaded.get_session(client: persistence_context.client)

0 commit comments

Comments
 (0)