dimanche 16 février 2020

Laravel 5.8 nested transactions not working as expected

I am using laravel 5.8 with mysql InnoDb engine. I have the following code:

DB::transaction(function () {
            $temp = new Permission();
            $temp->displayName = 'first1';
            $temp->uniqueName = 'first1';
            $temp->save();
            try {
                DB::transaction(function () {
                    $temp2 = new Permission();
                    $temp2->displayName = 'first2';
                    $temp2->uniqueName = 'first2';
                    $temp2->save();
                });
            } catch (\Exception $ex) {}
            throw new \Exception();
        });

I expect the permission first2 to be inserted in the database and the permission first1 will not be inserted since there is an exception in the outer transaction.

But what happens is: both permission first2 and permission first1 are not inserted. why?

by the way, I have tested the following:

https://github.com/laravel/framework/commit/9957329279fced0a24ac3d9d02cfe99407fa8f54#diff-2b6eae4ede78d520041c54df36afb408R453

and it works fine (since the exception in the inner transaction). can someone explain what happens?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/38xQc8B
via IFTTT

Aucun commentaire:

Enregistrer un commentaire