使用ActiveRecord冒泡嵌套事务失败

 Sunday老师 发布于 2023-02-06 15:31

我在服务对象中有一个方法,它组成应该包含在事务中的操作.其中一些操作也包含在事务中.例如:

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

我需要任何嵌套的失败一直到顶部,但我不确定如何实现这一点,嵌套事务上的ActiveRecord文档似乎没有提供解决方案.来自文档:

# 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

Gjaldon.. 31

以下是您在嵌套事务中出现故障的方法:

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

基本上,您必须在顶级事务中引发错误以使其回滚.为此,如果嵌套事务返回falsey值(nil)或truthy值,则会引发错误.

希望有所帮助!

2 个回答
  • 另一种更简单的方法是,可以引发CustomError <StandardError.与ActiveRecord :: Rollback不同,Error将通过父事务冒泡.您可以在任何需要的地方处理它.所有其他内部交易将被回滚.

    试试下面的模板.

    ActiveRecord::Base.transacton do
        ActiveRecord::Base.transacton do
            raise StandardError.new
        end
    end
    

    2023-02-06 15:33 回答
  • 以下是您在嵌套事务中出现故障的方法:

    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
    

    基本上,您必须在顶级事务中引发错误以使其回滚.为此,如果嵌套事务返回falsey值(nil)或truthy值,则会引发错误.

    希望有所帮助!

    2023-02-06 15:33 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有