Skip to content

Exception thrown in DB::afterCommit cause "Syntax error or access violation: 1305 SAVEPOINT trans2 does not exist" error #36886

Closed
@midas104

Description

@midas104
  • Laravel Version: 8.35.1
  • PHP Version: 8.0.3
  • Database Driver & Version: Percona 5.7

Description:

DB::afterCommit callbacks are executed before transaction level get decremented. So in case if callback do some actions inside it's own transaction and throws exception we will get Syntax error or access violation: 1305 SAVEPOINT trans2 does not exist DB error (percona in my case)

Steps To Reproduce:

<?php

declare(strict_types=1);

use Illuminate\Contracts\Console\Kernel;

$app = require_once __DIR__ . '/vendor/autoload.php';
$app = require_once __DIR__ . '/bootstrap/app.php';

/** @var \App\Console\Kernel $kernel */
$kernel = $app->make(Kernel::class);
$kernel->bootstrap();

class SomeService
{
    public function mainBusinessLogic()
    {
        \DB::transaction(function () {
            // DO some business logic stuff here

            \DB::afterCommit(function () {
                $this->callSomeSecondaryBusinessLogic();
            });
        });
    }

    private function callSomeSecondaryBusinessLogic()
    {
        \DB::transaction(function () {
            // for instance this code should save some model
            // and dispatch job to db queue
            // so we need transaction here for sure
            throw  new \RuntimeException('tst');
        });
    }
}


(new SomeService())->mainBusinessLogic();

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions