stanley-king 3 年之前
父節點
當前提交
f89b6bdc7a
共有 3 個文件被更改,包括 39 次插入24 次删除
  1. 5 7
      core/framework/db/comysqli.php
  2. 2 2
      core/framework/libraries/CoPool.php
  3. 32 15
      core/framework/libraries/CoRefPool.php

+ 5 - 7
core/framework/db/comysqli.php

@@ -29,8 +29,7 @@ class Db
 
     private static function init_link()
     {
-        $cid = Co::getCid();
-        $link = CoMysqliPool::instance()->find($cid);
+        $link = CoMysqliPool::instance()->find(Co::getCid());
 
         $reset = false;
         if (!isset($link['master']) || (is_object($link['master']) == false)) {
@@ -44,7 +43,7 @@ class Db
         }
 
         if($reset) {
-            CoMysqliPool::instance()->reset($cid,$link);
+            CoMysqliPool::instance()->reset(Co::getCid(),$link);
         }
 
         return $link;
@@ -627,21 +626,20 @@ class Db
     public static function beginTransaction($host = 'master')
     {
         $link = self::init_link();
-        if (!self::$ifTransacting)
+        $ifTransacting = CoMysqliPool::instance()->transaction(Co::getCid());
+        if (!$ifTransacting)
         {
             $result = $link[$host]->autocommit(false);
             if ($result == false) {
                 $link = self::connect($host);
                 CoMysqliPool::instance()->reset($cid,$link);
-                self::$ifTransacting = false;
-
                 Log::record("autocommit start 2", Log::DEBUG);
                 $link[$host]->autocommit(false);
             } else {
                 Log::record("autocommit start 1", Log::DEBUG);
             }
         }
-        self::$ifTransacting = true;
+        CoMysqliPool::instance()->begin(Co::getCid());
     }
 
     public static function commit($host = 'master')

+ 2 - 2
core/framework/libraries/CoPool.php

@@ -36,7 +36,7 @@ abstract class CoPool
         }
         $this->mUsingCount++;
 
-        Log::record("get mUsingCount={$this->mUsingCount}",Log::DEBUG);
+        Log::record("redis get mUsingCount={$this->mUsingCount}",Log::DEBUG);
         return $client;
     }
 
@@ -50,7 +50,7 @@ abstract class CoPool
         }
         $count = count($this->mFreeClients);
         $waitings = count($this->mSuspendCIDS);
-        Log::record("resume cid={$cid} mUsingCount={$this->mUsingCount} frees={$count} waitings={$waitings}",Log::DEBUG);
+        Log::record("redis resume cid={$cid} mUsingCount={$this->mUsingCount} frees={$count} waitings={$waitings}",Log::DEBUG);
     }
 
     private function getSuspend()

+ 32 - 15
core/framework/libraries/CoRefPool.php

@@ -44,7 +44,7 @@ abstract class CoRefPool
         }
 
         $count = count($this->mUsingClients);
-        Log::record("get mUsingCount={$count}",Log::DEBUG);
+        Log::record("mysqli get cid={$cid} mUsingCount={$count}",Log::DEBUG);
 
         return $client;
     }
@@ -54,8 +54,9 @@ abstract class CoRefPool
         if(array_key_exists($cid,$this->mUsingClients)) {
             return $this->mUsingClients[$cid]['client'];
         } else {
-            Log::record(__METHOD__ . " cannot find mysqli cid={$cid} client",Log::DEBUG);
-            throw new Exception("cannot find mysqli cid={$cid} client");
+            $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
+            Log::record($msg,Log::DEBUG);
+            throw new Exception($msg);
         }
     }
 
@@ -64,8 +65,9 @@ abstract class CoRefPool
         if(array_key_exists($cid,$this->mUsingClients)) {
             return $this->mUsingClients[$cid]['client'] = $client;
         } else {
-            Log::record(__METHOD__ . " cannot find mysqli cid={$cid} client",Log::DEBUG);
-            throw new Exception("cannot find mysqli cid={$cid} client");
+            $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
+            Log::record($msg,Log::DEBUG);
+            throw new Exception($msg);
         }
     }
 
@@ -74,8 +76,20 @@ abstract class CoRefPool
         if(array_key_exists($cid,$this->mUsingClients)) {
             return $this->mUsingClients[$cid]['ifTransacting'];
         } else {
-            Log::record(__METHOD__ . " cannot find mysqli cid={$cid} client",Log::DEBUG);
-            throw new Exception("cannot find mysqli cid={$cid} client");
+            $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
+            Log::record($msg,Log::DEBUG);
+            throw new Exception($msg);
+        }
+    }
+
+    public function begin($cid)
+    {
+        if(array_key_exists($cid,$this->mUsingClients)) {
+            return $this->mUsingClients[$cid]['ifTransacting'] = true;
+        } else {
+            $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
+            Log::record($msg,Log::DEBUG);
+            throw new Exception($msg);
         }
     }
 
@@ -84,8 +98,9 @@ abstract class CoRefPool
         if(array_key_exists($cid,$this->mUsingClients)) {
             return $this->mUsingClients[$cid]['ifTransacting'] = false;
         } else {
-            Log::record(__METHOD__ . " cannot find mysqli cid={$cid} client",Log::DEBUG);
-            throw new Exception("cannot find mysqli cid={$cid} client");
+            $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
+            Log::record($msg,Log::DEBUG);
+            throw new Exception($msg);
         }
     }
 
@@ -94,8 +109,9 @@ abstract class CoRefPool
         if(array_key_exists($cid,$this->mUsingClients)) {
             return $this->mUsingClients[$cid]['ifTransacting'] = false;
         } else {
-            Log::record(__METHOD__ . " cannot find mysqli cid={$cid} client",Log::DEBUG);
-            throw new Exception("cannot find mysqli cid={$cid} client");
+            $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
+            Log::record($msg,Log::DEBUG);
+            throw new Exception($msg);
         }
     }
 
@@ -118,15 +134,16 @@ abstract class CoRefPool
                     Co::resume($suspend_cid);
                 }
 
-                Log::record("CoRefPool put cid={$cid} suspend_cid={$suspend_cid} refcount={$refcount}",Log::DEBUG);
+                Log::record("mysqli CoRefPool put cid={$cid} suspend_cid={$suspend_cid} refcount={$refcount}",Log::DEBUG);
             }
             else {
-                Log::record("CoRefPool put cid={$cid} refcount={$refcount}",Log::DEBUG);
+                Log::record("mysqli CoRefPool put cid={$cid} refcount={$refcount}",Log::DEBUG);
             }
         }
         else {
-            Log::record(__METHOD__ . " cannot find mysqli cid={$cid} client",Log::DEBUG);
-            throw new Exception("cannot find mysqli cid={$cid} client");
+            $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
+            Log::record($msg,Log::DEBUG);
+            throw new Exception($msg);
         }
     }