{"record":{"id":"6e8b5b70b02b4ce6","repo":"mongodb/mongoid","slug":"a-session-was-started-while-another-session-was-be","errorCode":null,"errorMessage":"A session was started while another session was being used on this client.","messagePattern":"A session was started while another session was being used on this client\\.","errorType":"exception","errorClass":"Mongoid::Errors::InvalidSessionNesting","httpStatus":null,"severity":"error","filePath":"lib/mongoid/clients/sessions.rb","lineNumber":39,"sourceCode":"        # @example Execute some operations in the context of a session.\n        #   Band.with_session(causal_consistency: true) do\n        #     band = Band.create\n        #     band.records << Record.new\n        #     band.save\n        #     band.reload.records\n        #   end\n        #\n        # @param [ Hash ] options The session options. Please see the driver\n        #   documentation for the available session options.\n        #\n        # @raise [ Errors::InvalidSessionUse ] If an operation is attempted on a model using another\n        #   client from which the session was started or if sessions are nested.\n        #\n        # @return [ Object ] The result of calling the block.\n        #\n        # @yieldparam [ Mongo::Session ] The session being used for the block.\n        def with_session(options = {})\n          raise Mongoid::Errors::InvalidSessionNesting.new if Threaded.get_session(client: persistence_context.client)\n\n          session = persistence_context.client.start_session(options)\n          Threaded.set_session(session, client: persistence_context.client)\n          yield(session)\n        rescue Mongo::Error::InvalidSession => e\n          raise Mongoid::Errors::SessionsNotSupported.new if e.is_a?(Mongo::Error::SessionsNotSupported)\n\n          raise e\n        rescue Mongo::Error::OperationFailure => e\n          if (e.code == 40_415 && e.server_message =~ /startTransaction/) ||\n             (e.code == 20 && e.server_message =~ /Transaction/)\n            raise Mongoid::Errors::TransactionsNotSupported.new\n          else\n            raise e\n          end\n        rescue *transactions_not_supported_exceptions\n          raise Mongoid::Errors::TransactionsNotSupported\n        ensure","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/clients/sessions.rb#L21-L57","documentation":"Error \"A session was started while another session was being used on this client.\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/clients/sessions.rb:39 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0da0e23d71adfbad31128c18e5e2c8c301c6f7c4","analyzedAt":"2026-08-23T06:25:47.656Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}