【问题标题】:Transaction block prevents the commit to happen in database how can we skip certain thing?事务块阻止提交在数据库中发生我们如何跳过某些事情?
【发布时间】:2019-11-05 15:17:29
【问题描述】:

我正在使用事务块并在块内进行一些操作,有些事情应该忽略事务块,我无法从事务块中移动更新,因为它与代码紧密耦合。

我们怎样才能跳过某些部分,让其余的行为像往常一样

Record.transaction do
  check_for_errors required_columns
  create_report
end

def check_for_errors
 loop
  ...
 begin
  Methods
  // want to skip this perticular db update from transaction block
  job.update_column(total_number: loop index)
  // as this is under transaction block no changes can be seen on ui
 rescue => e
    populate_error_message(e.message)
    raise ActiveRecord::Rollback
  end
 end
end

知道在这种情况下可以做什么吗?

【问题讨论】:

  • 你使用什么数据库?

标签: ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

要跳过现有事务中的特定数据库操作,您可以在单独的线程中执行它。例如,对于您上面的情况

  ...
  Thread.new do
    ActiveRecord::Base.connection_pool.with_connection do
      job.update_column(total_number: loop_index)
    end
  end.join
  ... # continue method

【讨论】:

  • 对我没有帮助,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-25
  • 1970-01-01
  • 2011-06-21
相关资源
最近更新 更多