<bdo id='9AgBP'></bdo><ul id='9AgBP'></ul>
      <legend id='9AgBP'><style id='9AgBP'><dir id='9AgBP'><q id='9AgBP'></q></dir></style></legend>
    1. <tfoot id='9AgBP'></tfoot>
    2. <small id='9AgBP'></small><noframes id='9AgBP'>

      <i id='9AgBP'><tr id='9AgBP'><dt id='9AgBP'><q id='9AgBP'><span id='9AgBP'><b id='9AgBP'><form id='9AgBP'><ins id='9AgBP'></ins><ul id='9AgBP'></ul><sub id='9AgBP'></sub></form><legend id='9AgBP'></legend><bdo id='9AgBP'><pre id='9AgBP'><center id='9AgBP'></center></pre></bdo></b><th id='9AgBP'></th></span></q></dt></tr></i><div id='9AgBP'><tfoot id='9AgBP'></tfoot><dl id='9AgBP'><fieldset id='9AgBP'></fieldset></dl></div>

      PHP/MySQL - 如何防止两个请求 *Update

      时间:2023-10-10
      1. <i id='M05bI'><tr id='M05bI'><dt id='M05bI'><q id='M05bI'><span id='M05bI'><b id='M05bI'><form id='M05bI'><ins id='M05bI'></ins><ul id='M05bI'></ul><sub id='M05bI'></sub></form><legend id='M05bI'></legend><bdo id='M05bI'><pre id='M05bI'><center id='M05bI'></center></pre></bdo></b><th id='M05bI'></th></span></q></dt></tr></i><div id='M05bI'><tfoot id='M05bI'></tfoot><dl id='M05bI'><fieldset id='M05bI'></fieldset></dl></div>
            <tbody id='M05bI'></tbody>

          <small id='M05bI'></small><noframes id='M05bI'>

            <bdo id='M05bI'></bdo><ul id='M05bI'></ul>

            <legend id='M05bI'><style id='M05bI'><dir id='M05bI'><q id='M05bI'></q></dir></style></legend>
          • <tfoot id='M05bI'></tfoot>

              • 本文介绍了PHP/MySQL - 如何防止两个请求 *Update的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个问题...例如:用户会用他的美元买东西

                I have some question ... example: a user will buy something for his USD

                1. 检查他的美元余额
                2. 从他的账户中扣除美元
                3. 下订单 -> 订单队列
                4. 用户得到他的物品,另一个得到他的美元

                假设用户在同一秒内发出 5 个请求(非常快).所以有可能(并且发生)有 5 个请求正在运行.他只有从 1 个请求购买的钱.现在的要求太快了,脚本会检查他的余额,但不是太快,以至于它扣除了他账户里的钱.所以请求会通过两次!如何解决?

                Lets say, the users makes 5 requests in the same second (very fast). So it is possible (and happen) that 5 requests are running. He has only money to buy only from 1 request. Now the requests are so fast, that the script checks his balance, but is not so fast, that it deduct the money from his account. So the requests will pass two times! How to solve it?

                我在启动进程之前在mysql中使用了LOCK:

                I use LOCK in mysql before I start the process:

                1. IS_FREE_LOCK - 如果没有,请检查此用户是否有锁 -> 2.
                2. GET_LOCK - 设置锁
                3. 下订单/交易
                4. RELEASE_LOCK - 释放锁

                但这并没有真正起作用.还有别的办法吗?

                But this does not really work. Is there another way?

                function lock($id) {
                  mysql_query("SELECT GET_LOCK('$id', 60) AS 'GetLock'");
                }
                
                function is_free($id) {
                  $query = mysql_query("SELECT IS_FREE_LOCK('$id') AS 'free'");
                  $row = mysql_fetch_assoc($query);
                  if($row['free']) {
                    return true;
                  } else {
                    return false;
                  }
                }
                
                function release_lock($id) {
                  mysql_query("SELECT RELEASE_LOCK('$id')");
                }
                
                function account_balance($id) {
                  $stmt = $db->prepare("SELECT USD FROM bitcoin_user_n WHERE id = ?");
                  $stmt->execute(array($id));
                  $row = $stmt->fetch(PDO::FETCH_ASSOC);
                
                  return $row['USD'];
                }
                
                if(is_free(get_user_id())) {
                  lock(get_user_id());
                  if(account_balance(get_user_id()) < str2num($_POST['amount'])) {
                    echo "error, not enough money";
                  } else {
                    $stmt = $db->prepare("UPDATE user SET USD = USD - ? WHERE id = ?");
                    $stmt->execute(array(str2num($_POST['amount']), get_user_id()));
                    $stmt = $db->prepare("INSERT INTO offer (user_id, type, price, amount) VALUES (?, ?, ?, ?)");
                    $stmt->execute(array(get_user_id(), 2, str2num($_POST['amount']), 0));
                }
                

                更新使用 SELECT ... FOR UPDATE 测试事务功能

                Update Tested the transaction function with SELECT ... FOR UPDATE

                $db->beginTransaction();
                $stmt = $db->prepare("SELECT value, id2 FROM test WHERE id = ? FOR UPDATE");
                $stmt->execute(array(1));
                $row = $stmt->fetch(PDO::FETCH_ASSOC);
                if($row['value'] > 1) {
                  sleep(5);
                  $stmt = $db->prepare('UPDATE test SET value = value - 5 WHERE id = 1');
                  $stmt->execute();
                  $stmt = $db->prepare('UPDATE test SET value = value + 5 WHERE id = 2');
                  $stmt->execute();
                  echo "did have enough money";
                } else {
                  echo "no money";
                }
                $db->commit();
                

                推荐答案

                首先,您必须使用事务,但这还不够.在您的交易中,您可以使用 SELECT FOR UPDATE.

                First off, you have to use transactions, but that's not enough. In your transaction, you can use SELECT FOR UPDATE.

                它基本上是在说,我要更新我选择的记录",所以它设置了与 UPDATE 设置的相同的锁.但请记住,这必须在关闭自动提交的事务中发生.

                It's basically saying, "I'm going to update the records I'm selecting", so it's setting the same locks that an UPDATE would set. But remember this has to happen inside a transaction with autocommit turned off.

                这篇关于PHP/MySQL - 如何防止两个请求 *Update的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:PHP PDO 事务? 下一篇:MySQL 提交和事务

                相关文章

                <tfoot id='9jNZ2'></tfoot>
              • <i id='9jNZ2'><tr id='9jNZ2'><dt id='9jNZ2'><q id='9jNZ2'><span id='9jNZ2'><b id='9jNZ2'><form id='9jNZ2'><ins id='9jNZ2'></ins><ul id='9jNZ2'></ul><sub id='9jNZ2'></sub></form><legend id='9jNZ2'></legend><bdo id='9jNZ2'><pre id='9jNZ2'><center id='9jNZ2'></center></pre></bdo></b><th id='9jNZ2'></th></span></q></dt></tr></i><div id='9jNZ2'><tfoot id='9jNZ2'></tfoot><dl id='9jNZ2'><fieldset id='9jNZ2'></fieldset></dl></div>
              • <small id='9jNZ2'></small><noframes id='9jNZ2'>

                    • <bdo id='9jNZ2'></bdo><ul id='9jNZ2'></ul>

                    <legend id='9jNZ2'><style id='9jNZ2'><dir id='9jNZ2'><q id='9jNZ2'></q></dir></style></legend>