MySQL does not have this feature. For those who are looking for this topic in general, some RDBMSs have better / clever locking features than others.
For developers limited to MySQL, the best approach is to add a column (or use an existing state column, for example) that can be set to "locked" or "in progress" or similar, do SELECT ID, * ... WHERE IN_PROGRESS != 1 FOR UPDATE; get the id of the row you want to block, enter UPDATE .. SET IN_PROGRESS = 1 WHERE ID = XX to unlock the records.
Using LOCK IN SHARE MODE almost never a solution, because as long as it allows you to read the old value, but the old value is in the process of updating, so if you are performing a non-atomic task, there is no point in even looking at this record.
Better * RDBMS recognizes this pattern (select one row to work and lock it, work on it, unlock it) and provide a more reasonable approach that allows you to search only unlocked records. For example, PostgreSQL 9.5+ provides SELECT ... SKIP LOCKED , which selects only from an unlocked subset of rows matching the query. This allows you to get an exclusive lock on the line, a service that is recorded until completion, and then update and unlock the corresponding record without blocking other threads / consumers from the opportunity to work independently.
* “Better” here means in terms of atomic updates, multi-user architecture, etc. and not necessarily “better designed” or “better.” Do not try to run a flamvar there.
source share