diff --git a/lib/mongo/client.rb b/lib/mongo/client.rb index 70f5768628..1b04dd6f9f 100644 --- a/lib/mongo/client.rb +++ b/lib/mongo/client.rb @@ -1318,7 +1318,11 @@ def do_close # @api private def get_session!(options = {}) if options[:session] - return options[:session].validate!(self) + begin + return options[:session].validate!(self) + rescue Error::SessionClusterMismatched + nil # fall through to creating a new session + end end cluster.validate_session_support!(timeout: timeout_sec) diff --git a/lib/mongo/error/session_cluster_mismatched.rb b/lib/mongo/error/session_cluster_mismatched.rb new file mode 100644 index 0000000000..1f61d1d788 --- /dev/null +++ b/lib/mongo/error/session_cluster_mismatched.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true +# rubocop:todo all + +# Copyright (C) 2017-2025 MongoDB Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +module Mongo + class Error + + # Error indicating that the session was retrieved from a client with a + # different cluster than that of the client through which it is currently being used. + class SessionClusterMismatched < InvalidSession + + # Create the new exception. + # + # @example Create the new exception. + # SessionClusterMismatched.new + def initialize + super('The configured cluster of the client used to create this session does not match that ' + + 'of the client owning this operation. Please only use this session for operations ' + + 'through its parent client.') + end + end + end +end diff --git a/lib/mongo/session.rb b/lib/mongo/session.rb index be9c1f2a42..dd7a05af3e 100644 --- a/lib/mongo/session.rb +++ b/lib/mongo/session.rb @@ -289,14 +289,6 @@ def session_id # @api private attr_accessor :recovery_token - # Error message indicating that the session was retrieved from a client with a different cluster than that of the - # client through which it is currently being used. - # - # @since 2.5.0 - MISMATCHED_CLUSTER_ERROR_MSG = 'The configuration of the client used to create this session does not match that ' + - 'of the client owning this operation. Please only use this session for operations through its parent ' + - 'client.'.freeze - # Error message describing that the session cannot be used because it has already been ended. # # @since 2.5.0 @@ -1255,9 +1247,7 @@ def check_if_ended! end def check_matching_cluster!(client) - if @client.cluster != client.cluster - raise Mongo::Error::InvalidSession.new(MISMATCHED_CLUSTER_ERROR_MSG) - end + raise Mongo::Error::SessionClusterMismatched.new if @client.cluster != client.cluster end def check_transactions_supported!