瀏覽代碼

debug transaction

stanley-king 8 年之前
父節點
當前提交
9504f38060
共有 5 個文件被更改,包括 23 次插入15 次删除
  1. 2 2
      core/framework/libraries/log.php
  2. 6 5
      data/model/user_bonus.model.php
  3. 2 2
      helper/bonus/grab.php
  4. 1 4
      helper/bonus_helper.php
  5. 12 2
      test/DBTest.php

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

@@ -57,7 +57,7 @@ class Log
             $level = 'SQL';
             $content = "[{$now}] {$level}: {$message}\r\n";
             if(self::open_sql) {
-                $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'-sql.log';
+                $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'.log';
                 file_put_contents($log_file,$content, FILE_APPEND);
             }
             self::add_sql_log($message);
@@ -81,7 +81,7 @@ class Log
         $content = "\r\n";
 
         if($lev == self::SQL && self::open_sql) {
-            $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'-sql.log';
+            $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'.log';
             file_put_contents($log_file,$content, FILE_APPEND);
             return;
         }

+ 6 - 5
data/model/user_bonus.model.php

@@ -128,7 +128,6 @@ class user_bonusModel extends Model
                 }
             }
 
-            $this->beginTransaction();
             $bonus = $this->where(array('type_id' => $type_id,'session_id' => $sess_id))->field($fields)->order('bonus_status,bonus_id')->limit(1)->find();
             if(empty($bonus))
             {
@@ -166,10 +165,13 @@ class user_bonusModel extends Model
                             'session_id' => $sess_id);
                     }
 
+                    $this->beginTransaction();
                     $ret = $this->where(array('type_id' => $type_id,'bonus_id' => $bonus['bonus_id']))->update($datas);
                     $affect_rows = $this->affected_rows();
+                    $this->commit();
 
-                    if($ret && $affect_rows > 0) {
+                    Log::record("user_bonusModel::grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
+                    if($ret != false && $affect_rows > 0) {
                         $bonus = array_merge($bonus,$datas);
                     } else {
                         $bonus = array();
@@ -178,7 +180,6 @@ class user_bonusModel extends Model
                     }
                 }
             }
-            $this->commit();
 
             return $bonus;
         } catch (Exception $ex) {
@@ -199,7 +200,6 @@ class user_bonusModel extends Model
                 return false;
             }
 
-            $this->beginTransaction();
             $condition = array('type_id' => $type_id,'bonus_id' => $bonus_id,'session_id' => $sess_id,'bonus_status' => 1);
             $datas = array('bonus_status' => 2,
                 'user_mobile' => $mobile,
@@ -210,11 +210,12 @@ class user_bonusModel extends Model
                 $datas['user_id'] = $_SESSION['member_id'];
             }
 
+            $this->beginTransaction();
             $ret = $this->where($condition)->update($datas);
             $affect_rows = $this->affected_rows();
             $this->commit();
 
-            return ($ret && $affect_rows > 0);
+            return ($ret != false && $affect_rows > 0);
         } catch (Exception $ex) {
             $this->rollback();
             return false;

+ 2 - 2
helper/bonus/grab.php

@@ -58,8 +58,9 @@ class general_grab extends IGrab
 
         if(!empty($bonus))
         {
-            Db::beginTransaction();
             $user_bonus = user_bonus::create_by_param($bonus);
+
+            Db::beginTransaction();
             if($is_new_grab && $is_new_bind)
             {
                 Log::record("general_grab->get_bonus 1");
@@ -82,7 +83,6 @@ class general_grab extends IGrab
                 Model('bonus_type')->edit(array('type_id' => $type_id), array('grabed_num' => array('exp', 'grabed_num+1')));
             }
             Db::commit();
-
             Log::record("general_grab->get_bonus end with one bonus.");
             return $user_bonus;
         } else {

+ 1 - 4
helper/bonus_helper.php

@@ -157,20 +157,17 @@ class bonus_helper
         foreach($bind_bonus as $val)
         {
             $type_id = $val['type_id'];
-            $type = \bonus\type::create_by_id($type_id);
+            $type  = \bonus\type::create_by_id($type_id);
             $bonus = \bonus\user_bonus::create_by_param($val);
 
             try
             {
-                Db::beginTransaction();
                 if($manager->topup($mod_bonus,$val) == true) {
                     array_push($bonusex,$val);
                     ranklist_helper::add_money($_SESSION['member_id'],$bonus->bonus_value());
                     $pred->add_bonus($bonus,$type);
                 }
-                Db::commit();
             } catch (Exception $ex) {
-                Db::rollback();
             }
         }
 

+ 12 - 2
test/DBTest.php

@@ -30,9 +30,7 @@ class DBTest extends PHPUnit_Framework_TestCase
     public function testModel()
     {
         $result = Model()->table('config')->select();
-        
         $y = Model('member');
-
         $start = microtime(true);
         for ($i = 0; $i < 100; ++$i) {
             $member1 = Model('member');
@@ -44,6 +42,18 @@ class DBTest extends PHPUnit_Framework_TestCase
 
         echo  $period;
     }
+    public function testTransaction()
+    {
+        $mod_member = Model('member');
+        $mod_member->beginTransaction();
+        $mod_member->where(array('member_id' => 36490))->update(array('member_name' => '江南舟子F'));
+        $affect_rows_a = $mod_member->affected_rows();
+        //$mod_member->rollback();
+        $mod_member->commit();
+        $affect_rows_b = $mod_member->affected_rows();
+
+
+    }
 
     public function testConnection()
     {