Search code examples
activerecordsidekiq

ActiveRecord Transaction -- When Does the DB Commit Actually Happen?


I'm having an issue with Sidekiq not finding a DB record, which I suppose is because the transaction is not fully committed before Sidekiq runs. I was under the impression that the transaction is committed inside the block:

User.transaction do
    # db updates here
    # commit or rollback
end

MyWorker.perform_async()

So is my understanding incorrect, that even after the block, the DB operations may have not been fully committed? That's the behavior I'm seeing. I've heard of after_commit but I'm using service objects and want to keep the logic there.


Solution

  • Your impression is correct, the transaction commits at the end of the block. If MyWorker depends on the data in the block and the block commits, it should be visible when the job executes since the job is created outside of the block.