冒泡嵌套事务失败用ActiveRecord [英] Bubbling Up Nested-transaction Failures with ActiveRecord

查看:110
本文介绍了冒泡嵌套事务失败用ActiveRecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在该构成的操作应该被包裹在一个事务一个服务对象的方法。其中的一些操作也被包裹在交易。例如:

 类PostCreator
   DEF创建
      的ActiveRecord :: Base.transaction做
        post.do_this
        post.do_that
        user.do_more(帖子,other_stuff)
      结束
   结束
 结束

 高清帖子
   高清do_this
     交易做; ...;结束
   结束
 结束
 

我需要任何嵌套失败冒泡一路顶端,但我不知道如何做到这一点,并在<一个ActiveRecord的文档href="http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#label-Nested+transactions">nested交易似乎没有提供一个解决方案。从文档:

 #标准嵌套

User.transaction做
  User.create(用户名:'小鸟')
  User.transaction做
    User.create(用户名:音梦)
    提高的ActiveRecord ::回滚#这不会泡了:
                                    #_Both_用户仍会被创建。
  结束
结束

在嵌套事务TRUE`:#用`REQUIRES_NEW嵌套

User.transaction做
  User.create(用户名:'小鸟')
  User.transaction(REQUIRES_NEW:真)办
    User.create(用户名:音梦)
    提高的ActiveRecord ::回滚#这不会泡了两种
                                    #小鸟仍然会被创建。
  结束
结束
 

解决方案

下面是如何,你可以得到你的嵌套事务失败冒泡:

  User.transaction办
  User.create(用户名:'小鸟')
  提高的ActiveRecord ::回滚,除非User.transaction(REQUIRES_NEW:真)办
    User.create(用户名:音梦)
    提高的ActiveRecord ::回滚
  结束
结束
 

基本上,你必须产生一个错误在你的顶级事务为它回滚了。要做到这一点,你提出一个错误,如果嵌套事务返回falsey值(无)或truthy值。

希望帮助!

I have a method in a service object that composes operations that should be wrapped in a transaction. Some of these operations are also wrapped in transactions. For example:

class PostCreator
   def create
      ActiveRecord::Base.transaction do
        post.do_this
        post.do_that
        user.do_more(post, other_stuff)
      end
   end
 end

 def Post
   def do_this
     transaction do; ...; end
   end
 end

I need any nested failures to bubble up all the way to the top, but I'm not sure how to make that happen, and the ActiveRecord docs on nested transactions don't seem to offer a solution. From the docs:

# Standard nesting

User.transaction do
  User.create(username: 'Kotori')
  User.transaction do
    User.create(username: 'Nemu')
    raise ActiveRecord::Rollback    #  This won't bubble up:
                                    #  _Both_ users will still be created.
  end
end

# Nesting with `requires_new: true` on the nested transaction

User.transaction do
  User.create(username: 'Kotori')
  User.transaction(requires_new: true) do
    User.create(username: 'Nemu')
    raise ActiveRecord::Rollback    #  This won't bubble up either
                                    #  "Kotori" will still be created.
  end
end

解决方案

Here's how you could get failures in your nested transactions to bubble up:

User.transaction do
  User.create(username: 'Kotori')
  raise ActiveRecord::Rollback unless User.transaction(requires_new: true) do
    User.create(username: 'Nemu')
    raise ActiveRecord::Rollback
  end
end

Basically, you have to raise an error in your top-level transaction for it to rollback too. To do that, you raise an error if the nested transaction returns a falsey value(nil) or a truthy value.

Hope that helps!

这篇关于冒泡嵌套事务失败用ActiveRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆