简体   繁体   English

如果我的 lockForUpdate 返回一个空集,我是否需要回滚/提交?

[英]Do I need to rollback/commit if my lockForUpdate returned an empty set?

My code:我的代码:

$connection->beginTransaction();

$row = $this->newQuery()->where(['someField' => 'someValue'])->lockForUpdate()->get()->first();

if (is_null($row)) {
    $connection->rollBack();

    return null;
}
          
//do some stuff with that row

$connection->commit();

My question: The result is empty, so do I need that rollback?我的问题:结果是空的,所以我需要回滚吗? Should I commit?我应该承诺吗? Is transaction still opened by that point?到那时交易是否仍然打开?

This is a bug https://bugs.php.net/bug.php?id=76742 in php where deadlock errors were ignored when PDO::ATTR_EMULATE_PREPARES is set to false. This is a bug https://bugs.php.net/bug.php?id=76742 in php where deadlock errors were ignored when PDO::ATTR_EMULATE_PREPARES is set to false. This is now fixed in 7.4.13.这现在在 7.4.13 中得到修复。

Setting PDO::ATTR_EMULATE_PREPARES to true fixes this, ie an error is now thrown when lock wait timeout expires instead of returning an empty result set.将 PDO::ATTR_EMULATE_PREPARES 设置为 true 可以解决此问题,即现在在锁定等待超时到期而不是返回空结果集时会引发错误。

However note that setting PDO::ATTR_EMULATE_PREPARES to true returns all numbers as strings.但是请注意,将 PDO::ATTR_EMULATE_PREPARES 设置为 true 会将所有数字作为字符串返回。

You can set this at runtime using.您可以在运行时使用。

$connetion->setAttribute(PDO::ATTR_EMULATE_PREPARES,true);

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

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