stanley-king 3 years ago
parent
commit
aee24616c9
2 changed files with 33 additions and 31 deletions
  1. 19 17
      core/framework/db/mysqli.php
  2. 14 14
      helper/refill/RefillBase.php

+ 19 - 17
core/framework/db/mysqli.php

@@ -17,7 +17,7 @@ class Db
     const ErrLock = 1205;
 
     private static $link = [];
-    private static $ifTransacting = false;
+    private static $ifTransacting = 0;
 
     private function __construct()
     {
@@ -127,6 +127,10 @@ class Db
                 $error = "DbError eno={$eno} msg={$emsg}";
                 Log::record("{$error} sql={$sql}", Log::ERR);
 
+                if(self::$ifTransacting > 0) {
+                    throw new Exception($error);
+                }
+
                 if ($eno == self::ErrUnConnect) {
                     if ($count > 0) return false;
                     self::connect($host);
@@ -605,29 +609,26 @@ class Db
     public static function beginTransaction($host = 'master')
     {
         self::init_link();
-        if (!self::$ifTransacting) {
-            $result = self::$link[$host]->autocommit(false);
-            if ($result == false) {
-                self::connect($host);
-                self::$ifTransacting = false;
-                self::$link[$host]->autocommit(false);
-            } else {
-                Log::record("autocommit success", Log::DEBUG);
-            }
+
+        if (self::$ifTransacting === 0) {
+            self::$link[$host]->autocommit(false);
+            Log::record("transaction begin",Log::DEBUG);
         }
-        self::$ifTransacting = true;
+        self::$ifTransacting += 1;
     }
 
     public static function commit($host = 'master')
     {
-        if (self::$ifTransacting) {
+        self::$ifTransacting -= 1;
+        if (self::$ifTransacting === 0)
+        {
             $result = self::$link[$host]->commit();
-            Log::record("autocommit end commit", Log::DEBUG);
             self::$link[$host]->autocommit(true);
-            self::$ifTransacting = false;
+            Log::record("transaction commit",Log::DEBUG);
 
             if (!$result) {
                 $err = mysqli_error(self::$link[$host]);
+                self::connect($host);
                 throw_exception("Db Error: {$err}");
             }
         }
@@ -635,11 +636,12 @@ class Db
 
     public static function rollback($host = 'master')
     {
-        if (self::$ifTransacting) {
+        self::$ifTransacting -= 1;
+        if (self::$ifTransacting === 0)
+        {
             $result = self::$link[$host]->rollback();
-            Log::record("autocommit end rollback", Log::DEBUG);
             $fsuccess = self::$link[$host]->autocommit(true);
-            self::$ifTransacting = false;
+            Log::record("transaction rollback",Log::DEBUG);
 
             if (!$result || !$fsuccess) {
                 $err = mysqli_error(self::$link[$host]);

+ 14 - 14
helper/refill/RefillBase.php

@@ -123,7 +123,6 @@ class RefillBase
             $card_type = intval($refill_info['card_type']);
             $spec = intval($refill_info['refill_amount']);
             $mchid = intval($refill_info['mchid']);
-            $org_quality = intval($refill_info['org_quality']);
             $mch_order = $refill_info['mch_order'];
 
             if ($success) {
@@ -131,18 +130,19 @@ class RefillBase
                 util::incr_user_success($mchid,$card_type, $spec,$quality);
                 $logic_vr_order->changeOrderStateSuccess($order_id,true);
                 util::onOrderSuccess($refill_info,$order_info);
+                $tran->commit();
             }
             elseif ($can_try)
             {
                 util::incr_notify($chname, $card_type, $spec, $quality, false);
                 util::incr_amount_lock($mchid,$card_type,$spec);
                 $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,正在重试",true,true);
+                $mod_refill->edit($order_id, ['is_retrying' => 1,'notify_time' => time()]);
+                $tran->commit();
 
                 [$can_retry,$params] = $this->retry($refill_info, $order_info);
                 if ($can_retry)
                 {
-                    $mod_refill->edit($order_id, ['is_retrying' => 1,'notify_time' => time()]);
-                    $tran->commit();
                     if(util::push_add($params)) {
                         return true;
                     }
@@ -152,23 +152,23 @@ class RefillBase
                 util::incr_notify($chname, $card_type, $spec, $quality, false);
                 util::incr_amount_lock($mchid,$card_type,$spec);
                 $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,不可重试.",true,true);
+                $tran->commit();
+            }
+
+            $mod_refill->edit($order_id, ['notify_time' => time(), 'is_retrying' => 0,'notify_state' => 1]);
+            util::pop_queue_order($mchid,$mch_order);
+            QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
+
+            if(!$success) {
+                util::incr_user_fail($mchid,$card_type, $spec,$quality);
             }
-            $tran->commit();
+            return true;
         }
         catch (Exception $ex) {
             $tran->rollback();
             Log::record("Error:" . $ex->getMessage(), Log::ERR);
+            return false;
         }
-
-        if(!$success) {
-            util::incr_user_fail($mchid,$card_type, $spec,$quality);
-        }
-
-        $mod_refill->edit($order_id, ['notify_time' => time(), 'is_retrying' => 0,'notify_state' => 1]);
-        util::pop_queue_order($mchid,$mch_order);
-        QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
-
-        return true;
     }
 
     private function retry(array $refill_info, array $order_info)