Skip to content

Commit f87e526

Browse files
RUBY-1791 Raise if transactions not supported
1 parent e06e03a commit f87e526

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

Diff for: lib/mongo/error.rb

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def write_concern_error_labels
217217
require 'mongo/error/server_api_conflict'
218218
require 'mongo/error/server_api_not_supported'
219219
require 'mongo/error/server_not_usable'
220+
require 'mongo/error/transactions_not_supported'
220221
require 'mongo/error/unknown_payload_type'
221222
require 'mongo/error/unmet_dependency'
222223
require 'mongo/error/unsupported_option'

Diff for: lib/mongo/error/transactions_not_supported.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
# rubocop:todo all
3+
4+
# Copyright (C) 2019-2020 MongoDB Inc.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
module Mongo
19+
class Error
20+
21+
# Transactions are not supported by the cluster. There might be the
22+
# following reasons:
23+
# - topology is standalone
24+
# - topology is replica set and server version is < 4.0
25+
# - topology is sharded and server version is < 4.2
26+
#
27+
# @param [ String ] reason The reason why transactions are no supported.
28+
#
29+
# @since 2.7.0
30+
class TransactionsNotSupported < Error
31+
def initialize(reason)
32+
super("Transactions are not supported for the cluster: #{reason}")
33+
end
34+
end
35+
end
36+
end

Diff for: lib/mongo/server/description/features.rb

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Features
4848
# provided by the client during findAndModify operations, requiring the
4949
# driver to raise client-side errors when those options are provided.
5050
find_and_modify_option_validation: 8,
51+
sharded_transactions: 8,
5152
transactions: 7,
5253
scram_sha_256: 7,
5354
array_filters: 6,

Diff for: lib/mongo/session.rb

+15
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,8 @@ def with_transaction(options=nil)
538538
#
539539
# @since 2.6.0
540540
def start_transaction(options = nil)
541+
check_transactions_supported!
542+
541543
if options
542544
Lint.validate_read_concern_option(options[:read_concern])
543545

@@ -1185,5 +1187,18 @@ def check_matching_cluster!(client)
11851187
raise Mongo::Error::InvalidSession.new(MISMATCHED_CLUSTER_ERROR_MSG)
11861188
end
11871189
end
1190+
1191+
def check_transactions_supported!
1192+
raise Mongo::Error::TransactionsNotSupported, "standalone topology" if cluster.single?
1193+
1194+
cluster.next_primary.with_connection do |conn|
1195+
if cluster.replica_set? && !conn.fetures.transactions_enabled?
1196+
raise Mongo::Error::TransactionsNotSupported, "server version is < 4.0"
1197+
end
1198+
if cluster.sharded? && !conn.features.sharded_transactions_enabled?
1199+
raise Mongo::Error::TransactionsNotSupported, "sharded transactions require server version >= 4.2"
1200+
end
1201+
end
1202+
end
11881203
end
11891204
end

Diff for: spec/mongo/session_transaction_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ class SessionTransactionSpecError < StandardError; end
2626
collection.delete_many
2727
end
2828

29+
describe 'start_transaction' do
30+
context 'when topology is sharded and server is < 4.2' do
31+
max_server_fcv '4.2'
32+
require_topology :sharded
33+
34+
it 'raises an error' do
35+
expect { session.start_transaction }.to raise_error(Mongo::Error::TransactionsNotSupported, /sharded transactions require server version/)
36+
end
37+
end
38+
end
39+
2940
describe '#abort_transaction' do
3041
require_topology :replica_set
3142

@@ -75,6 +86,8 @@ class SessionTransactionSpecError < StandardError; end
7586
end
7687

7788
describe '#with_transaction' do
89+
require_topology :replica_set
90+
7891
context 'callback successful' do
7992
it 'commits' do
8093
session.with_transaction do

0 commit comments

Comments
 (0)