This article brings you relevant knowledge about shared locks and exclusive locks in mysql. I hope it will be helpful to everyone.
Shared Lock
Shared lock, S lock, read lock , are all his names.
And I like to call him Shared Read Lock.
A shared (S) lock permits the transaction that holds the lock to read.
#A shared lock allows the transaction holding the lock to read.
The sharing here is, Read and read sharing .
That is to say, whether it is row level or table level, if a shared read lock is placed on certain data, Other transactions can continue to read (that is, shared read locks are allowed to be held) , but cannot be written, that is, reading and writing are mutually exclusive.
By the way, let me introduce how to add a shared lock (shared read lock):
Upper table-level shared lock, that is, table-level shared read lock:
select * from table(表) lock in share mode ;
Copy after login
Upstream-level shared lock , that is, row-level shared read lock:
select * from table(表)where id = 10
lock in share mode
;
Copy after login
Let’s be a bit more verbose here. Note that under InnoDB, you don’t just use row locks if you want to use row locks. Let’s review the triggering conditions of row locks again ( Mentioned at the beginning):
Exclusive Lock
Exclusive lock, write lock, X lock , are all his names.
And I like to call him Exclusive write lock.
An exclusive (X) lock permits the transaction that holds the lock to update or delete.
Exclusive (X) locks allow the transaction holding the lock to update or delete.
Exclusive, this word. Have you ever played basketball? I didn’t know how to play basketball in junior high school. I would not pass the ball while I was holding it. My classmates said to me, you are so lonely.
Yes, I am very alone. Just like this exclusive write lock (exclusive lock), it is very unique.
When a transaction adds an exclusive write lock (exclusive lock) to certain data, only the current transaction can modify or delete the data.
Other transactions cannot be read or written. Because this lock is very unique, you must wait until this very unique lock is used up (released) before other transactions can take advantage of it.
So, the exclusive write lock (exclusive lock) is mutually exclusive for reading and writing, and mutually exclusive for writing and writing.
By the way, let me introduce how to add an exclusive lock (exclusive write lock):
Upper table-level exclusive lock, that is, table-level exclusive write lock:
select * from table
for update
;
Copy after login
Upstream-level exclusive lock , that is, row-level exclusive write lock:
select * from table where id =10
for update
;
Copy after login
Let me be a little more verbose here. Note that under InnoDB, you don’t just use row locks if you want to use row locks. We will again trigger the row lock conditions. To review (mentioned at the beginning):
The above sql can achieve an upstream-level exclusive lock because it hits the index, and id is the index.
Perhaps after seeing this, you are still vague about shared locks & exclusive locks. You roughly know what read-read sharing, read-write mutual exclusion, write-write mutual exclusion and so on.
So, we need to look at these two locks from God’s perspective again,
RedTransaction Operation 1
BlueTransaction Operation Two
##Shared lock (shared read lock)
Exclusive lock (exclusive write lock)
Shared lock (shared read lock)
Yes, compatible, read together
No, not compatible, you have to wait if you want to write The shared lock is gone
Exclusive lock (exclusive write lock)
No, it’s incompatible. If the exclusive lock is set, others can’t do anything. Move
Can’t move, it’s incompatible, it’s locked exclusively, no one else can move it
3. So if the exclusive write lock is never released, will other transactions keep waiting?
The same is true, it will wait for the timeout to return the query failure:
Add a little practice:
1. Still the same, first upload a certain data Exclusive write lock, no COMMIT:
2. Execute ordinary query, select:
You can see, Ordinary select statements can be obtained normally, why? Because we mentioned it earlier:
So I have to elaborate again. The so-called read-read sharing, read-write mutual exclusion, and write-write mutual exclusion are all for lock resources. That said, if you don't compete for lock resources, then there must be no mutual exclusion or mutual exclusion.
The above is the detailed content of Examples to explain shared locks and exclusive locks under MySQL and InnoDB. For more information, please follow other related articles on the PHP Chinese website!
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
The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.
The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database
Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.
I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.
Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.
The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.
Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.