SQL SERVER SELECT statement locking detailed description of a shared lock (read lock) and exclusive lock (write lock) options

Shared lock (read lock) and exclusive lock (write lock)

 

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

If the transaction data T to A plus shared lock, other transactions can only share plus A lock can not add exclusive lock. Granted a shared lock transaction can only read data, the data can not be modified.

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

If the transaction data T to A plus exclusive lock, other transactions can not be of any type A plus any blockade. Both approved the transaction row data read his locks, but also modify the data.

Data inconsistency when we operate the database, may be due to concurrency issues arising (data collision)

Optimistic locking
Optimistic locking is not a built-in database, we need to realize themselves. Optimistic locking means that when the operation of the database (update), the idea is very optimistic that this operation will not lead to conflict, when working with data, not any other special treatment (that is, not locked), while making after the update, go to determine whether there is a conflict.

Typically this is implemented: when the data in the table to operate (update), the data table plus give a version (Version) field, every time the operation, the piece of recording the version number is incremented. That is to check out the piece of history, get a version field, if you want to operate (updated) for that record, the first version of the judgment value at the moment is equal to the value of the version of the time just check out, if they are equal, then the during this period, no other programs to manipulate it, you can perform the update, the version field's value plus 1; the value at the moment of discovery version version of the value is not equal to just get out if the update, then have this period there are other programs that manipulate, and update operation is not performed.

For example:

3 comprises a single step of operation:

1. check out product information

select (status,status,version) from t_goods where id=#{id}

2. generate orders based on product information

3. Modify the commodity status of 2

update t_goods 

set status=2,version=version+1

where id=#{id} and version=#{version};

 

In addition to manually implement optimistic locking, there is now a lot of online framework is a good package to achieve optimistic locking, such as hibernate, when required, may own search "hiberate optimistic lock" give it a try.


Pessimistic lock
and optimistic lock is pessimistic corresponding lock. Pessimistic locking is when working with data that this data conflicts occur, it must operate on the same data by obtaining the lock can during each operation, this is very similar to java in synchronized, so pessimistic locks need spend more time. In addition to the corresponding lock optimism, pessimistic locking is achieved by the database itself, use the time, we directly call the related statements database on it.

Here, relates pessimistic locking to lock the other two concepts came out, they are shared locks and exclusive locks. Shared locks and exclusive locks are different implementations pessimistic locking, pessimistic locking Talia belong to the scope of.

 

Shared lock
shared lock refers to a number of different transactions, sharing the same lock on the same resource. Equivalent for the same door, the same as it has more keys. Like this, there is a door to your house, there are several keys to the door, you have one, you have a girlfriend, you are likely to enter your home through this key, go lovemaking session getting better, understand what Kazakhstan, Yes, this is the so-called shared lock.
Just say, pessimistic locking, the general database has been achieved, a shared lock also belong to a pessimistic lock, then shared lock in mysql through what command to call it. By querying the information learned by executing a statement in the back plus lock in share mode on behalf of certain resources coupled with a shared lock.
For example, I opened here by two mysql query editor in which to open a transaction does not perform commit statements
city Table DDL as follows:
the CREATE TABLE `city` (
` id` bigint (20) the NOT NULL AUTO_INCREMENT,
`name` VARCHAR (255) the DEFAULT NULL,
`state` VARCHAR (255) the DEFAULT NULL,
a PRIMARY KEY (` id`)
) = 18 is the AUTO_INCREMENT the InnoDB ENGINE = the DEFAULT the CHARSET = UTF8;

 

begin;
SELECT * from city where id = "1"  lock in share mode;

Then another query window for id to update the data 1

 

update city set name = "666" where id = "1";
this case, the interface enters state Caton, after a few seconds, and an error message
[SQL] update city set name = "666" where id = " 1 ";
[Err] 1205 - Lock the wait timeout exceeded Number of; the try Restarting Transaction

So to prove, for the record id = 1 lock succeeded, before the record has not commit, this record id = 1 is locked, and only in a transaction to be operated after releasing the lock off, or use shared lock to operate on this data.
And then experiment a bit:

 

update city set name="666" where id ="1" lock in share mode;
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lock in share mode' at line 1


After adding a shared lock, also an error message, and query the information to know, to update, insert, delete statements will automatically add rows reason it locks

So, I tried SELECT * from city where id = "1" lock in share mode;

 

This time success.

 


Exclusive lock
exclusive lock and a shared lock correspondence, refers to a number of different transactions, for the same resource can only have a lock.
And sharing lock types, after the statement to be executed can be a plus for update


Row lock
row lock, understand the literal meaning, it is to add a row lock, which is a record plus lock.

For example, before the presentation of sharing the lock statement

SELECT * from city where id = "1"  lock in share mode; 

Since for city table, id primary key field, it also corresponds to the index. When performing lock, this index will id record 1 plus the lock, the lock is a row lock.

 

Table lock
table lock and lock the corresponding row, add to the table lock.

MyISAM engine in there, being studied

-----------------------------------------------------------------------
When sql queries will add S lock, unlock after inquiry, S will release the lock. But will form clots, that depends on your select statement has not been completed, how long depends on the efficiency of blocking your sql statement
 
Depending on your isolation level, the default is plus S lock.
select a table lock is held by
more clog, you can rely on these indexes, transactions, and other adjustments lock granularity to minimize blockage of the time
----------------------------------------------------------------------
SQL Server provides a powerful and complete lock mechanism to help achieve concurrency and high-performance database system. Users can not only use the default settings SQL Server can also use the effect of "locking options" to achieve expected in the select statement. This article describes the SELECT statement in the "lock Options" and the corresponding function description.   

Function Description:    

NOLOCK (unlocked)   

When this option is selected, SQL Server without any locks when reading or modifying data. In this case, the user is likely to read data transaction is not completed (Uncommited Transaction) or rolled back (Roll Back), i.e. so-called "dirty."   

HOLDLOCK (keep the lock)   

When this option is selected, SQL Server shared lock will remain until the end of this entire transaction, without releasing the way.      

When UPDLOCK (modified lock) this option is selected, SQL Server uses modified lock when reading data instead of sharing the lock, and this lock is held until the end of the entire transaction or command. Use this option to ensure that multiple processes can simultaneously read the data but the process can only modify the data.   

TABLOCK (table lock) When this option is selected, SQL Server will set shared locks until the end of the command on the entire table. This option ensures that other processes can read but can not modify the data.      

PAGLOCK (page locks) This is the default option, when selected, SQL Server uses a shared page locks.      

 
  TABLOCKX (exclusive table lock) When this option is selected, SQL Server will set an exclusive lock on the entire table until the end of the order or transaction. This will prevent other processes to read or modify data in the table.     
 
  These options will allow the system to ignore transaction isolation level (Transaction Isolation Level) previously set in the SET statement. Please refer to SQL Server online manual for more information.

 

Example:

select * from tablename where ... with(nolock)

select * from tablename where ... with(holdlock)

The difference between a locking database tables: select * from tablename with (rowlock) where id = 3 Note

SELECT * FROM table WITH (HOLDLOCK) other transactions can read the table, but you can not delete update

SELECT * FROM other transactions table WITH (TABLOCKX) can not read the table, update, and delete

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

If the transaction data T to A plus shared lock, other transactions can only share plus A lock can not add exclusive lock. Granted a shared lock transaction can only read data, the data can not be modified.

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

If the transaction data T to A plus exclusive lock, other transactions can not be of any type A plus any blockade. Both approved the transaction row data read his locks, but also modify the data.

Data inconsistency when we operate the database, may be due to concurrency issues arising (data collision)

Optimistic locking
Optimistic locking is not a built-in database, we need to realize themselves. Optimistic locking means that when the operation of the database (update), the idea is very optimistic that this operation will not lead to conflict, when working with data, not any other special treatment (that is, not locked), while making after the update, go to determine whether there is a conflict.

Typically this is implemented: when the data in the table to operate (update), the data table plus give a version (Version) field, every time the operation, the piece of recording the version number is incremented. That is to check out the piece of history, get a version field, if you want to operate (updated) for that record, the first version of the judgment value at the moment is equal to the value of the version of the time just check out, if they are equal, then the during this period, no other programs to manipulate it, you can perform the update, the version field's value plus 1; the value at the moment of discovery version version of the value is not equal to just get out if the update, then have this period there are other programs that manipulate, and update operation is not performed.

For example:

3 comprises a single step of operation:

1. check out product information

select (status,status,version) from t_goods where id=#{id}

2. generate orders based on product information

3. Modify the commodity status of 2

update t_goods 

set status=2,version=version+1

where id=#{id} and version=#{version};

 

In addition to manually implement optimistic locking, there is now a lot of online framework is a good package to achieve optimistic locking, such as hibernate, when required, may own search "hiberate optimistic lock" give it a try.


Pessimistic lock
and optimistic lock is pessimistic corresponding lock. Pessimistic locking is when working with data that this data conflicts occur, it must operate on the same data by obtaining the lock can during each operation, this is very similar to java in synchronized, so pessimistic locks need spend more time. In addition to the corresponding lock optimism, pessimistic locking is achieved by the database itself, use the time, we directly call the related statements database on it.

Here, relates pessimistic locking to lock the other two concepts came out, they are shared locks and exclusive locks. Shared locks and exclusive locks are different implementations pessimistic locking, pessimistic locking Talia belong to the scope of.

 

Shared lock
shared lock refers to a number of different transactions, sharing the same lock on the same resource. Equivalent for the same door, the same as it has more keys. Like this, there is a door to your house, there are several keys to the door, you have one, you have a girlfriend, you are likely to enter your home through this key, go lovemaking session getting better, understand what Kazakhstan, Yes, this is the so-called shared lock.
Just say, pessimistic locking, the general database has been achieved, a shared lock also belong to a pessimistic lock, then shared lock in mysql through what command to call it. By querying the information learned by executing a statement in the back plus lock in share mode on behalf of certain resources coupled with a shared lock.
For example, I opened here by two mysql query editor in which to open a transaction does not perform commit statements
city Table DDL as follows:
the CREATE TABLE `city` (
` id` bigint (20) the NOT NULL AUTO_INCREMENT,
`name` VARCHAR (255) the DEFAULT NULL,
`state` VARCHAR (255) the DEFAULT NULL,
a PRIMARY KEY (` id`)
) = 18 is the AUTO_INCREMENT the InnoDB ENGINE = the DEFAULT the CHARSET = UTF8;

 

begin;
SELECT * from city where id = "1"  lock in share mode;

Then another query window for id to update the data 1

 

update city set name = "666" where id = "1";
this case, the interface enters state Caton, after a few seconds, and an error message
[SQL] update city set name = "666" where id = " 1 ";
[Err] 1205 - Lock the wait timeout exceeded Number of; the try Restarting Transaction

So to prove, for the record id = 1 lock succeeded, before the record has not commit, this record id = 1 is locked, and only in a transaction to be operated after releasing the lock off, or use shared lock to operate on this data.
And then experiment a bit:

 

update city set name="666" where id ="1" lock in share mode;
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lock in share mode' at line 1


After adding a shared lock, also an error message, and query the information to know, to update, insert, delete statements will automatically add rows reason it locks

So, I tried SELECT * from city where id = "1" lock in share mode;

 

This time success.

 


Exclusive lock
exclusive lock and a shared lock correspondence, refers to a number of different transactions, for the same resource can only have a lock.
And sharing lock types, after the statement to be executed can be a plus for update


Row lock
row lock, understand the literal meaning, it is to add a row lock, which is a record plus lock.

For example, before the presentation of sharing the lock statement

SELECT * from city where id = "1"  lock in share mode; 

Since for city table, id primary key field, it also corresponds to the index. When performing lock, this index will id record 1 plus the lock, the lock is a row lock.

 

Table lock
table lock and lock the corresponding row, add to the table lock.

MyISAM engine in there, being studied

Guess you like

Origin www.cnblogs.com/zhoading/p/11730446.html