<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ``` mysql innodb 的共享鎖和排它鎖的講解 https://www.jianshu.com/p/fe708aad6113?tdsourcetag=s_pcqq_aiomsg 原文講解 https://www.cnblogs.com/188-username/articles/12494964.html ``` ``` 先看沒有利用事務的時候并發的后果 創建庫存管理表 ~~~ CREATE TABLE `storage` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `number` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ~~~ 創建訂單管理表 ~~~ CREATE TABLE `order` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `number` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8 ~~~ 測試代碼 ~~~ $pdo = new PDO('mysql:host=127.0.0.1;port=3306; dbname=test','root','123456'); $sql="select `number` from storage where id=1 limit 1"; $res = $pdo->query($sql)->fetch(); $number = $res['number']; if($number>0) { $sql ="insert into `order` VALUES (null,$number)"; $order_id = $pdo->query($sql); if($order_id) { $sql="update storage set `number`=`number`-1 WHERE id=1"; $pdo->query($sql); } } ~~~ 我們預置庫存是十個,然后執行ab測試查看結果 ~~~ mysql> select * from storage -> ; +----+--------+ | id | number | +----+--------+ | 1 | -2 | +----+--------+ 1 row in set (0.00 sec) mysql> select * from `order`; +----+--------+ | id | number | +----+--------+ | 22 | 10 | | 23 | 10 | | 24 | 8 | | 25 | 8 | | 26 | 7 | | 27 | 6 | | 28 | 4 | | 29 | 3 | | 30 | 2 | | 31 | 2 | | 32 | 2 | | 33 | 1 | +----+--------+ 12 rows in set (0.00 sec) ~~~ 得到了訂單共有12個,而庫存表的庫存也減到了-2,這顯然不符合實際邏輯的; 下面我們來看利用數據庫行鎖來解決這個問題 修改代碼如下 ~~~ $pdo = new PDO('mysql:host=127.0.0.1;port=3306; dbname=test','root','123456'); $pdo->beginTransaction();//開啟事務 $sql="select `number` from storage where id=1 for UPDATE ";//利用for update 開啟行鎖 $res = $pdo->query($sql)->fetch(); $number = $res['number']; if($number>0) { $sql ="insert into `order` VALUES (null,$number)"; $order_id = $pdo->query($sql); if($order_id) { $sql="update storage set `number`=`number`-1 WHERE id=1"; if($pdo->query($sql)) { $pdo->commit();//提交事務 } else { $pdo->rollBack();//回滾 } } else { $pdo->rollBack();//回滾 } } ~~~ 查看結果 ~~~ mysql> select * from storage; +----+--------+ | id | number | +----+--------+ | 1 | 0 | +----+------ --+ 1 row in set (0.00 sec) mysql> select * from `order`; +----+--------+ | id | number | +----+--------+ | 1 | 10 | | 2 | 9 | | 3 | 8 | | 4 | 7 | | 5 | 6 | | 6 | 5 | | 7 | 4 | | 8 | 3 | | 9 | 2 | | 10 | 1 | +----+--------+ 10 rows in set (0.00 sec) ~~~ 很明顯在利用了mysql鎖之后,對庫存進行了有效的控制,很好的解決了第一段代碼里面,因為并發引起的一些邏輯性的問題 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看