module Mongoid::Clients::Sessions

Encapsulates behavior for using sessions and transactions.

Public Class Methods

included(base) click to toggle source

Add class method mixin functionality.

@todo Replace with ActiveSupport::Concern

# File lib/mongoid/clients/sessions.rb, line 13
def self.included(base)
  base.include(ClassMethods)
end

Private Instance Methods

ensure_client_compatibility!() click to toggle source

If at least one session is active, this ensures that the current model’s client is compatible with one of them.

“Compatible” is defined to mean: the same client was used to open one of the active sessions.

Currently emits a warning.

# File lib/mongoid/clients/sessions.rb, line 264
def ensure_client_compatibility!
  # short circuit: if no sessions are active, there's nothing
  # to check.
  return unless Threaded.sessions.any?

  # at this point, we know that at least one session is currently
  # active. let's see if one of them was started with the model's
  # client...
  session = Threaded.get_session(client: persistence_context.client)

  # if not, then we have a case of the programmer trying to use
  # a model within a transaction, where the model is not itself
  # controlled by that transaction. this is potentially a bug, so
  # let's tell them about it.
  if session.nil?
    # This is hacky; we're hijacking Mongoid::Errors::MongoidError in
    # order to get the spiffy error message translation. If we later
    # decide to raise an error instead of just writing a message, we can
    # subclass MongoidError and raise that exception here.
    message = Errors::MongoidError.new.compose_message(
      'client_session_mismatch',
      model: self.class.name
    )
    logger.info(message)
  end
end