浏览代码

fix transaction err

stanley-king 3 年之前
父节点
当前提交
7b0ccccfe9
共有 3 个文件被更改,包括 45 次插入22 次删除
  1. 14 17
      core/framework/db/mysqli.php
  2. 7 5
      test/TestBonus.php
  3. 24 0
      test/TestRefillThird.php

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

@@ -17,7 +17,7 @@ class Db
     const ErrLock = 1205;
     const ErrLock = 1205;
 
 
     private static $link = [];
     private static $link = [];
-    private static $ifTransacting = false;
+    private static $ifTransacting = 0;
 
 
     private function __construct()
     private function __construct()
     {
     {
@@ -606,29 +606,25 @@ class Db
     public static function beginTransaction($host = 'master')
     public static function beginTransaction($host = 'master')
     {
     {
         self::init_link();
         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);
         }
         }
-        self::$ifTransacting = true;
+        self::$ifTransacting += 1;
     }
     }
 
 
     public static function commit($host = 'master')
     public static function commit($host = 'master')
     {
     {
-        if (self::$ifTransacting) {
+        self::$ifTransacting -= 1;
+
+        if (self::$ifTransacting === 0)
+        {
             $result = self::$link[$host]->commit();
             $result = self::$link[$host]->commit();
-            Log::record("autocommit end commit", Log::DEBUG);
             self::$link[$host]->autocommit(true);
             self::$link[$host]->autocommit(true);
-            self::$ifTransacting = false;
 
 
             if (!$result) {
             if (!$result) {
                 $err = mysqli_error(self::$link[$host]);
                 $err = mysqli_error(self::$link[$host]);
+                self::connect($host);
                 throw_exception("Db Error: {$err}");
                 throw_exception("Db Error: {$err}");
             }
             }
         }
         }
@@ -636,11 +632,12 @@ class Db
 
 
     public static function rollback($host = 'master')
     public static function rollback($host = 'master')
     {
     {
-        if (self::$ifTransacting) {
+        self::$ifTransacting -= 1;
+
+        if (self::$ifTransacting === 0)
+        {
             $result = self::$link[$host]->rollback();
             $result = self::$link[$host]->rollback();
-            Log::record("autocommit end rollback", Log::DEBUG);
             $fsuccess = self::$link[$host]->autocommit(true);
             $fsuccess = self::$link[$host]->autocommit(true);
-            self::$ifTransacting = false;
 
 
             if (!$result || !$fsuccess) {
             if (!$result || !$fsuccess) {
                 $err = mysqli_error(self::$link[$host]);
                 $err = mysqli_error(self::$link[$host]);

+ 7 - 5
test/TestBonus.php

@@ -29,6 +29,13 @@ class TestBonus extends PHPUnit_Framework_TestCase
 
 
     }
     }
 
 
+    public function testTransWrapper()
+    {
+
+
+
+    }
+
     public function testSend()
     public function testSend()
     {
     {
         $param = bonus\parameters::test_bonus();
         $param = bonus\parameters::test_bonus();
@@ -98,9 +105,6 @@ class TestBonus extends PHPUnit_Framework_TestCase
         ksort($data);
         ksort($data);
         $xdata = $data;
         $xdata = $data;
         krsort($data);
         krsort($data);
-        //arsort($data);
-
-//        $xxxx = array_reverse($xdata,true);
         $xxxx = [];
         $xxxx = [];
         foreach ($data as $key => $val) {
         foreach ($data as $key => $val) {
             $xxxx[$key] = $val;
             $xxxx[$key] = $val;
@@ -157,8 +161,6 @@ class TestBonus extends PHPUnit_Framework_TestCase
 
 
     public function testMakeSn()
     public function testMakeSn()
     {
     {
-//        $t = (float) microtime();
-//        $x = sprintf('%03d', (float) microtime() * 1000);
         $sns = [];
         $sns = [];
         for ($index = 0; $index < 100000; $index++) {
         for ($index = 0; $index < 100000; $index++) {
             $sns[] = $this->make_sn();
             $sns[] = $this->make_sn();

+ 24 - 0
test/TestRefillThird.php

@@ -2,6 +2,7 @@
 
 
 
 
 use PHPUnit\Framework\TestCase;
 use PHPUnit\Framework\TestCase;
+use refill\util;
 
 
 define('APP_ID', 'test');
 define('APP_ID', 'test');
 define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
 define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
@@ -425,4 +426,27 @@ class TestRefillThird extends TestCase
     {
     {
         refill\RefillFactory::instance();
         refill\RefillFactory::instance();
     }
     }
+
+    public function testManualCancel()
+    {
+        $order_id = 87845887;
+
+        try {
+            $mod_order = Model('vr_order');
+            $logic_vr_order = Logic("vr_order");
+
+            $tran = new trans_wapper($mod_order,'manual_cancel state trans');
+
+            $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true);
+            $logic_vr_order->changeOrderStateCancel($order_info, '', "后台手动回调通知失败",true,true);
+            $tran->commit();
+
+            return true;
+        }
+        catch (Exception $ex) {
+            $tran->rollback();
+            Log::record("manual_cancel exception:{$ex->getMessage()}",Log::ERR);
+            return false;
+        }
+    }
 }
 }