Skip to content

DatabaseMigrations trait exhausts database connections #10619

@mfn

Description

@mfn

Using DatabaseMigrations with a lot of tests will exhaust all connections to the dabatase, i.e. they're not freed/returned to the system.

How to reproduce, as suggested from http://laravel.com/docs/5.1/testing#resetting-the-database-after-each-test :

  1. composer.phar create-project laravel/laravel --prefer-dist
  2. cd laravel
  3. Change .env to point to a valid database connecion
  4. Create a test script with many tests, using the trait; e.g. using this bash lines:
echo "<?php class ExhaustionTest extends TestCase {" > tests/ExhaustionTest.php
echo "use Illuminate\Foundation\Testing\DatabaseMigrations;" >> tests/ExhaustionTest.php
for i in $(seq 1 500); do echo "function test$i() {}" >>  tests/ExhaustionTest.php; done
echo "}" >> tests/ExhaustionTest.php
  1. Run the tests with vendor/bin/phpunit

After a some tests they start to fail with the following error (or similar, depending on your DB):
PDOException: SQLSTATE[08004] [1040] Too many connections

Here's an example trace:

1) ExhaustionTest::test143
PDOException: SQLSTATE[08004] [1040] Too many connections

/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:55
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php:22
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php:60
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php:49
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:175
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:67
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Database/Migrations/DatabaseMigrationRepository.php:171
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Database/Migrations/DatabaseMigrationRepository.php:139
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:393
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:100
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:58
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:503
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Console/Command.php:150
/home/vagrant/src/test/laravel/vendor/symfony/console/Command/Command.php:259
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Console/Command.php:136
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Console/Application.php:62
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:159
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Testing/ApplicationTrait.php:299
/home/vagrant/src/test/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php:12

To fix this for my suite, I created a trait similar to this:

trait DatabaseMigrationsCleanupTrait
{
    use Illuminate\Foundation\Testing\DatabaseMigrations;

    /**
     * @after
     */
    public function disconnectAllDatabases()
    {
        /** @var \Illuminate\Database\Connection $connection */
        foreach (\DB::getConnections() as $connection) {
            $connection->disconnect();
        }
    }
}

I would open a PR if someone points me in the right direction where the fix would be appropriate.

I tried this, which also worked:

diff --git a/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php b/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php
index d53b34e..b4a3205 100644
--- a/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php
+++ b/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php
@@ -13,6 +13,9 @@ trait DatabaseMigrations

         $this->beforeApplicationDestroyed(function () {
             $this->artisan('migrate:rollback');
+            foreach (\DB::getConnections() as $connection) {
+                $connection->disconnect();
+            }
         });
     }
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions