doushou8299 2014-08-25 22:58
浏览 42
已采纳

SQL Codeigniter:从控制器创建保存点并回滚到保存点/回滚多个事务

I have processing db transaction from page using ajax request if user don't press submit button than roll-back all sql transaction which is done by ajax request (i will manage this but currently following logic not working if refresh current page).

I have try following code but not working,

function viewPage(){
   $needRollBack=$this->session->userdata('needRollBack');
   if($needRollBack){
      $this->db->trans_rollback();
   }
   $this->db->trans_begin();
   $this->MyModel1->insert(.....);
   $this->MyModel2->insert(.....);
.........................
}

function submitDetails(){
   $this->db->trans_complete();
   $this->session->set_userdata('needRollBack',false);
}

when again viewPage() function call or refresh page than if submitDetails() not called than rolleback all sql stransaction done via ajax request (starting point from trans_begin()) will be roll-baked? is this possible? Kindly guide me...

  • 写回答

1条回答 默认 最新

  • dsfd3546 2014-09-14 19:03
    关注

    There are a number of things that need addressing here.

    First would be that your database and schema type must support transactions (e.g. if you are using MyISAM table type in MySQL you will not be able to use transactions).

    Second to test whether or not transactions are succeeding in CI you write error messages to the error log if $this->db->trans_status() === false.

    Finally I would use an alternative approach to the one above (if possible) - one possible approach would be to store your data in a session (through the different stages) and make the call or multiple database calls at the final point (when user clicks submit). You can still use transactions this way and it simplifies the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部