【问题标题】:Perform Atomic Block Transactions in Rails with Mongoid使用 Mongoid 在 Rails 中执行原子块事务
【发布时间】:2014-10-03 16:24:26
【问题描述】:

我在我的应用中使用 Rails 4.1.4Mongoid

我只是想知道是否有一种方法可以在 Rails 中使用 Mongoid 原子地执行块事务。

为了清楚起见,假设我有这个:

some_model_instance = SomeModel.find(some_id)
some_model_instance.some_attribute = "Some Attribute Value"
some_model_instance.save
other_model_instance = OtherModel.find(other_id)
other_model_instance.other_attribute = "Other Attribute Value"
other_model_instance.save

我想将该代码包装在单个原子事务中,我的意思是,如果出现问题,我不希望两个实例(some_model_instance、other_model_instance)都保存到数据库中。

我已经看到 ActiveRecord 是这样的:

SomeModel.transaction do
    # do stuff I did above
end

这也适用于 Mongoid?。

感谢您的帮助!!!。

【问题讨论】:

  • MongoDB 没有事务,所以你必须像穴居人一样自己做。

标签: ruby-on-rails transactions mongoid


【解决方案1】:

已结束的问题

MongoDB 不支持事务。它只对单个文档执行原子操作。所以没有办法用 Mongoid 进行 “Atomic Block Transaction”

最好的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    相关资源
    最近更新 更多