Suppose I'm a PHP application and I start an innodb transaction with mysqli_begin_transaction and it returns true. Then I do a series of inserts. As far as I am aware --- as a PHP application ---, all inserts went in perfectly. (Assume I checked all possible error codes.) It is my understanding that before I commit, the entire transaction could be rolled back --- due to a deadlock or something. Suppose that before I commit I delay a period of time doing something not MySQL related. Suppose further that --- during this delay --- MySQL gets into a situation where it must roll back my entire transaction. I claim I wouldn't know that it was rolled back because I'm not at this moment invoking any MySQL procedure. (Would the MySQL driver in PHP somehow discover so and throw an exception? I fear it would not, but I don't know.) When I'm ready to commit, it seems to me that MySQL will have no transaction pending relative to my connection, so I will be committing a new and empty transaction. MySQL reports no error when I commit an empty transaction, so I have no way of noticing that my entire transaction was rolled back.
Question. What is a small sample of code where I can be sure that the responsibility of my data in completely in MySQL's hands and not on mine? (In other words, how do I make sure (using PHP and MySQL) that when I commit a transaction, the entire transaction is successfully in MySQL's hands?)
Reference. Previously, I used to trust the return boolean from a mysqli_commit
. It is my understanding now that I cannot trust it. The answer in this linked-question might be saying that all I need is that try-catch with mysqli_report
not silenced. But I'm not convinced. Since I cannot easily reproduce an implicit rollback, I'm looking for two things --- (1) a sample of code where I can be sure the commit takes place as expected, (2) a deterministic way to reproduce an implicit rollback, if possible. It seems to me that (2) is not easy, so I'm researching (1) first.
Versions. I'm running MySQL 5.7.38-log and PHP 7.4.