dongzhan2461 2012-09-10 18:32
浏览 110
已采纳

Zend Framework数据库事务 - 无法回滚

Alright, let me get this out of the way first. My question is similar to this: Cannot rollback transaction in Zend Framework

My tables are always innoDB, always. I've checked the table in question and it is indeed innoDB. On to the problem..

I have a database instance, and model instances running through this transaction hitting the same database:

$db->beginTransaction();

try { 
  // Run an insert
  $model_record->insert(array('single_item' => 'its value'));

  // More logic, and run an update.
  $model_record->this_value = 'that';

  // Save it
  $model_record->save();

  //Commit the transaction
  $db->commit();
} catch (Exception $e) {

  // It finds the rollback, yet does nothing.
  $db->rollBack();  
}

Now, the reason I found this to not be working is I exceeded the character limit on a row during a test to make sure all the logic in place was correct.

It did not rollback. On top of that the record with 'single_item', was in the database. But the updated values, were not.

Am I completely missing something small, I've never had transaction issues with MySQL & innoDB. Could this be MySQL related or ZF related? Any insight is helpful, thanks.

Update:

I've been conducting a few more tests and here are a few results that might help:

    $this->_db->beginTransaction();

    // This works
    $this->_db->insert('table_a', 
        array(
           'a_field' => 'transaction test',
        )
    );

    // This does not work, at all.  It inserts and does not rollback.  There is no commit.
    $_table_a_model->insert(
        array(
            'a_field' => 'transaction test',
        )
    );

    $this->_db->rollback();

Additional Update You need to get the instance of the model, and call a transaction on that.

$the_model = $this->_model->getAdapter();
$the_model->beginTransaction();

This leaves no room for transactions for multiple tables, without doing multiple transactions for each model instance. Any ideas out there without reverting to the base database instance?

  • 写回答

2条回答 默认 最新

  • dongxieyi9115 2012-09-10 21:42
    关注

    I figured it out. I have to use $db = Zend_Db_Table_Abstract::getDefaultAdapter(); and then run my transactions off that so all operations in multiple models work together under a single transaction. If anyone has a fix for that, feel free to comment.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换