stanley-king 3 tahun lalu
induk
melakukan
f8f52557c0
1 mengubah file dengan 17 tambahan dan 15 penghapusan
  1. 17 15
      core/framework/db/mysqli.php

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

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