In relational databases, pessimistic locking and optimistic locking are solutions to resource concurrency scenarios. Next, we will explain in detail the actual use and advantages and disadvantages of these two concurrency solutions. First, define the database and make a simple inventory table as follows: CREATE TABLE `order_stock` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', `oid` int(50) NOT NULL COMMENT 'Product ID', `quantity` int(20) NOT NULL COMMENT 'Inventory', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; Quantity represents the inventory of different commodity oids. Next, OCC and PCC use this database for demonstration. Optimistic Locking (OCC) It assumes that multi-user concurrent transactions will not affect each other during processing, and each transaction can process the part of the data affected by it without generating locks. Before committing data updates, each transaction will first check whether other transactions have modified the data after reading it. If other transactions have updates, the transaction being committed will be rolled back. That is, "optimistic locking 🔒" believes that the user who takes the lock will most likely succeed, so it is enough to take the lock at the last step of the business operation that actually requires updating the data. This can avoid using the row locks defined by the database itself and avoid deadlocks. UPDATE order_stock SET quantity = quantity - 1 WHERE oid = 1 AND quantity - 1 > 0; Optimistic concurrency control is mostly used in environments with little data contention and conflicts. In such environments, the cost of occasionally rolling back a transaction is lower than the cost of locking data when reading it, so higher throughput can be achieved than other concurrency control methods. Pessimistic locking PCC It prevents a transaction from modifying data in a way that affects other users. If a transaction performs an operation that reads a row of data and applies a lock, other transactions can only perform operations that conflict with the lock when the transaction releases the lock. This design adopts the "one lock 🔒 two queries 🔍 three updates" mode, which uses BEGIN SELECT quantity FROM order_stock WHERE oid = 1 FOR UPDATE; UPDATE order_stock SET quantity = 2 WHERE oid = 1; COMMIT; Another problem with MySQL is that all scanned rows will be locked during the execution of the select ... for update statement, which can easily cause problems. Therefore, if you use pessimistic locking in MySQL, make sure to use the index instead of a full table scan. Pessimistic concurrency control is mainly used in environments with intense data contention and in environments where the cost of using locks to protect data when concurrency conflicts occur is lower than the cost of rolling back transactions. Advantages and disadvantages of OCC and PCC OCC Advantages and Disadvantages 【advantage】
【shortcoming】
PCC Advantages and Disadvantages 【advantage】 The conservative strategy of "lock first and then access" ensures the security of data processing; 【shortcoming】
The above is the details of pessimistic locking and optimistic locking in MySQL. For more information about MySQL pessimistic locking and optimistic locking, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to Check Memory Usage in Linux
Anyone in need can refer to it. If you have tried...
The custom encapsulation code of the vue button c...
A data volume container is a container specifical...
1. Download the latest nginx docker image $ docke...
Load balancing is a commonly used device in serve...
Both methods can be used to execute a piece of ja...
This article example shares the specific code for...
The current environment is: Centos 7.5 docker-ce ...
Due to the company's business requirements, t...
Table of contents The CSS custom variable functio...
This article shares the installation and configur...
I would like to share the installation and config...
This article shares the specific code for WeChat ...
Table of contents 1. What is an event? 2. Enable ...
1: Statement order of grouping function 1 SELECT ...