Row locks
select amount from tbl_account where customer_id = ‘1’ for update;
Page locks
select amount from tbl_account where customer_id in (‘1′,’2’) for update;
Table locks
select amount from tbl_account for update;
summary
Are essentially write locks (pessimistic locks)
Read lock/shared lock
The database creates a new query to represent the first thread
start transaction; Select amount from tbl_accountwhere customer_id = '1' lock in share mode;
Copy the code
The database creates another query to represent the second thread
Select amount from tBL_account where tBL_account is set to amount to tBL_accountwhere customer_id = '1';
select amount from tbl_account where customer_id = '1' lock inshare mode; Select amount from tbl_accountwhere customer_id = '1' for update;
Copy the code
Once the read lock is released in one thread, the second thread can add the write lock
commit;
Copy the code
Write lock/pessimistic lock
The database creates a new query to represent the first thread
start transaction; Select amount from tbl_accountwhere customer_id = '1' for update;
Copy the code
The database creates another query to represent the second thread
Select amount from tBL_accountwhere customer_id = '1'; Select amount from tbl_accountwhere customer_id = '1' lock in share mode;
select amount from tbl_account where customer_id = '1' for update;
Copy the code
Must wait until the first thread write lock is released
commit;
Copy the code