dqz7636 2012-12-05 00:13
浏览 50
已采纳

线程安全是PHP 5.4中的一个真正问题吗?

Is it possible, in my PHP 5.4.0 application (IIS, FastCGI, Non thread safe) for two people to update the same table in the database by running the same code at exactly the same time and inadvertently mess up each other's data?

The reason I ask is because I see the occasional unexplained data glitch, and in the most recent case I found that another customer updated the same table at exactly the same time.

And part two of my question is if this is indeed happening, how do I prevent it?

  • 写回答

2条回答 默认 最新

  • donglangtun1850 2012-12-05 00:54
    关注

    They can't "mess up each others data" because of non-thread safe PHP, no, unless you're readying/writing Apache settings (e.g. with SetLocale) or you've programmed it to update shared information simultanously (e.g. flat files, as Amadam says).

    Most normal processes such as MySQL, reading GET parameters etc will not be affected.

    So unless your problem is with locales, it'll be your code, not the thread settings.

    If it's with SetLocale, then transactions or other methods won't make any difference. Anyhting else you can program round.


    You can mess up data if you've not programmed for concurrent actions - this can happen in thread safe and non-thread safe. Remember that even in "thread safe" you can have concurrent threads being processed with different speed and orders.

    Here is a dangerous example:

    • Statement a) Read a table to get next update value
    • Statement b) Write to table using previous value
    • Statement c) Update "next update" table for next user.

    The statements could be processed by "User 1" running all, followed by "User 2" (ideal, and how you programmed it). But equally so, "User 1" runs "a" and "b", followed by "User 2" running all, then "User 1" running "c" - in this case, "User 2" will overwrite what "User 1" wrote.

    (To repeat, this is NOTHING to do with "non-thread safe" in PHP.)

    How to get around this latter issue:

    1. Transactions MAY help you; they won't actually help with the example above unless you use the "WITH CONSISTENT SNAPSHOT" option as all they do is delay the commit, and you've read the value too early in statement "a".
    2. Table locks allow you to prevent users reading or writing to a table, so in the example above, lock the "update" table first, run the transations then release the lock. This forces the second user to wait until "User 1" has completed the lot before it reads the number.
    3. Use the capabilities of mySQL including "AUTO INCREMENT" primary keys, or codes such as "INSERT INTO... ON DUPLICATE KEY" or "REPLACE".

    The third option is the best, if you can. Table locks can get messy and transactions my not fix your issues.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改