简体   繁体   English

MySQL InnoDB事务回滚不起作用

[英]MySQL InnoDB transaction rollback is not working

I had a problem with a transaction sent from Yii framework (using PDO) to InnoDB MySql database, the problem was: rollback was not working, update statement was committing right away. 我从Yii框架(使用PDO)发送到InnoDB MySql数据库的事务有问题,问题是:回滚不起作用,更新语句立即提交。

  • I tried setting autoCommit explicitly, with no luck. 我尝试显式设置autoCommit,但是没有运气。
  • Checked Mysql general log, only one Connection is being open, with one Start transaction sent, and later only one rollback. 检查了Mysql常规日志,只有一个Connection正在打开,发送了一个Start事务,后来又只有一个回滚。
  • In the general log, "Start transaction", "rollback" and the update statement were all from the same client thread. 在常规日志中,“开始事务”,“回滚”和更新语句均来自同一客户端线程。
  • No commit was found in the whole log. 在整个日志中未找到任何提交。
  • Tried a rollback example from a mysql client (MySQLWorkbench) and rollback worked! 尝试了从mysql客户端(MySQLWorkbench)的回滚示例,并且回滚有效!

The problem was just that MySql was committing right away when its sent from the Yii app and I didn't know why. 问题是MySql从Yii应用程序发送时马上就提交了,我不知道为什么。

For no particular reason, I just tried enabling log_bin in my.cnf and the rollback worked!! 出于某种原因,我只是尝试在my.cnf中启用log_bin,并且回滚有效!

Can someone please explain what just happened? 有人可以解释一下发生了什么吗?

I'm using MySql 5.6.25, with PHP 5.6.10 and Yii 1.1.14. 我正在使用MySql 5.6.25,PHP 5.6.10和Yii 1.1.14。

Update: 更新:

Turns out that I have made a mistake, I thought the rollback worked after enabling log_bin, but it didn't. 原来我犯了一个错误,我认为启用log_bin后回滚有效,但没有成功。

So now I'm back to the original problem, the rollback is not working, and here is the source code: 所以现在我回到了原始问题,回滚不起作用,这是源代码:

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

    $data = array();
    // fill some data here..
    $model = Model::createOrUpdate($data);

    $errors = $model->getErrors();

} catch (Exception $e) {
    $errors []= $e->getMessage();
}

if (empty($errors)) {
    $msg = 'Success message!';
    $transaction->commit();
    echo CJSON::encode(array('success', $msg));
} else {
    if ($transaction->active) {
        $transaction->rollback();
    }
    echo CJSON::encode(array('error', implode(', ', $errors)));
}

Yii::app()->end();

After some debugging, I found the reason behind that. 经过一些调试,我找到了背后的原因。 Basically the method: 基本上方法是:

Model::createOrUpdate($data);

is calling a stored procedure within, so if anyone is having this issue, please check if that's covered. 正在内部调用存储过程,因此,如果有人遇到此问题,请检查是否已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM