-
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
[5.4] Fix 'Too Many Connections' error during testing using $connectionsToTransact
#20340
[5.4] Fix 'Too Many Connections' error during testing using $connectionsToTransact
#20340
Conversation
…ting while using the DatabaseTransactions trait.
I run into this one all the time. The point where I created a trait that does this (but does so by overriding tearDown by keeping temp variables around to be able to call setPDO(null) on it afterwards). Never noticed connection had a disconnect method. I quite like this approach. How well does it work when used with the DatabaseMigrations trait? |
$connectionsToTransact
$connectionsToTransact
What causes the too many connections? Obviously this doesn't happen on every test suite. Is adding this going to seriously affect performance of the tests? |
Maybe related: #18471 |
I think something is holding onto the connection object. Maybe there's a reference cycle that's not being cleaned up?
Most likely because:
If you have an application that connects to more than one database (I have one that uses 4 for instance) it makes this problem pop up much, much quicker. |
In my case I am using MySql for the testing. I'm not sure where the default 'mysql' connection is terminated, but upon testing it appears that it is terminated after each test is ran, preventing this sort of issue from occurring if a user is not using the |
@taylorotwell If I increase my So these changes actually increased performance in my scenario, which makes sense because my database isn't trying to maintain hundreds of connections simultaneously. |
I would also think that any proponent of TDD would say that my suite of 36 tests is child's play, not suitable as a true testing solution. In fact the tests in question are only my acceptance tests, this issue would be greatly exacerbated if I include integration tests across components that reference multiple database connections. |
Edit: never mind using I did a bit of testing. I can reproduce the error with this script: require __DIR__.'/bootstrap/autoload.php';
for ($i = 0; $i < 300; $i++) {
$app = require __DIR__.'/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$app["db"]->connection(null)->beginTransaction();
$app["db"]->connection(null)->rollBack();
$app = null;
echo ".";
} And it appears that |
Many thanks for the PR! Can also confirm it works. I feel a bit bad because I was using a custom trait with exactly this changes for years but failed to provide an upstream PR :-/ |
I am still having this problem in Laravel 5.6.8 with the |
Terminate user defined database connections after rollback while using the DatabaseTransactions trait.
Note: This bug fix is pointed to the 5.4 branch as the corresponding code doesn't exist in the 5.1 release.