What is Locking in MySQL (or any RDBMS) and when would you use it? A Layman explanation with a Example would be great!
We have a joint bank account with a balance of $200
I go to the ATM and put my card into the machine, the machine checks that I have a balance of $200
Meanwhile, you go into the bank and ask for $50, the teller brings up your account and confirms that you have the money.
I request a withdrawal of $200, the machine counts my money gives me $200 and sets my balance at $0
The teller counts your money and gives you the $50, the system then updates the balance on the account as $150 ($200 - $50 withdrawl).
So now we have $250 cash and $150 left in the account. $200 profit.
The database should have used locks to prevent both transactions occuring at the same time.
The problem is if you handle every transaction in that way then we would lose concurrency and performance would suffer, so there are different transaction isolation levels
that are used depending on the scenario, for instance you might not care that someone can modify data that has been read in a transaction.
http://en.wikipedia.org/wiki/Isolation_%28database_systems%29
You should learn these and understand the scenarios where they are applicable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With