Examples of using MySQL pessimistic locking and optimistic locking

Examples of using MySQL pessimistic locking and optimistic locking

Pessimistic Lock

Pessimistic lock, considers the data to be pessimistic. When we query the data, we add a lock. Prevent other threads from tampering until they get the lock.

For example, there is the following table. status=1 means that the order can be placed, and status=2 means that the order cannot be placed. If two users check status = 1 at the same time during the concurrent process, then logically both users can add new orders, but this will cause overselling of the product.

The following example

CREATE TABLE `goods` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) DEFAULT NULL,
 `status` tinyint(4) DEFAULT NULL,
 `version` int(11) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4
INSERT INTO demo.goods (id, name, status, version) VALUES (1, 'test', 1, 1);

session1 execution

set autocommit=0;
begin;
select *
from goods where id=1 and goods.status=1 for update;
update goods set status=2 where id=1;

session2 execution

begin;
select * from goods where id=1 for update;

At this time, session 2 is blocked because the lock is still in session 1, so the lock is always waiting. If session1 is not submitted, session2 will time out and disconnect after a certain period of time, and report

(1205, 'Lock wait timeout exceeded; try restarting transaction') error,

The specific lock waiting time can be controlled by setting the innodb_lock_wait_timeout parameter.

If the commit operation is executed in session1 at this time, session2 will obtain the query results and the lock will be given to session2.

We can also

show status like 'innodb_row_lock_%';

To further view the lock information.

Optimistic Locking

Optimistic locking is different from pessimistic locking. Optimistic locking is implemented through its own program rather than mySql itself.

Optimistic locking does not lock the query, and only checks the version number when updating.

For example, if we query the goods table and find that version is 1, then when updating this table, SQL will be

select * from goods where id=1;
update goods set status=2,version=version+1 where id=1 and version=1;

The version here is the version number at the time of query, and each change will result in version+1. If the version numbers do not match the update will not be successful.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Examples of optimistic locking and pessimistic locking in MySQL
  • Understanding and application analysis of mysql pessimistic locking and optimistic locking
  • Comprehensive analysis of optimistic locking, pessimistic locking and MVCC in MySQL
  • Pessimistic locking and optimistic locking in MySQL
  • MySQL pessimistic locking and optimistic locking implementation

<<:  Detailed explanation of zabbix executing scripts or instructions on remote hosts

>>:  Detailed examples of Zabbix remote command execution

Recommend

A brief discussion on Python's function knowledge

Table of contents Two major categories of functio...

SQL method for calculating timestamp difference

SQL method for calculating timestamp difference O...

Briefly talk about mysql left join inner join

Preface I have been busy developing a cold chain ...

40 fonts recommended for famous website logos

Do you know what fonts are used in the logo desig...

Vue implements an Input component that gets the key display shortcut key effect

I encountered a requirement to customize shortcut...

Implementation code of front-end HTML skin changing function

50 lines of code to change 5 skin colors, includi...

JavaScript to implement login slider verification

This article example shares the specific code of ...

How to design a web page? How to create a web page?

When it comes to understanding web design, many p...

Summary of common docker commands

Docker installation 1. Requirements: Linux kernel...

Vue ElementUI implements asynchronous loading tree

This article example shares the specific code of ...

Vue advanced usage tutorial dynamic components

Table of contents Basic description AST parsing R...

HTML Marquee character fragment scrolling

The following are its properties: direction Set th...