@@ -328,8 +328,13 @@ public function beginTransaction()
328
328
}
329
329
if ($ this ->_transactionLevel === 0 ) {
330
330
$ this ->logger ->startTimer ();
331
- parent ::beginTransaction ();
332
- $ this ->logger ->logStats (LoggerInterface::TYPE_TRANSACTION , 'BEGIN ' );
331
+ try {
332
+ $ this ->performQuery (function () {
333
+ parent ::beginTransaction ();
334
+ });
335
+ } finally {
336
+ $ this ->logger ->logStats (LoggerInterface::TYPE_TRANSACTION , 'BEGIN ' );
337
+ }
333
338
}
334
339
++$ this ->_transactionLevel ;
335
340
return $ this ;
@@ -598,9 +603,30 @@ protected function _checkDdlTransaction($sql)
598
603
* @return \Zend_Db_Statement_Pdo|void
599
604
* @throws Zend_Db_Adapter_Exception To re-throw \PDOException.
600
605
* @throws Zend_Db_Statement_Exception
601
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
602
606
*/
603
607
protected function _query ($ sql , $ bind = [])
608
+ {
609
+ $ result = null ;
610
+ try {
611
+ $ this ->_checkDdlTransaction ($ sql );
612
+ $ this ->_prepareQuery ($ sql , $ bind );
613
+ $ this ->logger ->startTimer ();
614
+ $ result = $ this ->performQuery (fn () => parent ::query ($ sql , $ bind ));
615
+ } finally {
616
+ $ this ->logger ->logStats (LoggerInterface::TYPE_QUERY , $ sql , $ bind , $ result );
617
+ }
618
+
619
+ return $ result ;
620
+ }
621
+
622
+ /**
623
+ * Execute query and reconnect if needed.
624
+ *
625
+ * @param callable $queryExecutor
626
+ * @return \Zend_Db_Statement_Pdo|void
627
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
628
+ */
629
+ private function performQuery (callable $ queryExecutor )
604
630
{
605
631
$ connectionErrors = [
606
632
2006 , // SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
@@ -609,22 +635,15 @@ protected function _query($sql, $bind = [])
609
635
$ triesCount = 0 ;
610
636
do {
611
637
$ retry = false ;
612
- $ this ->logger ->startTimer ();
613
638
try {
614
- $ this ->_checkDdlTransaction ($ sql );
615
- $ this ->_prepareQuery ($ sql , $ bind );
616
- $ result = parent ::query ($ sql , $ bind );
617
- $ this ->logger ->logStats (LoggerInterface::TYPE_QUERY , $ sql , $ bind , $ result );
618
- return $ result ;
639
+ return $ queryExecutor ();
619
640
} catch (\Exception $ e ) {
620
641
// Finalize broken query
621
642
$ profiler = $ this ->getProfiler ();
622
643
if ($ profiler instanceof Profiler) {
623
- /** @var Profiler $profiler */
624
644
$ profiler ->queryEndLast ();
625
645
}
626
646
627
- /** @var $pdoException \PDOException */
628
647
$ pdoException = null ;
629
648
if ($ e instanceof \PDOException) {
630
649
$ pdoException = $ e ;
@@ -641,12 +660,10 @@ protected function _query($sql, $bind = [])
641
660
$ retry = true ;
642
661
$ triesCount ++;
643
662
$ this ->closeConnection ();
644
-
645
663
$ this ->_connect ();
646
664
}
647
665
648
666
if (!$ retry ) {
649
- $ this ->logger ->logStats (LoggerInterface::TYPE_QUERY , $ sql , $ bind );
650
667
$ this ->logger ->critical ($ e );
651
668
// rethrow custom exception if needed
652
669
if ($ pdoException && isset ($ this ->exceptionMap [$ pdoException ->errorInfo [1 ]])) {
0 commit comments