Does runtime persistence options thread safed? #5770
-
I just read about this neat feature about Runtime Persistence Options. To recap, Mongoid allows MyModel.with({ client: :query }) do |collection|
collection.last
# something else
end to explicitly specify client usage for some operations. During the operation, if some other thread accesses Also, is there any other way I can utilize non-default clients? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @midnight-wonderer! The persistence options are thread safe; they ultimately call By "non-default" clients, do you mean clients created ad-hoc, instead of declared in the mongoid config? The Mongoid::Clients.set(:client_name, client_instance) In general, we encourage developers to use previously-declared clients for this, rather than ad-hoc clients. Use of ad-hoc clients can make it difficult to understand what the behavior of a particular query or update really is. |
Beta Was this translation helpful? Give feedback.
Hello @midnight-wonderer! The persistence options are thread safe; they ultimately call
PersistenceContext#get_context
(here), which fetches the information from thread-local storage.By "non-default" clients, do you mean clients created ad-hoc, instead of declared in the mongoid config? The
client
option requires a client name, but you can declare a new client and assign it a name programmatically, like so:In general, we encourage developers to use previously-declared clients for this, rather than ad-hoc clients. Use of ad-hoc clients can make it difficult to understand what the behavior of a particular query or update really is.