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

Postgre "too many clients already" on running tests #18056

Closed
mariomka opened this issue Feb 22, 2017 · 5 comments
Closed

Postgre "too many clients already" on running tests #18056

mariomka opened this issue Feb 22, 2017 · 5 comments

Comments

@mariomka
Copy link

  • Laravel Version: 5.4.12
  • PHP Version: PHP 7.1.0-2+deb.sury.org~xenial+1 (cli) ( NTS )
  • Database Driver & Version: psql (PostgreSQL) 9.5.5

(Running on Homestead)

Description:

On running more than 100 tests that use DatabaseMigrations the following error appears:

Illuminate\Database\QueryException: SQLSTATE[08006] [7] FATAL:  sorry, too many clients already

Steps To Reproduce:

Run more than 100 tests (methods) using DatabaseMigrations on Postgree (default max connections is 100).

I have created a repository to ease reproduce it: https://github.com/mariomka/laravel-pgsql-too-many-clients-test

While tests are running you can see Postgre connections count with:

select count(*) from pg_stat_activity;

Or full info with:

select * from pg_stat_activity;
@apstyx
Copy link

apstyx commented Mar 4, 2017

I am experiencing the same error with one difference, not making use of DatabaseMigrations in my tests.

@wins1908
Copy link

I have the fix for lumen above

@wins1908
Copy link

here is PR for Laravel #18752

@RobvH
Copy link
Contributor

RobvH commented Oct 24, 2017

Yep, same here, see my post on the PR for details.

@mfn
Copy link
Contributor

mfn commented Dec 16, 2017

I believe this was fixed (for Laravel) in 5.5 and at least backported to 5.4 (or it was introduced in 5.4, don't remember); I know this issue well because I had the same problem in the past.

It's all connected to this trait \Illuminate\Foundation\Testing\DatabaseTransactions.

See the code in 5.3:

    public function beginDatabaseTransaction()
    {
        $database = $this->app->make('db');

        foreach ($this->connectionsToTransact() as $name) {
            $database->connection($name)->beginTransaction();
        }

        $this->beforeApplicationDestroyed(function () use ($database) {
            foreach ($this->connectionsToTransact() as $name) {
                $database->connection($name)->rollBack();
            }
        });
    }

Now compare it to 5.4/5.5

    public function beginDatabaseTransaction()
    {
        $database = $this->app->make('db');

        foreach ($this->connectionsToTransact() as $name) {
            $database->connection($name)->beginTransaction();
        }

        $this->beforeApplicationDestroyed(function () use ($database) {
            foreach ($this->connectionsToTransact() as $name) {
                $connection = $database->connection($name);

                $connection->rollBack();
                $connection->disconnect();
            }
        });
    }

This contains the needed ->disconect() call.

It works for us (we've >2k tests, lots of them using this trait) and before this fix I basically had a copy of the trait doing exactly this and could remove it once we upgraded to 5.4/5.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants