9
I’ve been reading the question What is a Mysql Transaction for? and I was left with many doubts on this subject.
When we should use transactions?
Why should we use transactions?
How we should use transactions?
Some examples:
In case I’m in product sales website, in PHP and that has stocks, and that they cannot fall from zero what happens or what I can do if: The stock of one product is with a unit and we have two customers trying to buy the product. Starts the transaction in one of them, the other can have access to BD? Or the query SQL throws an exception on the second customer trying to buy the product? It will not cause much embarrassment if there are many customers trying to buy the same product with stock equal to a?
If several users are registering at the same time, does it make sense for each user to have a new transaction? Will each of them have to wait for the other’s transaction to end? Otherwise, there may be conflicts of login’s equal?
It makes sense in these cases to use transactions? Because if I have the comic book prepared not to accept negative values, in the case of stocks and so that the login’s be unique no longer need to use transactions, right?
Transactions would no longer be indicated for dependencies between tables? Such as foreign keys.
But what if you have both (or two million users) buying at the same time and click at the same time to buy?
– Jorge B.
For this purpose the query after the transaction, because if the quantity is less than 0 is because someone bought practically together, but some was even 1 ms was before, so you have to make a check before and after removing the product from stock.
– Leonardo Patricio
This won’t lead to too many comic book hits, and as a result, my site won’t be too slow if it’s used by thousands of people?
– Jorge B.
For you to have an Ecommerce with so much access you will need a very robust server, so that is not what has to give a problem, because an Ecommerce that has 2 million people buying at the same time it has a lot generates a lot of money, so server investment and security is a must.
– Leonardo Patricio
Can you understand? I can help you with something else?
– Leonardo Patricio
But then the fact that it’s a transaction doesn’t matter, you could do it without the transaction, right?
– Jorge B.
No, because for example 2 people at the same time will make an operation to buy the same product and this product has only one in stock, if in the second check the product is with quantity less than 0 then you will have to remove the product from the person’s cart because it is no longer available in stock, because someone for a few fractions of a second bought before it, and to perform the rollback of this situation you will need to use the transaction in order to remove what was changed in the database from the moment the transaction started up to the unavailable stock error. Understands?
– Leonardo Patricio
Leonardo, please try to consolidate all your comments within the answer. The idea is that our content should serve primarily for future visitors, besides those who asked the original question. Check out the guide [Answer].
– brasofilo