File tree 5 files changed +66
-0
lines changed
5 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -217,6 +217,7 @@ def write_concern_error_labels
217
217
require 'mongo/error/server_api_conflict'
218
218
require 'mongo/error/server_api_not_supported'
219
219
require 'mongo/error/server_not_usable'
220
+ require 'mongo/error/transactions_not_supported'
220
221
require 'mongo/error/unknown_payload_type'
221
222
require 'mongo/error/unmet_dependency'
222
223
require 'mongo/error/unsupported_option'
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -48,6 +48,7 @@ class Features
48
48
# provided by the client during findAndModify operations, requiring the
49
49
# driver to raise client-side errors when those options are provided.
50
50
find_and_modify_option_validation : 8 ,
51
+ sharded_transactions : 8 ,
51
52
transactions : 7 ,
52
53
scram_sha_256 : 7 ,
53
54
array_filters : 6 ,
Original file line number Diff line number Diff line change @@ -538,6 +538,8 @@ def with_transaction(options=nil)
538
538
#
539
539
# @since 2.6.0
540
540
def start_transaction ( options = nil )
541
+ check_transactions_supported!
542
+
541
543
if options
542
544
Lint . validate_read_concern_option ( options [ :read_concern ] )
543
545
@@ -1185,5 +1187,18 @@ def check_matching_cluster!(client)
1185
1187
raise Mongo ::Error ::InvalidSession . new ( MISMATCHED_CLUSTER_ERROR_MSG )
1186
1188
end
1187
1189
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
1188
1203
end
1189
1204
end
Original file line number Diff line number Diff line change @@ -26,6 +26,17 @@ class SessionTransactionSpecError < StandardError; end
26
26
collection . delete_many
27
27
end
28
28
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
+
29
40
describe '#abort_transaction' do
30
41
require_topology :replica_set
31
42
@@ -75,6 +86,8 @@ class SessionTransactionSpecError < StandardError; end
75
86
end
76
87
77
88
describe '#with_transaction' do
89
+ require_topology :replica_set
90
+
78
91
context 'callback successful' do
79
92
it 'commits' do
80
93
session . with_transaction do
You can’t perform that action at this time.
0 commit comments