使用場景例如: 有3件商品 10 個人同時去購買容易出現超額出售,
Db::startTrans(); //開啟事務
try{
//lock(true) 意思是加鎖
$num = Db::name(' goods ')->lock(true)->where('goods_id',1)->value('num') // 庫存
if ( $num <= 0){
return;
}
//生成訂單 減少庫存
具體操作就行
}catch( \Exception $e){
}
//測試工具 apache 下面的ab 工具 目錄位置
/lib 打開命令行
ab -n 100 -c 100 http://www.xyh.com/index/index/mysql_unlock
意思是 -n100次請求 每次-c 100個用戶 url地址
詳情地址 https://blog.csdn.net/qq_16887641/article/details/96855254