So I'm working in an environment with high concurrency. What I want to do is select a row from the mysql database based on a certain property and then once I have that row, I want to set a "locked" flag in the row so I can do some work in php (which could take some time) and then unlock it when I'm done. What is the best way to do this in order to prevent concurrency problems that arise when having a separate SELECT and UPDATE command?
The main way we can avoid such concurrency issues and build reliable code is to work with immutable objects. This is because their state cannot be modified by the interference of multiple threads. However, we can't always work with immutable objects.
Exclusive Lock (X): For example, if a transaction wants to update some information about a particular stock the database will let them read by placing an exclusive lock. Therefore, when the second transaction wants to read or write, the exclusive lock prevents this operation.
DBMS in Simple Steps These are: Binary Locks - These can only be in one of two states, locked or unlocked. Shared/Exclusive Locks - Shared locks are acquired when only read operation is to be performed. Shared locks can be shared between multiple transactions as there is no data being altered.
there will be occasions where two users will both read the same data into memory. User 1 will update the data and write those changes back to the database before user 2 does the same thing. Now you have a concurrency control conflict because user 1 read the data before user 2 wrote it back to the database.
using the select for update command : http://dev.mysql.com/doc/refman/5.1/en/innodb-locking-reads.html
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