@@ -14,24 +14,24 @@ class DBTransactionRetryHelper
1414 * @param callable $callback The transaction logic to execute.
1515 * @param int $maxRetries Number of times to retry on deadlock.
1616 * @param int $retryDelay Delay between retries in seconds.
17- * @param string $logFileName
18- * @return void
17+ * @param string $logFileName The log file name
18+ * @return mixed
1919 * @throws QueryException
2020 * @throws Throwable
2121 */
22- public static function transactionWithRetry (callable $ callback , int $ maxRetries = 5 , int $ retryDelay = 5 , string $ logFileName = 'mysql-deadlocks-log ' )
22+ public static function transactionWithRetry (callable $ callback , int $ maxRetries = 5 , int $ retryDelay = 5 , string $ logFileName = 'mysql-deadlocks-log ' ): mixed
2323 {
2424 $ attempt = 0 ;
2525 $ throwable = null ;
2626 $ log = [];
2727 while ($ attempt < $ maxRetries ) {
2828 try {
2929 // Execute the transaction
30- $ done = true ;
30+ $ exceptionCatched = false ;
3131 return DB ::transaction ($ callback );
3232
3333 } catch (QueryException $ e ) {
34- $ done = false ;
34+ $ exceptionCatched = true ;
3535 // Check if the error is a deadlock (MySQL error code 1213)
3636 if ($ e ->getCode () == 1213 || $ e ->getCode () == 40001 || $ e ->errorInfo [1 ] == 1213 ) {
3737 $ attempt ++;
@@ -68,7 +68,7 @@ public static function transactionWithRetry(callable $callback, int $maxRetries
6868 $ throwable = $ e ;
6969 }
7070 } finally {
71- if (is_null ($ throwable ) and $ done ) {
71+ if (is_null ($ throwable ) and ! $ exceptionCatched ) {
7272 if (count ($ log ) > 0 ) {
7373 generateLog ($ log [count ($ log ) - 1 ], $ logFileName , 'warning ' );
7474 }
0 commit comments