How to make properly transaction and/or lock tables in MySQL for e-shop in PHP?
Get the solution ↓↓↓I would like to use transactions and/or locking tables in my e-shop, mainly to avoid order-id duplication (it is not only autoincrement ID).
Now I have this scenario:
1) cart submitted by user
2) check if all informations are correcly filled
3) lock tables with products, orders and discount cupons
4) begin transaction
-- try { --
5) update products stocks
6) mark discount cupon as used
7) get order-id (it is based on number of orders today)
8) save order
-- } catch { --
9a) rollback transaction
10a) unlock tables
-- } --
9b) commit transaction
10b) unlock tables
11) send mail and show thankfull message to user
Q1: I am wondering if this scenario is "correct" or there is a better way?
Q2: Do I need to lock tables when I use transactions? Beside not getting order-id duplicated, I would like to affect users and admins of e-shop as little as possible. I decided to use transaction because of making data consistent - for example, when products stocks are updated but saving order fail, it would be nice to rollback previous stocks numbers.
I appreciate all comments and recommendations guys.
Answer
Solution:
Per comments, remove table locks, other wise fine.
You should pretty much never use explicit table locks. There may be some edge cases in provisioning scripts when its needed, but otherwise best if you forget they exist.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: target class [commandmakecommand] does not exist.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.