无法完成交易;超过锁定等待超时;尝试重新启动事务

时间:2013-08-09 02:47:28

标签: php mysql symfony-1.4 innodb propel

当我尝试运行以下代码时:

    $conBud = Propel::getConnection(MyTestBudgetPeer::DATABASE_NAME); // DATABASE_NAME = 'Budget'
    $conBud->beginTransaction();        
    $conIn = Propel::getConnection(MyTestInvoicePeer::DATABASE_NAME);  // DATABASE_NAME = 'Invoice'
    $conIn->beginTransaction();
    $idcl = '1235';
    try
    {
      // Do db udpates related to database Budget (here around 15 tables and 500 data rows are update)
       // budExModel is a table, primary id from this table is used to update InvoiceTest Table below
       $idtest = $budExModel->save($conBud);
       ...
      // Code to Update one table for database Invoice (only one table)
      // Create a Criteria object that will select the correct rows from the database
        $selectCriteria = new Criteria();
        $selectCriteria->add(InvoiceTestPeer::IDCL, $idcl, Criteria::EQUAL);            
        $selectCriteria->setDbName(InvoiceTestPeer::DATABASE_NAME);
        // Create a Criteria object includes the value you want to set
        $updateCriteria = new Criteria();
        $updateCriteria->add(InvoiceTestPeer::IDTEST, $idtest);
        // Execute the query
        BasePeer::doUpdate($selectCriteria, $updateCriteria, $conIn);

        $conBud->commit();
        $conIn->commit();           
    } catch (Exception $e)
    {
        $conBud->rollBack();
        $conIn->rollBack(); 
    }

我收到错误:["Unable to execute UPDATE statement [UPDATE invoice_test SET IDTEST =:p1 WHERE invoice_test.IDCL=:p2 ] [wrapped: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction]

  

超过锁定等待超时;尝试重新启动交易

我得到的错误是表/ db,它有较少的数据,只有一个表的进程。

这是不允许的mysql?

我已经更改了innodb_lock_wait_timeout并尝试重新启动mysql,因此它们不是一个选项。

Edit: Here IDTEST I am trying to udpate for table invoice_test is an fk from Table Budget_test from database Budget.

1 个答案:

答案 0 :(得分:2)

错误背后的原因似乎是idtest的外键约束。

此处$idtest是表bud_ex中新保存的行的primary_key;这是从last_insert_id中检索到的,这与尝试在incoice_test表中使用的id相同。这里的问题是,我试图使用$ idtest,但是连接/事务没有提交,因此当尝试使用这个id时,它抛出了一个fk约束错误,其中超出了锁定时间。

为了使其工作,我必须运行查询以将发票数据库的外键检查设置为false。

set foreign_key_checks = 0; 

除此之外,我对php代码进行了一些修改,以使try catch块更加具体。

$con1->beginTransaction();
try
{
    // Do stuff
    $con2->beginTransaction();
    try
    {
        // Do stuff
        $con2->commitTransaction();
    }
    catch (Exception $e)
    {
        $con2->rollbackTransaction();
        throw $e;
    }
    try
    {
        $con1->commitTransaction();
    }
    catch (Exception $e)
    {
        // Oops $con2 was already committed, we need to manually revert operations done with $con2
        throw $e;
    }
}
catch (Exception $e)
{
    $con1->rollbackTransaction();
    throw $e;
}