Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Close doctrineConnection on disconnect #41584

merged 2 commits into from Mar 21, 2022


Copy link

@lcharette lcharette commented Mar 20, 2022

Consider this test:

class ExampleTest extends TestCase
    use DatabaseMigrations; 

    protected function tearDown(): void
        // parent::tearDown(); <-- Omitted on purposed (or not)


    public function test_1()

    public function test_2()

    // Repeat test 102x times...

    public function test_105()

With a migration that modify a column:

public function up()
    Schema::create('users', function (Blueprint $table) {
        $table->string('user_name', 50);

    Schema::table('users', function (Blueprint $table) {
        $table->string('user_name', 120)->change();

On PostgreSQL, after the 100th iteration, this error will pop up:

Illuminate\Database\QueryException: SQLSTATE[08006] [7] connection to server at "", port 5432 failed: FATAL:  sorry, too many clients already (SQL: create table "migrations" ("id" serial primary key not null, "migration" varchar(255) not null, "batch" integer not null))

While disconnect sets the “main” PDO object as null, the Doctrine connection created by the “change” action is not. The actual connection to the database is not actually closed in this specific case as the underlying PDO object is not fully destroyed as per PHP Documentation:

This bug won't affect any migration that are not modifying a column, as the Doctrine Connection is only created when needed, eg. using ->change().

I understand this fix won’t have much impact on a normal Laravel install, as :

  1. Laravel TestCase undefined the whole app on tearDown(), Connection included, closing the Doctrine connection.
  2. This test is pointless and should call parent::tearDown();

But in a situation where disconnect() is called outside of a test scenario, or anyone not using the whole Laravel TestCase might encounter this issue.

References :

@taylorotwell taylorotwell merged commit 4a9027a into laravel:8.x Mar 21, 2022
@lcharette lcharette deleted the lcharette-patch-1 branch March 21, 2022 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

2 participants