Shared locks and exclusive database locks

Following switched   murphy_gb  blog

Shared lock, and later called a read lock, obtain a shared lock, you can view but not modify, and delete data.

Exclusive lock, also known as a write lock, exclusive lock, access to exclusive lock after, both to read data, but also to modify the data.

Why lock

Many people know that the lock is used to solve concurrency problems, then what is the concurrency problem? Concurrent case, unlocked what is the problem?

Take the life of the child to the bathroom, for example, each will have a restroom door, and can be locked, when we will enter the bathroom door locked, then when we came out to open the lock.

When the door was locked, others can only wait at the door. The reason to have the bathroom door, is to protect privacy, avoid multiple people at the same time to enter the bathroom appear.

This database lock is actually the same, in order to avoid simultaneous operation of multiple transaction databases cause data anomalies, usually resolved by the lock mechanism.

Before introducing shared locks and exclusive locks, let's take a big example, already use the example of a toilet, then continue the example. Under normal circumstances, we may have to enter the bathroom to do some things: wash your hands, make-up, the toilet, the toilet in fact, as long as it is extremely private, but other things are not so private.

We can think of the toilet is a database table, while the interior of toilet facilities is the data in a database table. Each of us wants to enter the bathroom is a transaction, simple hand washing, make-up and other operations can be considered a read operation, and the toilet operation can be considered a write operation.

Shared lock

Simple analogy introduced earlier relationship between the database and the bathroom, then the next continue to analyze what is shared lock.

Sometimes, if we go to the bathroom just want to wash their hands, we generally do not lock the door. While others can also come in hand washing and make-up. However, other people are not supposed to come to the toilet.

This is a shared lock, also called a read lock. When what we read operation on the data, in fact, does not change the value of the data.

So we can add to the database read locks, get a read lock on the transaction can read the data. When the database has been increased when other people read lock, other new transaction data can be read, but not write.

In other words, 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.

usage

In the back of increased query LOCK IN SHARE MODE, Mysql will query results in each row are shared locks.

SELECT ... LOCK IN SHARE MODE;

When no other thread line query result set using the exclusive lock, shared lock can be successfully applied, otherwise it will be blocked. Other threads can also be read using a shared lock table, and read these threads is the same version of the data.

Exclusive lock

After introduced over a shared lock, it said in a mutex.

If we just want to wash their hands into the toilet, then we can allow other people to come wash your hands. However, if we go to the bathroom on the toilet, then anyone can not come to do anything.

This is an exclusive lock, also called a write lock. When the transaction is our data write operation, must first obtain a write lock, write lock can be obtained either write data can also be read data. However, if the database has been increased exclusive write locks others, then subsequent transactions can not obtain any locks in the database.

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

usage

In the back of increased query FOR UPDATE, Mysql query will result in each row plus exclusive lock

SELECT ... FOR UPDATE;

When no other thread line query result set using the exclusive lock, it can be successfully applied for an exclusive lock, otherwise it will be blocked.

Lock principle

Take the MySql InnoDB engine, for the insert, update, deleteand other operations. Will be automatically added to the data involved in exclusive lock;

For general selectstatement, InnoDB will not add any locks, the transaction can be displayed to add a shared lock or exclusive lock in the following statement.

Shared locks:SELECT ... LOCK IN SHARE MODE;

Exclusive lock:SELECT ... FOR UPDATE;

Shared lock, and later called a read lock, obtain a shared lock, you can view but not modify, and delete data.

Exclusive lock, also known as a write lock, exclusive lock, access to exclusive lock after, both to read data, but also to modify the data.

Why lock

Many people know that the lock is used to solve concurrency problems, then what is the concurrency problem? Concurrent case, unlocked what is the problem?

Take the life of the child to the bathroom, for example, each will have a restroom door, and can be locked, when we will enter the bathroom door locked, then when we came out to open the lock.

When the door was locked, others can only wait at the door. The reason to have the bathroom door, is to protect privacy, avoid multiple people at the same time to enter the bathroom appear.

This database lock is actually the same, in order to avoid simultaneous operation of multiple transaction databases cause data anomalies, usually resolved by the lock mechanism.

Before introducing shared locks and exclusive locks, let's take a big example, already use the example of a toilet, then continue the example. Under normal circumstances, we may have to enter the bathroom to do some things: wash your hands, make-up, the toilet, the toilet in fact, as long as it is extremely private, but other things are not so private.

We can think of the toilet is a database table, while the interior of toilet facilities is the data in a database table. Each of us wants to enter the bathroom is a transaction, simple hand washing, make-up and other operations can be considered a read operation, and the toilet operation can be considered a write operation.

Shared lock

Simple analogy introduced earlier relationship between the database and the bathroom, then the next continue to analyze what is shared lock.

Sometimes, if we go to the bathroom just want to wash their hands, we generally do not lock the door. While others can also come in hand washing and make-up. However, other people are not supposed to come to the toilet.

This is a shared lock, also called a read lock. When what we read operation on the data, in fact, does not change the value of the data.

So we can add to the database read locks, get a read lock on the transaction can read the data. When the database has been increased when other people read lock, other new transaction data can be read, but not write.

In other words, 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.

usage

In the back of increased query LOCK IN SHARE MODE, Mysql will query results in each row are shared locks.

SELECT ... LOCK IN SHARE MODE;

When no other thread line query result set using the exclusive lock, shared lock can be successfully applied, otherwise it will be blocked. Other threads can also be read using a shared lock table, and read these threads is the same version of the data.

Exclusive lock

After introduced over a shared lock, it said in a mutex.

If we just want to wash their hands into the toilet, then we can allow other people to come wash your hands. However, if we go to the bathroom on the toilet, then anyone can not come to do anything.

This is an exclusive lock, also called a write lock. When the transaction is our data write operation, must first obtain a write lock, write lock can be obtained either write data can also be read data. However, if the database has been increased exclusive write locks others, then subsequent transactions can not obtain any locks in the database.

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

usage

In the back of increased query FOR UPDATE, Mysql query will result in each row plus exclusive lock

SELECT ... FOR UPDATE;

When no other thread line query result set using the exclusive lock, it can be successfully applied for an exclusive lock, otherwise it will be blocked.

Lock principle

Take the MySql InnoDB engine, for the insert, update, deleteand other operations. Will be automatically added to the data involved in exclusive lock;

For general selectstatement, InnoDB will not add any locks, the transaction can be displayed to add a shared lock or exclusive lock in the following statement.

Shared locks:SELECT ... LOCK IN SHARE MODE;

Exclusive lock:SELECT ... FOR UPDATE;

Guess you like

Origin www.cnblogs.com/fanghl/p/11316540.html