Skip to content

SQLSTATE[25P01]: No active sql transaction: 7 ERROR: SAVEPOINT can only be used in transaction blocks error when using $afterCommit = true; #48658

Closed
@azgooon

Description

@azgooon

Laravel Version

10.25.2 & 10.25.1

PHP Version

8.2.10

Database Driver & Version

PostgreSQL

Description

The project I am working on uses nested transactions and they used to work flawlesly however I've updated laravel framework from version 10.25.0 to 10.25.2 and now I keep getting errors:
PDOException: SQLSTATE[25P01]: No active sql transaction: 7 ERROR: SAVEPOINT can only be used in transaction blocks in /var/www/vendor/laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php:159

I reverted back to v10.25.0 and the problem has gone away, then changed to v10.25.1 and problem re-appeared.

I then looked into the changes in vendor/laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php and it turned out that if I revert logic of this trait back to https://github.com/laravel/framework/blob/4b5ef9e96e15eff8b1a8f848419feba8dc0be50f/src/Illuminate/Database/Concerns/ManagesTransactions.php then issue vanished as well.

Please see below steps to reproduce and let me know if anything is unclear.

FYI (I hope you don't mid) : @mpyw, @tonysm @crynobone

Steps To Reproduce

Step 1 - below code is executed, however ActionQueue "created" event is observerd. ActionQueue observer has got $afterCommit set to true.

        DB::transaction(function () {
            $actionQueue = new ActionQueue([
                'action_id'     => Action::TEST->actionId(),
                'delay'         => 0,
                'payload'       => ['data' => 'test'],
            ]);
            $actionQueue->save();
        });

Step2 - in ActionQueue observer I dispatch below job using dispatchSync() method.

<?php

namespace App\Jobs;

use App\Models\ActionNote;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Spatie\InteractsWithPayload\Concerns\InteractsWithPayload;

class Test implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, InteractsWithPayload;

    public function handle(): void
    {
        DB::transaction(function () {
            $note = new ActionNote(['note' => mt_rand(1, 10)]);
            return $note->save();
        });
    }
}

This is when I get error:

SQLSTATE[25P01]: No active sql transaction: 7 ERROR: SAVEPOINT can only be used in transaction blocks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions