Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ktotes's avatar

Test Suite "General error: 1205 Lock wait timeout exceeded"

I'm having an odd issue with my test suite that I'm struggling to narrow down. Using the DatabaseTransactions trait and a MySQL database connection.

The vast majority of the tests pass however the exact same 15 or so are failing due to the below:

Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction

or

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction (SQL: insert into `friendships` (`created_at`, `friend_id`, `updated_at`, `user_id`) values (2019-07-10 10:36:12, 1131, 2019-07-10 10:36:12, 1130))

However if I run the tests individually they pass all green. The error only occurs when running my full suite with ./vendor/bin/phpunit.

They are very simple tests for example for checking if two Users are friends.

public function test_is_friends_with_helper(): void
{
        $user = factory(User::class)->state(User::CUSTOMER)->create([
            'client_id' => $this->client->id,
        ]);

        $result = $this->customer->fresh()->isFriendsWith($user);

        $this->assertFalse($result);

        $this->customer->friends()->attach($user);

        $result = $this->customer->fresh()->isFriendsWith($user);

        $this->assertTrue($result);
}

Falling back to using DatabaseMigrations trait "fixes" the issue but then obviously the whole suite will run way slower.

Does anyone have any ideas?

EDIT: Fixed.

parent::tearDown();
0 likes
3 replies
bobbybouwmann's avatar

It's probably a transaction that hasn't been closed! This should be done by Laravel itself!

You need to add the parent::tearDown() call in your tearDown method

public function tearDown()
{
    parent::tearDown();

    // Do your own tearDown here
}
2 likes
Tray2's avatar

Yes most definately a transaction issue.

somewhere in your code you do a select for update and then you don't do a commit or rollback. Then the database is locking that record and then you try to update that record and it's locked by the database from the previoustransaction. The message is when the second transactions times out.

If you really need to use transactions read up on it and to prevent locking situations you can use no wait skipped locked. However consider if you really need to use transactions and if you do read up on it.

Please or to participate in this conversation.