• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

php / mysql事务不回滚故障Codeigniter框架

用户头像
it1352
帮助1

问题说明

下面我有以下代码。如果事务失败,则不会回滚。如果我删除锁表语句它回滚。为了使用锁和事务,有什么特别的吗?

below I have the following code. If the transaction fails, it is NOT being rolled back. If I remove the lock table statements it rolls back. Is there anything special I need to do in order to use locks and transactions?

function save ($items,$customer_id,$employee_id,$comment,$show_comment_on_receipt,$payments,$sale_id=false, $suspended = 0, $cc_ref_no = '', $auth_code = '', $change_sale_date=false,$balance=0, $store_account_payment = 0)
{
    if(count($items)==0)
        return -1;

    $sales_data = array(
        'customer_id'=> $this->Customer->exists($customer_id) ? $customer_id : null,
        'employee_id'=>$employee_id,
        'payment_type'=>$payment_types,
        'comment'=>$comment,
        'show_comment_on_receipt'=> $show_comment_on_receipt ?  $show_comment_on_receipt : 0,
        'suspended'=>$suspended,
        'deleted' => 0,
        'deleted_by' => NULL,
        'cc_ref_no' => $cc_ref_no,
        'auth_code' => $auth_code,
        'location_id' => $this->Employee->get_logged_in_employee_current_location_id(),
        'store_account_payment' => $store_account_payment,
    );

    $this->db->trans_start();

    //Lock tables invovled in sale transaction so we don't have deadlock
    $this->db->query('LOCK TABLES '.$this->db->dbprefix('customers').' WRITE, '.$this->db->dbprefix('sales').' WRITE, 
    '.$this->db->dbprefix('store_accounts').' WRITE, '.$this->db->dbprefix('sales_payments').' WRITE, '.$this->db->dbprefix('sales_items').' WRITE, 
    '.$this->db->dbprefix('giftcards').' WRITE, '.$this->db->dbprefix('location_items').' WRITE, 
    '.$this->db->dbprefix('inventory').' WRITE, '.$this->db->dbprefix('sales_items_taxes').' WRITE,
    '.$this->db->dbprefix('sales_item_kits').' WRITE, '.$this->db->dbprefix('sales_item_kits_taxes').' WRITE,'.$this->db->dbprefix('people').' READ,'.$this->db->dbprefix('items').' READ
    ,'.$this->db->dbprefix('employees_locations').' READ,'.$this->db->dbprefix('locations').' READ, '.$this->db->dbprefix('items_tier_prices').' READ
    , '.$this->db->dbprefix('location_items_tier_prices').' READ, '.$this->db->dbprefix('items_taxes').' READ, '.$this->db->dbprefix('item_kits').' READ
    , '.$this->db->dbprefix('location_item_kits').' READ, '.$this->db->dbprefix('item_kit_items').' READ, '.$this->db->dbprefix('employees').' READ , '.$this->db->dbprefix('item_kits_tier_prices').' READ
    , '.$this->db->dbprefix('location_item_kits_tier_prices').' READ, '.$this->db->dbprefix('location_items_taxes').' READ
    , '.$this->db->dbprefix('location_item_kits_taxes'). ' READ, '.$this->db->dbprefix('item_kits_taxes'). ' READ');


    $this->db->insert('sales',$sales_data);
    $sale_id = $this->db->insert_id();

    //A bunch of mysql other queries to save a sale


    $this->db->query('UNLOCK TABLES');

    $this->db->trans_complete();

    if ($this->db->trans_status() === FALSE)
    {
        return -1;
    }

    return $sale_id;

}

正确答案

#1

它主要是因为MySQLLOCK TABLES提交任何活动事务 之前尝试锁定表。

I believe it's mainly beacuse of the fact that MySQL "LOCK TABLES" commits any active transaction before attempting to lock the tables.

实际上,两个操作(LOCKING和TRANSACTIONS)之间的交互在MySQL中显得相当棘手,因为它在 MySQL手册页

Actually, the interaction between the two actions (LOCKING and TRANSACTIONS) looks quite tricky in MySQL, as it's very clearly and completely outlined in the MySQL manual page.

建议的解决方案是关闭 autocommit 标志(默认情况下为ON,因此通常每个发出的查询都会自动提交执行后)通过发出 SET autocommit = 0 命令:

The proposed solution is to turn OFF the autocommit flag (which is ON by default, so usually every query issued is automatically committed after execution) by issueing the SET autocommit = 0 command:


正确的方式使用
事务表[..]使用LOCK TABLES和UNLOCK TABLES将使用SET
autocommit = 0(而不是START TRANSACTION),然后是LOCK TABLES,以及
不调用UNLOCK TABLES直到您显式地提交事务。
例如,如果您需要写入表t1并从表t2读取
,您可以这样做:

The correct way to use LOCK TABLES and UNLOCK TABLES with transactional tables [..] is to begin a transaction with SET autocommit = 0 (not START TRANSACTION) followed by LOCK TABLES, and to not call UNLOCK TABLES until you commit the transaction explicitly. For example, if you need to write to table t1 and read from table t2, you can do this:

您的代码可能如下所示:

Your code could look like this:

$this->db->query("SET autocommit=0");
$this->db->query("LOCK TABLES...."); // your query here
$this->db->query("COMMIT");
$this->db->query("UNLOCK TABLES');

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhecfjja
系列文章
更多 icon
同类精品
更多 icon
继续加载