dp926460 2012-06-09 16:50
浏览 8
已采纳

由于延迟时间和多个请求,避免双DB查询

I have a MySql database linked to my website, that has a table with a task list (columns are separated by "/" below), i.e.:

task1 / doTask1.sh / todo

task2 / doTask2.sh / done

task3 / doTask3.sh / todo

task4 / doTask4.sh / todo

etc...

On every request to my website, I want the "todo" tasks to be executed and then marked as "done" by the server. The task can only be executed once. But here comes the problem.

What if:

User A selects the tasks that are marked as "todo". doTask1.sh executes and is marked as done, triggered by request from user A. User B selects the tasks that are marked as "todo" (task3 and task3 only, since task1 is done). doTask3.sh executes and is marked as done, triggered by request from user A. doTask3.sh executes again, triggered by request from user B. Same for doTask4.sh.

Can this happen using PHP/Nginx/MySql, due to simultaneous DB connections? If yes, how can this problem be bypassed/avoided?


In this example, I used doTaskx.sh to simplify the situation. In reality, this column would contain a description of the task and the PHP script powering my website would execute a task depending on that description.

  • 写回答

2条回答 默认 最新

  • duanbo5230 2012-06-09 17:04
    关注

    Yes, this can happen.

    One approach for solving this problem is to do the following:

    • Create a "pending" status".
    • When a request comes in to execute a task, run an update on the database setting everything matching the ID and status = "todo" to "pending".
    • If the number of records updated is 1, execute the task and mark it as "done".
    • If the number of records updated is 0, it means another request got there first.
    • Either way, update the user interface to show that the task is pending/done.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路