@@ -96,6 +96,16 @@ class Connection extends BaseConnection
9696 */
9797 protected bool $ strictOn = false ;
9898
99+ /**
100+ * InnoDB errno 1213 ended this transaction.
101+ */
102+ private bool $ transServerRolledBack = false ;
103+
104+ /**
105+ * The next statement must BEGIN before it runs.
106+ */
107+ private bool $ transNeedsServerRestart = false ;
108+
99109 /**
100110 * Checks whether the native database error represents a unique constraint violation.
101111 */
@@ -350,6 +360,18 @@ protected function execute(string $sql)
350360 }
351361
352362 try {
363+ // 1213 ended the server transaction. BEGIN again so this statement
364+ // does not autocommit. A failed BEGIN stays pending. 1205 is unchanged:
365+ // innodb_rollback_on_timeout decides that case.
366+ if ($ this ->connID instanceof mysqli && $ this ->serverTransactionRestartPending ()) {
367+ // Exception reporting off: a failed BEGIN returns false.
368+ if (! $ this ->connID ->begin_transaction ()) {
369+ return false ;
370+ }
371+
372+ $ this ->transNeedsServerRestart = false ;
373+ }
374+
353375 return $ this ->connID ->query ($ this ->prepQuery ($ sql ), $ this ->resultMode );
354376 } catch (mysqli_sql_exception $ e ) {
355377 log_message ('error ' , "{message} \nin {exFile} on line {exLine}. \n{trace} " , [
@@ -359,6 +381,10 @@ protected function execute(string $sql)
359381 'trace ' => render_backtrace ($ e ->getTrace ()),
360382 ]);
361383
384+ if ($ e ->getCode () === 1213 && $ this ->transDepth > 0 ) {
385+ $ this ->markServerRolledBackTransaction ();
386+ }
387+
362388 $ exception = $ this ->createDatabaseException ($ e ->getMessage (), $ e ->getCode (), $ e );
363389
364390 if ($ this ->DBDebug ) {
@@ -637,16 +663,53 @@ public function insertID(): int
637663
638664 protected function _transBegin (): bool
639665 {
640- return $ this ->connID ->begin_transaction ();
666+ if (! $ this ->connID ->begin_transaction ()) {
667+ return false ;
668+ }
669+
670+ $ this ->clearDeadlockTransactionState ();
671+
672+ return true ;
641673 }
642674
643675 protected function _transCommit (): bool
644676 {
677+ // mysqli::commit() returns true after 1213. End the transaction
678+ // and report failure instead.
679+ if ($ this ->transServerRolledBack ) {
680+ $ this ->transRollback ();
681+
682+ return false ;
683+ }
684+
645685 return $ this ->connID ->commit ();
646686 }
647687
648688 protected function _transRollback (): bool
649689 {
650- return $ this ->connID ->rollback ();
690+ if (! $ this ->connID ->rollback ()) {
691+ return false ;
692+ }
693+
694+ $ this ->clearDeadlockTransactionState ();
695+
696+ return true ;
697+ }
698+
699+ private function markServerRolledBackTransaction (): void
700+ {
701+ $ this ->transServerRolledBack = true ;
702+ $ this ->transNeedsServerRestart = true ;
703+ }
704+
705+ private function serverTransactionRestartPending (): bool
706+ {
707+ return $ this ->transNeedsServerRestart && $ this ->transDepth > 0 ;
708+ }
709+
710+ private function clearDeadlockTransactionState (): void
711+ {
712+ $ this ->transServerRolledBack = false ;
713+ $ this ->transNeedsServerRestart = false ;
651714 }
652715}
0 commit comments