“原子截面”在 Ruby MongoDB 驱动程序中?
有没有办法在 MongoDB 服务器上(使用 Ruby 驱动程序)将一组非原子操作作为单个原子操作执行?我需要的基本上是对某些对象/集合的锁定。
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 lock on certain object/collection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您可以对单个文档执行原子操作,因此有多种方法可以模拟您想要的操作。请参阅这篇文章:
http://kylebanker.com/blog/2010/ 06/07/mongodb-inventory-transactions/
有关该想法背后的一些原则,请参阅以下内容:
http://www.eaipatterns.com/docs/IEEE_Software_Design_2PC.pdf
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
在 Ruby 驱动程序中无法做到这一点,因为在 MongoDB 中也无法做到这一点。 Mongo仅支持单文档原子操作。因此基本上单个文档的插入、更新或删除是原子完成的,但不是跨多个文档的操作。
如果发生错误,您可以通过尝试手动“回滚”来伪造交易。在这种情况下,回滚是将所有更改替换为之前的值。但这需要手动操作,并且没有大多数 SQL 服务器提供的 ACID 保证。
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.