第二个数据库(postgres)不在事务中回滚

时间:2017-05-13 11:07:30

标签: ruby-on-rails ruby heroku transactions

我有一个代码,我在ruby代码中测试回滚作为事务,如果它在那个块中失败了,这是我的代码;

def create_log
  begin
   ActiveRecord::Base.transaction do
     log = Log.new(params[:log])
     if log.save
      # line of code
      # line of code

      #Only this below function happening through **SECOND database postgres(under heroku)**
      log.add_logs_in_review(status)
     end
   end
   rescue Timeout::Error => e
     puts "#{e}"
   end
end

所以这里我们正在进行超时,并且所有进程都将在没有第二个数据库发生的进程的情况下回滚,那么我们如何从第二个数据库回滚进程呢?

1 个答案:

答案 0 :(得分:0)

如果您正在使用2个数据库,解决方案是拥有嵌套事务并处理回滚

如果您有数据库A和B

A.transaction do
    B.transaction do
        raise ActiveRecord::Rollback if (some condition)#this will rollback both A and B
    end
    raise ActiveRecord::Rollback if (some condition)#this will rollback only A as B has already ended
end

来源:  nested-transctions