Skip to content

Laravel 10: DB::transactionLevel() shows incorrect level, if DDL operation was done #48777

Closed
@mrckzgl

Description

@mrckzgl

Laravel Version

10.28.0

PHP Version

8.2.11

Database Driver & Version

10.10.2-MariaDB-1:10.10.2+maria~ubu220

Description

We encounter that DB::transactionLevel() shows an incorrect level after a DDL operation was done, which does commit implicitly. Consider this code snippet which is the current behaviour:

Log::debug(DB::transactionLevel()); // 0
DB::beginTransaction();
Log::debug(DB::transactionLevel()); // 1
DB::commit();
Log::debug(DB::transactionLevel()); // 0

DB::beginTransaction();
Log::debug(DB::transactionLevel()); // 1
// some DDL operation which closes the transaction
Log::debug(DB::transactionLevel()); // 1

This is problematic on different levels. First, getting the correct transaction level is important to be able to close all pending transactions. Second, due to a current change (see: #35380), DB::commit() and DB::rollback() both throw an exception if no active transaction is present. That means after beginTransaction and a DDL statement, this code:

if (DB::transactionLevel() > 0) {
  DB::commit();
}

will fail.

Steps To Reproduce

DB::beginTransaction();
Schema::dropIfExists('test');
Schema::create('test', function (Blueprint $table) {
    $table->id();
});
if (DB::transactionLevel() > 0) {
  DB::commit();
}

I just confirmed this behaviour in a minimal example, happy to provide a repo if needed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions