Table of Contents
回复内容:
表级锁
行级锁
页面锁
Home Backend Development PHP Tutorial mysql - 关于php开发抢购程序涉及到的问题

mysql - 关于php开发抢购程序涉及到的问题

Jun 06, 2016 pm 08:23 PM
mysql php

我没事做了个小程序,主要的功能是用户抢购商品,比如一个商品标价是40元,每个用户出1元,用户抢购记录的总数等于40,那么这个商品就关闭购买。

我有个疑虑,如果用户多并发怎么办呢,如果同一时刻有10个用户同时抢购并付款,怎么保证购买记录表中的购买总数不超过商品总价呢?

如果商品价值40,已经有36个人成功付款并购买,还剩下4个,这是又有10个用户都想抢剩下的4个名额,那就说有6个人是抢不到的,那么怎么控制呢?不让总数溢出···

谢谢各位啦~~

有人说是数据库加锁,不知道什么意思···

回复内容:

我没事做了个小程序,主要的功能是用户抢购商品,比如一个商品标价是40元,每个用户出1元,用户抢购记录的总数等于40,那么这个商品就关闭购买。

我有个疑虑,如果用户多并发怎么办呢,如果同一时刻有10个用户同时抢购并付款,怎么保证购买记录表中的购买总数不超过商品总价呢?

如果商品价值40,已经有36个人成功付款并购买,还剩下4个,这是又有10个用户都想抢剩下的4个名额,那就说有6个人是抢不到的,那么怎么控制呢?不让总数溢出···

谢谢各位啦~~

有人说是数据库加锁,不知道什么意思···

update Item set count=count+1 where id=$id and count=$count;
大致逻辑:
try=0;
while true do
select * from item where id=1;
if $count >=40 then close item; break end
if ++try >10 then break end
update item count=count+1 where id=$id and count=$count;
if update ok then
xxxx
break;
end
end

这种应用场景可以考虑memcache或redis。
mysql高并发下就得使用锁机制了。
InnoDB引擎,该引擎支持行锁,支持事务,外键。
可以使用 for update 来共享锁,结合事务如下。
begin;
select price from good where id=1 for update;//查询当前商品的价格 for update 一定要加上
然后判断是否达到40
没有达到40执行

1

2

3

<code>update good set price=price+1 where id=1

最后(再提交之前 你再开一个mysql客户端 来操作这条数据 你会发现一直在等待锁解除)

commit</code>

Copy after login

达到的话就回滚终止

1

<code>rollback</code>

Copy after login

锁是计算机协调多个进程或线程并发访问某一资源的机制,为了保证数据的一致性和有效性。
锁分为表锁、行锁还有页面锁,高并发的环境我推荐用行锁。

表级锁

开销小,加锁快;不会出现死锁;锁定粒度大,发生锁冲突的概率最高,并发度最低;适合查询操作较多

行级锁

开销大,加锁慢;会出现死锁;锁定粒度最小,发生锁冲突的概率最低,并发度也最高。适合并发更新数据

页面锁

开销和加锁时间介于表锁和行锁之间;会出现死锁;锁定粒度介于表锁和行锁之间,并发度一般。

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

How to create a new connection to mysql in navicat How to create a new connection to mysql in navicat Apr 09, 2025 am 07:21 AM

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

PHP: Is It Dying or Simply Adapting? PHP: Is It Dying or Simply Adapting? Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

See all articles