LOCK IN SHARE MODE
Read rows with a shared lock. This clause ensures rows remain unchanged by other transactions while allowing other sessions to read them.
InnoDB supports row-level locking. Selected rows can be locked using LOCK IN SHARE MODE or FOR UPDATE. In both cases, a lock is acquired on the rows read by the query, and it will be released when the current transaction is committed.
When LOCK IN SHARE MODE is specified in a SELECT statement, MariaDB will wait until all transactions that have modified the rows are committed. Then, a write lock is acquired. All transactions can read the rows, but if they want to modify them, they have to wait until your transaction is committed.
InnoDB locks index records rather than logical rows. If a SELECT is satisfied by a secondary index (a covering index read), it just locks that index. Instead, the primary key records will be locked if a concurrent UPDATE modifies columns outside of that index. These locks do not overlap under READ COMMITTED isolation level, so the update can proceed. To ensure a block is used, the SELECT query must reference columns from the primary index.
For further information and an example, see InnoDB Lock Modes.
If autocommit is set to 1 (the default), the LOCK IN SHARE MODE and FOR UPDATE clauses have no effect in InnoDB. For non-transactional storage engines like MyISAM and ARIA, a table level lock will be taken even if autocommit is set to 1.
See Also
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

