How PESSIMISTIC_WRITE
Works With UPDATE
/INSERT
And DELETE
Operations
Description: This application is an example of triggering UPDATE
, INSERT
and DELETE
operations in the context of PESSIMISTIC_WRITE
locking against MySQL. While UPDATE
and DELETE
are blocked until the exclusive lock is released, INSERT
depends on the transaction isolation level. Typically, even with exclusive locks, inserts are possible (e.g., in PostgreSQL). In MySQL, for the default isolation level, REPEATABLE READ
, inserts are prevented against a range of locked entries, but, if we switch to READ_COMMITTED
, then MySQL acts as PostgreSQL as well.
Key points:
- start Transaction A and trigger a
SELECT
withPESSIMISTIC_WRITE
to acquire an exclusive lock - start a concurrent Transaction B that triggers an
UPDATE
,INSERT
orDELETE
on the rows locked by Transaction A - in case of
UPDATE
,DELETE
andINSERT
+REPEATABLE_READ
, Transaction B is blocked until it timeouts or Transaction A releases the exclusive lock - in case of
INSERT
+READ_COMMITTED
, Transaction B can insert in the range of rows locked by Transaction A even if Transaction A is holding an exclusive lock on this range