Home > Database > Mysql Tutorial > How to implement pessimistic locking in mysql?

How to implement pessimistic locking in mysql?

coldplay.xixi
Release: 2020-06-30 17:21:37
Original
6401 people have browsed it

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.

How to implement pessimistic locking in mysql?

mysql pessimistic lock method:

1. Start transaction

begin;/begin work;/start transaction; (三者选一就可以)
Copy after login

2. Query Output product information

select status from table where id=1 for update;
Copy after login

3. Generate order based on product information

insert into table111 (id,goods_id) values (null,1);
Copy after login

4. Modify product status to 2

update table set status=2 where id=1;
Copy after login

5. Submit transaction

commit;/commit work; (任选一种释放锁)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template