取消MySQL InnoDB中的事务,而不是提交或回滚 [英] cancel a transaction in MySQL InnoDB instead of commit or rollback

查看:232
本文介绍了取消MySQL InnoDB中的事务,而不是提交或回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MySQL InnoDB数据库中使用事务执行2次插入.但是,如果第一次插入失败,我想简单地取消"交易.有没有一种好的方法来取消"交易,而不是使用commitrollback?

I am using a transaction in a MySQL InnoDB database to perform 2 inserts. However, if the first insert fails, I would like to simply "cancel" the transaction. Is there a good approach to "cancel" the transaction rather than using commit or rollback?

例如,在php中,我正在执行以下操作:

For example, in php, I am doing the following:

$connection->beginTransaction();
$affectedRows = $tableOne->insertIgnore('example data');
if ($affectedRows == 0) {
    $connection->rollback();
} else {
    $tableTwo->insert('more example data');
    $connection->commit();
}

如您所见,我正在使用rollback取消交易,但这是用词不当,因为实际上没有任何回滚的内容.

As you can see, I am using rollback to cancel the transaction, but this is a misnomer because there actually is nothing to rollback.

推荐答案

实际上没有取消交易的概念.相反,您需要执行rollback.另外,您也什么也不能做.如果连接对象超出范围时有未提交的事务,或者如果连接对象已关闭,则该事务将自动回滚.

There is really no concept of canceling a transaction. Instead, you need to do a rollback. Alternatively, you can also do nothing. If the connection object has an uncommitted transaction when it goes out of scope, or it is otherwise closed, the transaction will automatically be rolled back.

换句话说,没有什么可回滚的没关系,因为在后台,DB驱动程序将优雅地处理这种情况.

In other words, it's alright if there is nothing to rollback, because underneath the hood, the DB driver will handle this case gracefully.

这篇关于取消MySQL InnoDB中的事务,而不是提交或回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆