Module: Mongoid::Clients::Sessions::ClassMethods
- Defined in:
- lib/mongoid/clients/sessions.rb
Overview
Instance Method Summary collapse
-
#with_session(options = {}) {|The| ... } ⇒ Object
Execute a block within the context of a session.
Instance Method Details
#with_session(options = {}) {|The| ... } ⇒ Object
Note:
You cannot do any operations in the block using models or objects that use a different client; the block will execute all operations in the context of the implicit session and operations on any models using another client will fail. For example, if you set a client using store_in on a particular model and execute an operation on it in the session context block, that operation can’t use the block’s session and an error will be raised. You also cannot nest sessions.
Execute a block within the context of a session.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/mongoid/clients/sessions.rb', line 102 def with_session( = {}) if Threaded.get_session raise Mongoid::Errors::InvalidSessionUse.new(:invalid_session_nesting) end session = persistence_context.client.start_session() Threaded.set_session(session) yield(session) rescue Mongo::Error::InvalidSession => ex if # Driver 2.13.0+ defined?(Mongo::Error::SessionsNotSupported) && Mongo::Error::SessionsNotSupported === ex || # Legacy drivers ex. == Mongo::Session::SESSIONS_NOT_SUPPORTED then raise Mongoid::Errors::InvalidSessionUse.new(:sessions_not_supported) end raise Mongoid::Errors::InvalidSessionUse.new(:invalid_session_use) ensure Threaded.clear_session end |