Laravel MySQL Query Return Empty When there is a Deadlock

Suppose you have two processes executing the following PHP code at the same time, what results will be printed?

Surprisingly, one process prints the query results, while another process prints an empty collection (array). The code that returned an empty collection did not trigger a deadlock exception as expected.

However, if you have checked the source code of the transaction method, you will find that this method should automatically retry the transaction after a deadlock is found. When the number of deadlocks reaches a set value, the deadlock will be thrown as an exception finally. But, the current situation is that the query looks normal because it returns an empty collection!

This unexpectedly returned empty array and un-thrown deadlock exception is actually an old and tricky PHP-PDO bug. Before 7.4.13 was released, it had been widely present in a large number of PHP versions. There are lots discussions related to this, such as:


Before PHP7.4.13, there was almost no way to detect this bug directly. Never try to open PDO::ATTR_EMULATE_PREPARES to solve this problem! Because this option will cause all your PDO query results to become strings, and the loss of data types will cause more serious consequences.

Therefore, the only feasible way is to manually compile the repaired PHP source code, or install the new pre-compiled version of PHP7.4.13.

Leave a Reply

Your email address will not be published. Required fields are marked *