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
1. Configure local yum source 1. Mount the ISO im...
Application Scenario Taking the background manage...
This article example shares the specific code for...
MySQL itself does not support recursive syntax, b...
background Since I was assigned to a new project ...
Method 1: Adding values Let's go to MDN to se...
1. Check the currently installed PHP packages yum...
nbsp   no-break space = non-breaking spa...
One-click execution To install Python 3.8 in a vi...
There are three ways to create an image: creating...
When learning about inline-block, I found that the...
Table of contents 1. Why use slots? 1.1 slot 1.2 ...
There are two ways: 1. Service method Check the f...
Table of contents Preface 1. The significance of ...
1. Download and unzip to: /Users/xiechunping/Soft...