123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Clients::Sessions

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongoid/clients/sessions.rb

Overview

Encapsulates behavior for using sessions and transactions.

Class Method Summary

Instance Method Summary

  • #ensure_client_compatibility! private

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

Class Method Details

.included(base)

TODO:

Replace with ActiveSupport::Concern

Add class method mixin functionality.

[ GitHub ]

  
# File 'lib/mongoid/clients/sessions.rb', line 13

def self.included(base)
  base.include(ClassMethods)
end

Instance Method Details

#ensure_client_compatibility! (private)

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.

[ GitHub ]

  
# 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