drp935159 2013-06-24 13:36
浏览 42
已采纳

澄清PHP PDO选择更新事务

I am new to using transactions.

Basically, I am able to successfully lock a single row in the MySQL console using START TRANSACTION followed by a SELECT ... FOR UPDATE statement. The row remains locked until I run the COMMIT command in the console

However, when I do the same in a PHP script using $db->beginTransaction() followed by $db->execute("SELECT ... FOR UPDATE"), the row does not get locked before I run a $db->commit() script.

What I mean is I can still successfully do a SELECT ... FOR UPDATE statement in the console even before I run a commit script in PHP to end the transaction. Does this mean my PHP script is not successfully locking the row during the transaction? How am I able to check?

  • 写回答

1条回答 默认 最新

  • doumi4676 2013-06-24 16:35
    关注

    PHP respectively PDO automatically closes and rolls back open transactions after the script has ended, and for very good reasons - generally, you do not want transactions staying open after the PHP parsing ends. See here: http://php.net/manual/en/pdo.transactions.php

    When the script ends or when a connection is about to be closed, if you have an outstanding transaction, PDO will automatically roll it back. This is a safety measure to help avoid inconsistency in the cases where the script terminates unexpectedly--if you didn't explicitly commit the transaction, then it is assumed that something went awry, so the rollback is performed for the safety of your data.

    To test, you might add a sleep(120) after your execute, start the PHP script, switch to the MySQL console and issue statements there. I think you will experience the expected lock.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备