This article uses six points to describe SQL lock usage overview. If you need to know about mssql locks, you can refer to this article for details.
This article uses six points to describe SQL lock usage overview. If you need to know about mssql locks, you can refer to this article for details.
I. Why should I introduce locks?
When multiple users simultaneously perform concurrent operations, the following data inconsistency may occur:
Update loss
A and B read and modify the same data. The Modification result of one user destroys the Modification result of another user, such as the ticket booking system.
Dirty read
User A modified the data, and user B then read the data again. However, user A canceled the data modification and restored the original value for some reason, the data obtained by B is inconsistent with the data in the database.
Non-repeated read
User A reads the data, and user B then reads the data and modifies the data. At this time, user A reads the data again and finds that the values of the first and second times are inconsistent.
The main method of concurrency control is blocking. The lock is to prohibit users from performing some operations within a period of time to avoid data inconsistency.
Binary lock Classification
There are two lock types:
1. From the perspective of the database system: exclusive locks (exclusive locks), shared locks, and update locks
Ms SQL Server uses the following resource lock modes.
Lock mode description
Share (S) is used for operations without changing or updating data (read-only operations), such as SELECT statements.
Update (U) is used in updatable resources. It prevents common deadlocks when multiple sessions are reading, locking, and subsequent resource updates.
Arrange it (X) for data modification operations, such as INSERT, UPDATE, or DELETE. Make sure that multiple updates are not performed for the same resource at the same time.
Intention locks are used to establish a lock hierarchy. The intention lock type IS: Intention sharing (IS), intention ranking (IX), and intention ranking sharing (SIX ).
The schema lock is used to perform operations dependent on the table schema. The schema lock types are: schema modification (Sch-M) and schema stability (Sch-S ).
Large-capacity Update (BU) is used to copy data to a table in large capacity and specify the TABLOCK prompt.
Shared lock
The share (S) Lock allows concurrent transactions to read (SELECT) a resource. When a shared (S) lock exists on the resource, no other transactions can modify the data. Once the data has been read, the shared (S) lock on the resource is released immediately, unless the transaction isolation level is set to repeated read or higher, or use the lock prompt to keep the share (S) Lock within the transaction lifecycle.
Update lock
Update (U) locks can prevent normal deadlocks. Generally, the update mode is composed of a transaction. The transaction reads the record, obtains the share (S) lock of the resource (page or row), and then modifies the row, this operation requires that the lock be converted to an exclusive (X) Lock. If two transactions obtain the Shared Mode Lock on the resource and attempt to update the data at the same time, a transaction attempts to convert the lock to the lock (X. The conversion from the sharing mode to the exclusive lock must wait for a while, because the exclusive lock of a transaction is incompatible with the Sharing Mode Lock of other transactions; a lock wait occurs. The second transaction attempts to obtain the row lock (X) for update. Because both transactions need to be converted to the exclusive (X) lock, and each transaction waits for another transaction to release the share mode lock, a deadlock occurs.
To avoid this potential deadlock problem, use the update (U) Lock. Only one transaction can obtain the resource Update (U) Lock at a time. If the transaction modifies the resource, the update (U) Lock is converted to the row (X) Lock. Otherwise, the lock is converted to a shared lock.
Exclusive lock
Locking (X) prevents concurrent transactions from accessing resources. Other transactions cannot read or modify the data locked by the lock (X.
Intention lock
The intention lock indicates that SQL Server needs to obtain the share (S) lock or arrange it (X) Lock on some underlying resources in the hierarchy. For example, a table-level share intention lock indicates that the transaction intends to place the share (S) lock on the page or row of the table. Setting the intention lock at the table level can prevent another transaction from getting the row lock (X) on the table containing that page. Intention locks can improve performance, because SQL Server only checks intention locks at the table level to determine whether transactions can safely obtain the locks on the table. Instead of checking the locks on each row or page in the table to determine whether the transaction can lock the entire table.
Intention locks include intention sharing (IS), intention arranging it (IX), and intention sharing (SIX ).
Lock mode
Description
By placing the S lock on each resource, intention sharing (IS) indicates that the transaction intends to read some (not all) of the underlying resources in the hierarchy.
By placing the X lock on each resource, the intention of the transaction is to modify some (rather than all) underlying resources in the hierarchy. Ix is the superset of IS. By placing an IX lock on each resource, SIX shares with the intention to indicate that the transaction intends to read all the underlying resources in the hierarchy and modify some (rather than all) of the underlying resources. Allow concurrent IS locks on top-level resources. For example, the table's SIX lock places a SIX lock on the table (the concurrency IS allowed), and the IX lock on the current modification page (the X lock on the modified row ). Although each resource can have only one SIX lock for a period of time, to prevent other transactions from updating resources, however, other transactions can read the underlying resources in the hierarchy by obtaining the table-level IS lock.
Exclusive lock: only the lock operation is allowed by the program. Other operations on the lock operation will not be accepted. When the data update command is executed, SQL Server automatically uses the exclusive lock. An exclusive lock cannot be applied to an object when other locks exist.
Shared lock: the shared lock can be read by other users, but other users cannot modify it. When executing Select, SQL Server will apply a shared lock to the object.
Update lock: When SQL Server is preparing to update data, it first locks the data object so that the data cannot be modified but can be read. When SQL Server determines that it wants to update data, it will automatically replace the update lock with an exclusive lock. When other locks exist on the object, it cannot be updated.
2. From the programmer's perspective: Optimistic locks and pessimistic locks.
Optimistic lock: it relies entirely on the database to manage the lock.
Pessimistic lock: the programmer manages the lock processing on data or objects by himself.
Ms SQL Server uses locks to implement pessimistic concurrency control among users who execute modifications simultaneously in the database
Granularity of three locks
The lock granularity is the size of the target to be blocked. If the lock granularity is small, the concurrency is high, but the overhead is large. If the lock granularity is large, the concurrency is low, but the overhead is small.
The lock granularity supported by SQL Server can be divided into rows, pages, keys, key ranges, indexes, tables, or databases to obtain locks.
Resource Description
RID row identifier. Used to lock a row in a table.
The row lock in the key index. Used to protect the key range in a serializable transaction.
Page 8 KB data page or index page.
A group of eight adjacent data pages or index pages in the extended Disk Area.
The entire table includes all data and indexes.
DB database.
4. Length of lock time
The lock hold duration is the time required to protect resources at the requested level.
The duration of the shared lock used to protect read operations depends on the transaction isolation level. When the default transaction isolation level of read committed is adopted, the shared lock is only controlled during page reading. During the scan, the lock is released only when the lock is obtained on the next page within the scan. If you specify the HOLDLOCK prompt or set the transaction isolation level to repeatable read or SERIALIZABLE, the lock will not be released until the transaction ends.
Based on the concurrency options set for the cursor, the cursor can obtain the scroll lock in the sharing mode to protect the extraction. The scroll lock is released only when the cursor is extracted or closed for the next time (whichever comes first. However, if HOLDLOCK is specified, the rolling lock is released until the transaction ends.
The exclusive lock used to protect updates will not be released until the transaction ends.
If a connection attempts to obtain a lock and the lock conflicts with the lock controlled by another connection, the connection attempting to obtain the lock will be blocked:
Release the conflicting lock and the connection obtains the requested lock.
The connection timeout interval has expired. By default, there is no timeout interval, but some applications set a timeout interval to prevent indefinite waiting.
5. Custom locks in SQL Server
1. Handle deadlocks and set deadlock priority
A deadlock means that multiple users apply for different locks, SET DEADLOCK_PRIORITY can be used to control the session response mode in the case of a deadlock. If both processes lock data and wait until other processes release their own locks, each process can release its own locks, that is, deadlock occurs.
2. Process timeout and set the lock timeout duration.
@ LOCK_TIMEOUT returns the current lock timeout settings for the current session, in milliseconds
SET LOCK_TIMEOUT allows the application to SET the maximum time for the statement to wait for resource blocking. When the waiting time of a statement is greater than the LOCK_TIMEOUT setting, the system automatically cancels the blocking statement and Returns Error 1222 to the application that "exceeds the lock request timeout period ".
Example
In the following example, the lock timeout period is set to 1,800 milliseconds.
SET LOCK_TIMEOUT 1800
3) set the transaction isolation level.
4) use the table-level locking prompt for SELECT, INSERT, UPDATE, and DELETE statements.
5) configure the index lock Granularity
You can use the sp_indexoption system stored procedure to set the locking granularity for indexes.
6. View lock Information 1. Execute EXEC SP_LOCK to report lock information.
2. Press Ctrl + 2 in the query analyzer to view the lock information.
Seven precautions
How to avoid deadlocks
1. When using a transaction, try to shorten the logical processing process of the transaction and commit or roll back the transaction as soon as possible;
2. Set the deadlock timeout parameter to a reasonable range, for example, 3 minutes to 10 minutes. If the timeout period is exceeded, the operation is automatically abandoned to avoid process suspension;
3. Optimize the program to check and avoid deadlock;
4. All scripts and SP should be carefully tested before the version is correct.
5. All SP should have error handling (via @ error)
6. do not modify the default transaction level of SQL Server. Force lock not recommended
Eight lock problems
1. How to lock a row in a table
Set transaction isolation level read uncommittedselect * FROM table rowlock where id =
1
2. Lock a table in the database
SELECT * FROM table WITH (HOLDLOCK)
Lock statement:
Sybase: update TABLE set col1 = col1 where 1 = 0; ms SQL: col1 from TABLE (tablockx) where 1 = 0; oracle: LOCK TABLE IN EXCLUSIVE MODE;
No one else can operate after the lock, until the locked user is unlocked and unlocked with commit or rollback
The following examples help you better understand table 1 (A, B, C)
A B C
A1 b1 c1
A2 b2 c2
A3 b3 c3
1) exclusive lock
Create two new connections and execute the following statement in the first connection
Begin tranupdate table1set A = 'A' where B = 'b2' waitfor delay' 00: 00: 30'
-- Wait 30 seconds for commit tran
Execute the following statement in the second connection
Begin transelect * from table1where B = 'b2' commit tran
If the preceding two statements are executed at the same time, the select query must wait 30 seconds until the update statement is executed.
2) shared lock
Execute the following statement in the first connection:
Begin transelect * from table1 holdlock-holdlock artificially locking where B = 'b2' waitfor delay' 00: 00: 30'
-- Wait 30 seconds for commit tran
Execute the following statement in the second connection
Begin transelect A, C from table1where B = 'b2' update table1set A = 'A' where B = 'b2' commit tran
If the preceding two statements are executed at the same time, the select query in the second connection can be executed, and the update statement can only be executed 30 seconds after the first transaction releases the shared lock and converts it to the exclusive lock.
3) deadlock
Add table2 (D, E)
D E
D1 e1
D2 e2
Execute the following statement in the first connection:
Begin tranupdate table1set A = 'A' where B = 'b2' waitfor delay' 00: 00: 30 'Update table2set D = 'd5 'where E = 'e1' commit tran
Execute the following statement in the second connection
Begin tranupdate table2set D = 'd5 'where E = 'e1 'waitfor delay' 00: 00: 10' update table1set A = 'A' where B = 'b2' commit tran
At the same time, the system detects a deadlock and terminates the process.
Add:
Prompt for table-level locking supported by SQL Server 2000
HOLDLOCK holds the shared lock until the entire transaction is completed. It should be released immediately when the locked object is not needed, equal to the SERIALIZABLE transaction isolation level.
The NOLOCK statement does not issue a shared lock when it is executed. Dirty reads are allowed, which is equal to the read uncommitted transaction isolation level.
PAGLOCK uses multiple page locks when a table lock is used
READPAST allows the SQL Server to skip any locked rows and execute transactions. This applies to the READ UNCOMMITTED transaction isolation level, which only skips the RID lock and does not skip pages, regions, and table locks.
ROWLOCK force row lock
TABLOCKX forces the use of an exclusive table lock, which prevents any other transactions from using this table during the transaction.
UPLOCK forces the use of updates during table reading without sharing locks
Application lock:
The application lock is the lock generated by the client code, not the lock generated by the SQL Server itself.
Two processes for processing application locks
Sp_getapplock: Lock application resources
Sp_releaseapplock unlock application resources
Note: What is the difference between locking a database table?
SELECT * FROM table WITH (HOLDLOCK) other transactions can read the table, but cannot update or delete the table.
SELECT * FROM table WITH (TABLOCKX) other transactions cannot read, update, and delete tables.