dongleiwei2182 2013-05-02 14:38
浏览 32
已采纳

Yii交易没有回滚

enter image description herei have the following piece of code and i pass it some data to generate an exception and test if the transaction rollback is happening. It seems it's not and i'm not sure why. can someone help me? thanks

            $transaction = Yii::app()->db->beginTransaction();

            try {

                //.....

                //call private methods
                $category = MyController::saveCategory($params);
                $test_saved = MyController::saveTest($params);
                MyController::saveCommunity($param);   // here is an exception and it should rollback the transaction but it doesn't  

                $transaction->commit();

            } catch(Exception $e) {
                $transaction->rollback();
                throw new Exception($e);
            }


            private function saveCommunity($param){

                  $community = new Community();
                  $community->user_id = $user_id;
                  $community->name = $name; 
                  $community->id = 71;  // this is a duplicate primary key and will generate an exception


                  try{
                      $community->save(false, null);
                  }catch(Exception $e){
                     throw $e;
                  }

                  return $community;

            }

(mysql tables are set to innodb)

  • 写回答

2条回答 默认 最新

  • duanbipu1720 2013-05-02 15:04
    关注

    Try changing your code responsible for exception throwing:

              try{
                  $community->save(false, null);
              }catch(Exception $e){
                 throw $e;
              }
    

    to something like:

              if(!$community->save(false, null))
                   throw new Exception('Error saving');
    

    And remove the exception throwing here:

                } catch(Exception $e) {
                $transaction->rollback();
                //throw new Exception($e);
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试