Can mysql shared lock and exclusive lock coexist?

start transaction;
select * from test where id = 8 for share;
update test set name = "grayvtouch" where id = 8;
select * from test where id = 8 for share;

at first I thought it was a shared lock , but as a result, I did get stuck by executing the following statement in another session:

select * from test where id = 8 for share;

this means that after the above transaction is executed (uncommitted), the lock finally added to the id = 8 record is the exclusive lock .

excuse me, is this the actual situation? Why is there such a phenomenon?

Jan.14,2022

buddy, your update has not been submitted, and the row lock on id=8 will not be released. So from the perspective of other transactions, you still hold the X lock on id=8 even though you add for share, after it.

Menu