Skip to content

Commit 69ada9d

Browse files
committed
Skip cluster mismatched error; allow queries on other clusters to execute without error
1 parent 5960f5f commit 69ada9d

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

lib/mongo/client.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,11 @@ def do_close
13181318
# @api private
13191319
def get_session!(options = {})
13201320
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
13221326
end
13231327

13241328
cluster.validate_session_support!(timeout: timeout_sec)
Lines changed: 36 additions & 0 deletions
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) 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

lib/mongo/session.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,6 @@ def session_id
289289
# @api private
290290
attr_accessor :recovery_token
291291

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-
300292
# Error message describing that the session cannot be used because it has already been ended.
301293
#
302294
# @since 2.5.0
@@ -1255,9 +1247,7 @@ def check_if_ended!
12551247
end
12561248

12571249
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
12611251
end
12621252

12631253
def check_transactions_supported!

0 commit comments

Comments
 (0)