Database lock mechanism 1------share lock

一,share lock

What is a shared lock: In short, the lock lock resource can only be read, and can not be added, deleted or changed. Until the lock is released.

example 1:

Request1:select * from tabletest

Request 2:update tabletast set ....

sql statement of database execution request 1, is a select statement, the database will add a shared lock to the table The data of the query is consistent. If the request 1 has not released the shared lock, the sql statement of the database execution request 2 is an update statement, and the database will add an exclusive lock to the table, but the same resource shared lock and exclusive lock are Can not coexist, so request 2 can only wait for the request 1 to complete the release of the shared lock, then the update of request 2 can proceed.

example 2:

Request1:select * from tabletest

Request 2:select * from tabletest

Database execution request 1 sql statement is a select statement, the database will add a shared lock to the table, database execution Request 2 sql statement, the database will also add a shared lock to the table, then request 1 will not block request 2, because the same resource shared lock can be superimposed, shared lock and shared lock compatible.

example 3:

Request1:select * from tabletest

Request 2:select * from tabletest

Request 3:update tabletast set ....

sql execution request 1 sql statement is select Statement, the database will add a shared lock to the table, the database executes the SQL statement of request 2, the database will also add a shared lock to the table, request 3 update statement, only wait for request 1, the shared lock of request 2 is released The request sql statement will be executed.