NielsNumbers's avatar

Parallel test fail with [1040] Too many connections ?

I have around 500 tests and executing all of them is very slow, so I want to run them in parallel (https://laravel.com/docs/9.x/testing#running-tests-in-parallel).

In addition, i am using DBTransaction on my tests, because I have a lot of tables and the migration itself takes quite some time, so the transaction approach makes my tests much faster. If one uses DatabaseCreation then the test dbs would be created during runtime, but for DBTransaction thats not the case so I had to create the tabels my_db_test_1 and my_db_test_2 before hand and also migrate those.

After those, I run sail artisan test --parallel --processes=2 and from my 500 tests most passed, but 30 of them failed, all with the same error:

Doctrine\DBAL\Driver\PDO\Exception: SQLSTATE[08004] [1040] Too many connections

I also tried to disconnect db on teardown as suggested in https://github.com/laravel/framework/issues/18471#issuecomment-289433808 to my TestCase.php but this had no effect. Still the same error. I also think its not needed anymore, as inside DBTransaction, the connection is disconnected:.

In my mysql container, max_connections was set to 151, I increased it to 200, but it had no effect either. However, whats strange is that those errors appear only after 200 tests. In the first 200 tests, no error is thrown.

Any idea what could cause "too many connections"?

0 likes
3 replies
cmackinlay's avatar

I have the same issue with parallel testing and also use DBtransaction for a similar reason - it is a multi-tenant application and while the central database is lightweight there are lots of tables and seeding needed in the tenant DB and that is too costly to create each time.

WIth 8 processes, it takes a while to set up 8 tenant DBs but then the test suite runs fast. Because the tenants DBs aren't removed until the end of the test run, Laravel doesn't close the connections until then so they build up to well over 500.

I've got round it by setting max_connections to 2000 but the annoying thing is this has to be done manually after a reboot as when the mysql docker container restarts it doesn't retain that. I haven't yet found a way to add additional config to the container but will work on this next.

Tray2's avatar

You can define how many connections you are allowed to have in your RDBMS, however the migration is just run once, and then transactions are used by default, so there isn't any need to use those in your tests.

However there is a package that I like to use when running tests where I need to use the database.

plannr/laravel-fast-refresh-database

https://github.com/PlannrCrm/laravel-fast-refresh-database

Please or to participate in this conversation.