Home  >  Article  >  Backend Development  >  PHP+MYSQL怎么防止并发导致库存被超卖的情况

PHP+MYSQL怎么防止并发导致库存被超卖的情况

WBOY
WBOYOriginal
2016-06-13 12:19:522620browse

PHP+MYSQL如何防止并发导致库存被超卖的情况。
网站遇到如团购、秒杀、特价之类活动的时候,然库存是有限的,目前 情况是,如果某商品只有一个库存了,此时有两名用户同时下单,则库存会变成了-1,也就说明两名用户都买了,可我只有一个库存,怎么解决这个问题?
------解决思路----------------------
数据库锁,select for update。。
用缓存控制库存。。每次decrement。。。下单没付款的后面再重新设置回去
------解决思路----------------------
1.事物处理
一般情况 ,事务处理都可以解决,我 这边用的基本上都是这个


2.每减少库存对应一个订单流水号(这个订单流水号在生成减少库存这个用户动作时就要生成),订单流水号是存在自增ID 的表里的,然后统计这个流水,就好解决了吧!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn