I have nothing to do a small program, the main function is the user snapped up goods, such as a commodity price is 40 yuan, each user out of 1 yuan, the user snapped record total equals 40, then this commodity is closed to purchase.
I have a doubt, if the user is more concurrent how to do, if there are 10 users at the same time snapping and payment, how to ensure that the purchase record in the total number of purchases does not exceed the total price of the goods?
If the value of goods 40, already have 36 people successfully pay and buy, there are still 4, this is 10 users want to rob the remaining 4 places, that said there are 6 people can not be robbed, then how to control it? Don't let the total overflow ...
Thank you, everyone.
Some people say the database lock, do not know what meaning ...
Reply content:
I have nothing to do a small program, the main function is the user snapped up goods, such as a commodity price is 40 yuan, each user out of 1 yuan, the user snapped record total equals 40, then this commodity is closed to purchase.
I have a doubt, if the user is more concurrent how to do, if there are 10 users at the same time snapping and payment, how to ensure that the purchase record in the total number of purchases does not exceed the total price of the goods?
If the value of goods 40, already have 36 people successfully pay and buy, there are still 4, this is 10 users want to rob the remaining 4 places, that said there are 6 people can not be robbed, then how to control it? Don't let the total overflow ...
Thank you, everyone.
Some people say the database lock, do not know what meaning ...
Update Item set count=count+1 where id= $id and count= $count;
Approximate logic:
try=0;
While True does
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
This scenario can consider memcache or Redis.
The lock mechanism is used for MySQL high concurrency.
InnoDB engine, the engine supports row locks, supports transactions, foreign keys.
You can use the for update to share a lock, combined with the following transaction.
Begin
Select prices from good where id=1 for update;//Query The price of the current product for update be sure to add
And then determine if it reaches 40.
Did not reach 40 execution
update good set price=price+1 where id=1最后(再提交之前 你再开一个mysql客户端 来操作这条数据 你会发现一直在等待锁解除)commit
When reached, the rollback terminates
rollback
A lock is a mechanism by which a computer coordinates multiple processes or threads concurrently accessing a resource in order to ensure consistency and validity of the data.
Lock is divided into table locks, row locks and page locks, high Concurrency environment I recommend a row lock.
Table-Level Locks
Small overhead, fast lock, no deadlock, lock granularity, lock collision is the highest probability, the concurrency is the lowest; more suitable for query operation
Row-level Locks
The cost is big, locking is slow; there is a deadlock, the lock granularity is minimal, the probability of lock conflict is the lowest, and the concurrency is the highest. Suitable for concurrent update data
Page lock
Overhead and lock times are between table and row locks, deadlocks occur, locking granularity is between table and row locks, and concurrency is common.