Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/mongoid/persistence_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ def ==(other)
options == other.options
end

# Whether the client of the context can be reused later, and therefore should
# not be closed.
#
# If the persistence context is requested with :client option only, it means
# that the context should use a client configured in mongoid.yml.
# Such clients should not be closed when the context is cleared since they
# will be reused later.
#
# @return [ true | false ] True if client can be reused, otherwise false.
#
# @api private
def reusable_client?
@options.keys == [:client]
end
private

def set_options!(opts)
Expand Down Expand Up @@ -238,7 +252,7 @@ def get(object)
def clear(object, cluster = nil, original_context = nil)
if context = get(object)
unless cluster.nil? || context.cluster.equal?(cluster)
context.client.close
context.client.close unless context.reusable_client?
end
end
ensure
Expand Down