framework: Queueable is not waiting for all database transactions to complete.

  • Laravel Version: 8.21.0
  • PHP Version: 8.0.0
  • Database Driver & Version: Amazon RDS

Description:

I have a peculiar issue with nested transactions and transaction aware event dispatching. Let’s say I have a function which looks something like this :

function a() {
  return DB::transaction(function() {
    return b();
  });
}

function b() {
  return DB::transaction(function() {
    event(new SomeEvent());
    return true;
  });
}

It appears SomeEvent will be dispatched after the first transaction is committed as I have my queue config 'after_commit' => true, Now I’m not really sure as to why this is happening, shouldn’t this be firing after the second transaction is committed too? Now I know this is happening because on my vapor ui, it shows me an exception of ModelNotFoundException and this only happens some of the time. Now I checked the database and the record is there.

Steps To Reproduce:

I hope this is pretty self-explanatory on how to reproduce this bug.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 30 (12 by maintainers)

Most upvoted comments

@r4y7s I need afterCommit to work on the sync driver since that is what I am using for testing. It seems like the sync driver does not care about afterCommit and dispatches the job instantly. I found a package that is like sync driver but honors afterCommit. But this package is not up-to-date for Laravel 9 and fails on queueable event listeners, it works for Jobs tho: https://github.com/jlorente/laravel-transaction-commit-queue