Link to home
Start Free TrialLog in
Avatar of manajit
manajitFlag for India

asked on

How do I lock a MySQL table so that other process can not read the table.

Hi,

I am using PHP Laravel. I am storing the count of items in a table.  When a customer purchases an item, we first look at the count of items available, then there are some other related DB operations that are done and there is a logic to reduce the item count and store it back.

However, if 2 customers purchase the item at the same time, there is a race condition and the count gets reduced only by 1. There is a lot of other backend logic and DB operations that happen before the value is updated in the table.

How can I manage the race condition either at the DB level, so that the table is locked for reading till the previous transaction is completed or at the code level, the code is considered as a critical section and locked till the complete code for one purchase goes through.

Help will be appreciated.
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
abandoned question