-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
PDO Too many connections when running test suite #18471
Comments
Its problem with Naive solution is to increase
To check current
I tested
|
I believe the problem is that the connection should be reused (or a few of them pooled), not that the database has a too low max_connections value. |
What exactly do you mean reused? Connection is closed after each test, and until its closed new one opens up... I tested this using Innotop on my local machine (installed from homebrew), I have 69 test total, and each test class uses I found this post about this issue.
After that |
I had a similar problem not too long ago in Lumen, and solved it with: public function tearDown()
{
\DB::connection()->setPdo(null);
} |
I can also confirm this issue.
|
Do you use the DatabaseMigration's trait in your tests? Your solution looks like it's only applicable if you're using the DatabaseTransations trait. |
@ryrobbo yes, that is correct.
And it worked for me. I have to be explicit here, because i have a mongodb connection open which doesn't cause any problems. |
I have modified my TestCase class as follows: abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function setUpTraits()
{
parent::setUpTraits(); // TODO: Change the autogenerated stub
$uses = array_flip(class_uses_recursive(static::class));
if (isset($uses[DatabaseMigrations::class])) {
$this->beforeApplicationDestroyed(function () {
$this->app->make('db')->connection('mysql_test')->disconnect();
});
}
}
} Note: I am using an database connection called "mysql_test" to run my migrations during tests. But I am still getting the too many connections error. |
The best way we find to fix this issue was to move the test to sqlite driver:
and my phpunit.xml
With this we saved around 90% of time running our test cases. |
Also confirm this issue on PostgresSQL 9.4 @jycr753 tip is good to use |
For anyone coming across this issue now, I've followed @Kyslik 's advice and just manually set a high connections value (set global max_connections = 800;). Not the perfect solution but it's allowing me to run all of my tests. |
@ryrobbo the problem with that is that it does become a exponential issue, before I made the jump to sqlite for testing I was setting the connections to 4000 |
We are using Doctrine via $connection->getDoctrineConnection()->close();
\DB::purge($connectionName ?: null); Is this perhaps the issue you're facing as well? And is it intentional to have to clean up a Doctrine connection like this? (PHP 7.1, laravel/framework 5.4.27, doctrine/dbal 2.5.12, Postgres 9.5.7) |
@jycr753 There is always possibility to use sqlite driver with
But remember to enable constrains checks https://sqlite.org/foreignkeys.html |
I'm getting this error in Laravel 5.5.2, I'm not using DatabaseTransactions trait, I have a dedicated database to run integration tests. Laravel Version: 5.5.2 Running a test suite of 234 tests. |
Adding this option in the testing database solve the problem in Laravel 5.5.2 'options' => array(
PDO::ATTR_PERSISTENT => env('DB_PERSISTENT', true),
), |
Still getting this error in L5.4 from a test suite of 148 tests and going up. Laravel Version: 5.4.36 @avirdz solution worked, but it's obviously a workaround I think this is related to the fact that I forced the application creation into TestCase constructor
in order to use factories and access data already present in the database into dataProviders. Dunno if I should open a new issue or keep with this one. |
@IlCallo (great name btw, and I am being ironic...) L5.4 is not supported anymore. |
Of course, but the problem could be present also on 5.5, I'll check when upgrading. |
I'm currently experiencing this problem on 5.5.24. Adding this to my config solved it: 'options' => array(
PDO::ATTR_PERSISTENT => env('DB_PERSISTENT', true),
), |
Persistent DB did not solve it after all. Original solution from @ryrobbo did. |
Many of things here sound similar to #18056 . The major difference being: the other is talking about From what I read here, this issue suffers the same problem in the end: connections are not free'd back. Can anyone affected here try and use the |
I just noticed that both https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/Testing/RefreshDatabase.php I can’t test it at this moment. But I’m pretty this is the problem. The solution by the issue starter fixes it, and that just adds the missing disconnect. |
@GuidoHendriks was right, I've just replaced: $database->connection($name)->rollBack(); by: $connection = $database->connection($name);
$connection->rollBack();
$connection->disconnect(); in the |
Description:
I have a test suite which now has 140 tests. Each of my tests uses the DatabaseMigrations trait along with a dedicated database solely for tests to run against.
When PHP Unit gets to the last test it fails with the following error:
I can run this test individually and it passes fine.
After finding this issue (#10619 (comment)) I tried the following but the problem still persists:
PDO::ATTR_PERSISTENT => false
option to my test database connection entrySteps To Reproduce:
N/A
The text was updated successfully, but these errors were encountered: