class method self.after_all_transactions_commit

Ruby on Rails 8.0.4

Since v7.2.3

Available in: v7.2.3 v8.0.4 v8.1.2

Signature

self.after_all_transactions_commit(&block)

Registers a block to be called after all the current transactions have been committed.

If there is no currently open transaction, the block is called immediately.

If there are multiple nested transactions, the block is called after the outermost one has been committed,

If any of the currently open transactions is rolled back, the block is never called.

If multiple transactions are open across multiple databases, the block will be invoked if and once all of them have been committed. But note that nesting transactions across two distinct databases is a sharding anti-pattern that comes with a world of hurts.

Parameters

block block
Source
# File activerecord/lib/active_record.rb, line 528
  def self.after_all_transactions_commit(&block)
    open_transactions = all_open_transactions

    if open_transactions.empty?
      yield
    elsif open_transactions.size == 1
      open_transactions.first.after_commit(&block)
    else
      count = open_transactions.size
      callback = -> do
        count -= 1
        block.call if count.zero?
      end
      open_transactions.each do |t|
        t.after_commit(&callback)
      end
      open_transactions = nil # rubocop:disable Lint/UselessAssignment avoid holding it in the closure
    end
  end

Defined in activerecord/lib/active_record.rb line 528 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord

Type at least 2 characters to search.

↑↓ navigate · open · esc close