The introduction of SQL Server database locks the reason

Today, we mainly to introduce the SQL Server database locks, as well as introduction of the reason for introducing SQL Server data locks and lock the distinction between classes of the two methods, the following is a description of the specific content, we hope you will learn in the future help.

I. Why introduce lock

When multiple users simultaneously bring the following data inconsistency issue concurrent operation of the database:

Lost update

A, B and two user reading the same data is modified, wherein the modification results in a destruction of the user of the results of another modification, such as booking system

Dirty read

A user modifies the data, the user B and then reads the data, but for some reason the user A to cancel the modification of data, data restore the original value, then B to give the generated data inconsistent with the data in the database

Non-repeatable read

A user reads the data, and then the user reads the data B and modifying, when inconsistency is found twice before and after re-read data when the user A

Concurrency control is the main method of sealing SQL Server database locks, lock that prevents users over a period of time to do certain operations to avoid data inconsistencies

Two locks of classification

There are two categories of lock points system:

1. From the database point of view of the system:

Into exclusive lock (i.e. exclusive lock), and update the shared locks lock

MS-SQL Server uses the following resource lock mode.

Lock mode Description

Shared (S) is not used to change or update the operation data (read-only operation), such as a SELECT statement.

Update (U) for renewable resources. Prevent a common form of deadlock occurs when multiple sessions are reading, SQL Server database lock and subsequent resource updates when possible.

Exclusive (X) for the data modification operations, such as INSERT, UPDATE or DELETE. The same resource at the same time ensure that no multiple updates.

Intent locks used to establish a lock hierarchy. Intention locks types: intent shared (IS), intent exclusive (IX), and shared with intent exclusive (SIX).

Schema locks used when performing the operation depends on the schema of a table. Architecture lock types: schema modification (Sch-M) and schema stability (Sch-S).

Bulk Update (BU) using the copy data to the table and specify the mass TABLOCK prompt.

Shared lock

Shared (S) locks allow concurrent transactions to read (SELECT) a resource. When the presence of shared (S) lock on a resource, no other transaction can modify data. Once the data has been read, immediately release the shared (S) lock on the resource, unless the transaction isolation level is set to repeatable read or higher, or use within the transaction life cycle locking hint reserve shared (S) lock.

Update lock

Update (U) is generally in the form of locks prevent deadlock. Transaction generally consists of a composition update mode, the transaction record is read, acquiring a shared resource (page or row) (S) of the lock, and then modify the line, which requires the lock conversion to an exclusive (X) latch. If two transactions acquired shared mode lock on the resource, and then trying to update data, a transaction attempts to lock conversion to an exclusive (X) lock. Sharing mode conversion to the exclusive lock must wait a period of time, because the lock other transactions share mode incompatible exclusive lock a transaction; lock wait occurs. The second transaction attempts to acquire an exclusive (X) lock to be updated. Since the two transactions should be converted to an exclusive (X) lock, and each transaction is waiting for the other transaction releases SQL Server database in shared mode lock, and therefore a deadlock occurs.

To avoid this potential deadlock problem, use the update (U) lock. Only one transaction can obtain the resource update (U) lock. If the transaction to modify the resource, the update (U) lock conversion to an exclusive (X) lock. Otherwise, lock into a shared lock.

Exclusive lock

Exclusive (X) lock prevents concurrent transactions access to the resource. No other transaction can modify or read exclusive (X) locks data.

Intent locks

Intent lock indicates that SQL Server requires some underlying resources in the hierarchy acquire a shared (S) lock or exclusive (X) lock. For example, placed in the shared table level intent lock on the intention to indicate that the transaction table shared page or row is placed (S) lock. Is provided at the table level intent lock prevents another transaction then comprises obtaining exclusive on that page table (X) lock. Intent locks improve performance because SQL Server only checks intent lock at the table level to determine if a transaction can safely acquire a lock on the table. Without having to check each row in the table locks on each page or to determine if a transaction can lock the entire table.

Intention locks include intent shared (IS), intent exclusive (IX), and shared with intent exclusive (SIX).

Lock mode Description

Intent shared (IS) by placing the respective S locks on the resource, indicating that the transaction is intended reading portion (but not all) of the underlying resource hierarchy.

Intent exclusive (IX) by placing each of the X lock on the resource, indicating that the intention is to modify the transaction hierarchy portion (but not all) the underlying resources. IX is a superset of IS.

Shared with intent exclusive (the SIX) by placing a lock on each resource IX, it shows that the intention is to read all of the underlying transaction resource hierarchy and modify some (but not all) the underlying resources. Allow concurrent IS locks on the top-level resource. For example, placing a lock table SIX SIX lock (lock allows concurrent IS) in the above table, IX locks placed on the current page being modified (X lock is placed on the line has been modified). Although each resource within a period of time only one SIX lock to prevent other transactions updating of resources, but allows other transactions to read the underlying resource hierarchy by obtaining IS table level lock.

Exclusive lock:

Only allow program-locking operation using any other action against him will not be accepted. When performing a data update command, SQL Server will automatically use an exclusive lock. When other locks exist on the object, not extending its exclusive lock.

Shared locks: shared locks resources can be read by other users, but other users can not modify it, in the implementation of Select, SQL Server will target shared locks.

Update locks:

When ready to update SQL Server data, it first update of data objects as locks, so that the data can not be modified, but you can read. When SQL Server determines the data to be updated until the operation, he will automatically update lock to an exclusive lock change, when there are other locks exist on an object, it can not lock plus updates.

2. From a programmer's point of view: divided into optimistic and pessimistic locking.

Optimistic locking: completely rely on the database to manage the locks work.

Pessimistic lock: SQL Server database lock on the programmer to manage data or object processing.

MS-SQLSERVER performed simultaneously using a lock achieved between the modified user pessimistic concurrency control in a database in a plurality of

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2011/08/02/2125611.html

Guess you like

Origin blog.csdn.net/weixin_33834679/article/details/93494960