Home >Database >Mysql Tutorial >How to implement pessimistic locking in mysql?
Mysql pessimistic lock method: 1. First use [select ... for update] to lock, and after the operation is completed, use commit to release the lock; then the innodb engine defaults to the row-level lock; finally no data can be found , just don’t lock the table.
mysql pessimistic lock method:
1. Start transaction
begin;/begin work;/start transaction; (三者选一就可以)
2. Query Output product information
select status from table where id=1 for update;
3. Generate order based on product information
insert into table111 (id,goods_id) values (null,1);
4. Modify product status to 2
update table set status=2 where id=1;
5. Submit transaction
commit;/commit work; (任选一种释放锁)
Summary:
1. Implement pessimistic locking using select... for update to lock, and use commit to release the lock after the operation is completed;
2. Innodb engine, by default Row-level locking, when there is a clear field, a row will be locked, if there is no query condition or condition;
When the field is unclear, the entire table will be locked, and when the condition is a range, the entire table will be locked;
3. When no data can be found, the table will not be locked.
Related learning recommendations: mysql video tutorial
The above is the detailed content of How to implement pessimistic locking in mysql?. For more information, please follow other related articles on the PHP Chinese website!