开发者

"Atomic section" in Ruby MongoDB driver?

开发者 https://www.devze.com 2023-01-26 01:54 出处:网络
Is there a way to perform a set of non-atomic actions on MongoDB server (using the Ruby driver) as an single atomic action? What I need is basically a lo开发者_如何学Pythonck on certain object/collect

Is there a way to perform a set of non-atomic actions on MongoDB server (using the Ruby driver) as an single atomic action? What I need is basically a lo开发者_如何学Pythonck on certain object/collection.


Because you can perform atomic operations on single documents, there are ways to simulate what you want. See this article:

http://kylebanker.com/blog/2010/06/07/mongodb-inventory-transactions/

And for some of the principles behind the ideas there, see this one:

http://www.eaipatterns.com/docs/IEEE_Software_Design_2PC.pdf


There's no way to do it in the Ruby driver because there's no way to do it in MongoDB. Mongo only supports single-document atomic operations. So basically an insert, update or delete of a single document is done atomically, but not operations across multiple documents.

You might be able to fake a transaction by attempting a manual "roll-back" if an error occurs. A roll-back in this case would be to replace any changes with the previous values. But that's going to manual and not have the ACID guarantees that you would get from most SQL servers.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号