简体   繁体   English

在MySQL InnoDB中取消事务,而不是提交或回滚

[英]cancel a transaction in MySQL InnoDB instead of commit or rollback

I am using a transaction in a MySQL InnoDB database to perform 2 inserts. 我在MySQL InnoDB数据库中使用事务来执行2次插入。 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 ? 有没有一种好的方法来“取消”事务,而不是使用commitrollback

For example, in php, I am doing the following: 例如,在php中,我正在执行以下操作:

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

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 . 相反,您需要执行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. 如果连接对象超出范围时有未提交的事务,或者如果连接对象已关闭,则该事务将自动回滚。

In other words, it's alright if there is nothing to rollback, because underneath the hood, the DB driver will handle this case gracefully. 换句话说,没有任何回滚可以,因为在幕后,DB驱动程序将优雅地处理这种情况。

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

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