1:
I tried to do this and it worked fine:
start transaction; select * from orders where id = 21548 LOCK IN SHARE MODE; update orders set amount = 1500 where id = 21548; commit;
According to the definition of LOCK IN SHARE MODE, it locks the table with the IS lock and locks the selected rows with the S lock.
When a row is locked with lock S. How can I change it without releasing the lock? It requires an X lock to change it. Correct? Or is it valid only for different connection transactions?
2:
//session1 start transaction; select * from orders where id = 21548 FOR UPDATE;
Keep this session1 the same and try this in a different session:
//session2 select * from orders where id = 21548; //working update orders set amount = 2000 where id = 21548; //waiting
FOR UPDATE, locks the entire table in mode IX and the selected row in mode X.
Since mode X is incompatible with S-mode, then how is the selection request performed in the second session?
One answer may be that the select request does not request an S-lock, why it works successfully. But in the second session of the update request, the X lock is also not requested, but as it starts, it starts to wait for the lock on session1.
I read a lot of things regarding this, but could not clear my doubts. Please, help.
source share