the role and usage for update

A, for update definitions

for update is a row level locking, called exclusive lock, once the user applies the row level locking for a row, then the user can query can also update the data line is locked, other users can only query but not update data rows are locked. If the user wants to update the other data row in the table, it is also necessary to apply the table row level locking. Even if multiple users are using a table to share updates, but does not allow the two services at the same time a table is updated, the real update on the table, the table is exclusively locked until the transaction date submitted or rehabilitation. Line lock is always locked exclusively.

Only when one of the following conditions occur, will release the shared update lock:
1, commit (COMMIT) statement
2, exit the database (LOG OFF)
3, the program stops running

 

Second, the concept and usage

Normally, select statement is not locking the data, handicapping other DML and DDL operations. Meanwhile, with the support of multi-version read consistency mechanism, select statements will not be hindered by other types of statements.

The select ... for update statement is that we often use manual lock statement. In the database to perform select ... for update, you will find tables in a database or some data row lock table in mysql, if the query with a primary key, lock rows of data will, if not, it will lock the table.

 Because InnoDB default is Row-Level Lock, so only "clear" in the specified primary key, MySQL will perform Row lock (lock only data cases to be selected), or else MySQL will perform Table Lock (to lock the entire data form live).

For example: Suppose there are tables user, there are two id and name, id is the primary key.

 

Example 1: (explicitly specify the primary key, and the real data, row lock)

SELECT  *  FROM  users  WHERE id = 3  FOR  UPDATE ; 

SELECT  *  FROM  users  WHERE id = 3  and especially = ' Tom '  FOR  UPDATE ;

 

Example 2: (primary key explicitly specified, but there is no data, no lock)

SELECT  *  FROM  users  WHERE id = 0  FOR  UPDATE ;

 

Example 3: (primary key is not clear, table lock)

SELECT  *  FROM  users  WHERE id <> 3  FOR  UPDATE ; 

SELECT  *  FROM  users  WHERE id LIKE  ' % 3% '  FOR  UPDATE ;

 

Example 4: (no primary key, table lock)

SELECT * FROM user WHERE name='Tom' FOR UPDATE;

 

Note:
1, the FOR UPDATE only applies to InnoDB, and must take effect at the transaction processing module (BEGIN / COMMIT) in.

2, to test the condition of the lock, you can use the MySQL Command Mode (Command mode), open two windows to do the test.

3, Myisam supports only table-level locking, InnerDB added support for row-level locking (row-level locking / table-level lock) lock data can not be re-locked by other transactions, nor been modified by another transaction. It is a table-level locking, regardless of whether or not to record the query will lock the table.

 

Third, when you need to use for update?

Aid for update statement, we can achieve the level of data protection operations manual locking applications. That is, those who need the business level data exclusivity, consider using for update.

When on the scene, such as ticket booking, displayed on the screen have the votes, but the votes were genuine, the need to redefine what this data has not been modified by other clients. Therefore, in this confirmation process can be used for update.

 

Four, for update pessimistic locking

Pessimistic locks: always assume the worst case, every time the data are considered to get others will modify, so every time she took the data will be locked, so people want to take this data will be blocked until it is unlocked. Traditional relational database inside to use a lot of this locking mechanism, such as row locks, table locks, etc., read lock, write lock, are locked before doing the first operation. Like for update, another example of realization of the synchronized keyword Java synchronization primitives which are also pessimistic locking.

Optimistic locking: As the name suggests, is very optimistic, pick up data every time when they are that others will not be modified, so it will not be locked, but when the update will determine what others during this time did not go to update the data, you can use mechanism version number. Optimistic locking is suitable for the types of applications to read, which can improve throughput, like similar write_condition mechanisms provided by the database, in fact, optimistic locking is provided.

Guess you like

Origin www.cnblogs.com/banma/p/11797560.html