File tree 3 files changed +42
-12
lines changed
3 files changed +42
-12
lines changed Original file line number Diff line number Diff line change @@ -1318,7 +1318,11 @@ def do_close
1318
1318
# @api private
1319
1319
def get_session! ( options = { } )
1320
1320
if options [ :session ]
1321
- return options [ :session ] . validate! ( self )
1321
+ begin
1322
+ return options [ :session ] . validate! ( self )
1323
+ rescue Error ::SessionClusterMismatched
1324
+ nil # fall through to creating a new session
1325
+ end
1322
1326
end
1323
1327
1324
1328
cluster . validate_session_support! ( timeout : timeout_sec )
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ # Copyright (C) 2017-2025 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
+ # Error indicating that the session was retrieved from a client with a
22
+ # different cluster than that of the client through which it is currently being used.
23
+ class SessionClusterMismatched < InvalidSession
24
+
25
+ # Create the new exception.
26
+ #
27
+ # @example Create the new exception.
28
+ # SessionClusterMismatched.new
29
+ def initialize
30
+ super ( 'The configured cluster of the client used to create this session does not match that ' +
31
+ 'of the client owning this operation. Please only use this session for operations ' +
32
+ 'through its parent client.' )
33
+ end
34
+ end
35
+ end
36
+ end
Original file line number Diff line number Diff line change @@ -289,14 +289,6 @@ def session_id
289
289
# @api private
290
290
attr_accessor :recovery_token
291
291
292
- # Error message indicating that the session was retrieved from a client with a different cluster than that of the
293
- # client through which it is currently being used.
294
- #
295
- # @since 2.5.0
296
- MISMATCHED_CLUSTER_ERROR_MSG = 'The configuration of the client used to create this session does not match that ' +
297
- 'of the client owning this operation. Please only use this session for operations through its parent ' +
298
- 'client.' . freeze
299
-
300
292
# Error message describing that the session cannot be used because it has already been ended.
301
293
#
302
294
# @since 2.5.0
@@ -1255,9 +1247,7 @@ def check_if_ended!
1255
1247
end
1256
1248
1257
1249
def check_matching_cluster! ( client )
1258
- if @client . cluster != client . cluster
1259
- raise Mongo ::Error ::InvalidSession . new ( MISMATCHED_CLUSTER_ERROR_MSG )
1260
- end
1250
+ raise Mongo ::Error ::SessionClusterMismatched . new if @client . cluster != client . cluster
1261
1251
end
1262
1252
1263
1253
def check_transactions_supported!
You can’t perform that action at this time.
0 commit comments