Why databases require locking mechanism? What are locking mechanisms?

Lock concept:

First, let's understand under what is a database lock,

Before the transaction is a lock access request to Xianxiang system resources in a database (e.g., tables and records), the resource block,

After the transaction to acquire a lock, that is, to obtain control of the data, before the transaction releases its lock, other transactions can not update this data. After the withdrawal of the transaction, release the locked resources.

Why lock?

    The database is a shared resource used by multiple users, such as a user table t_user, two people in front of the browser logged in with an account number, phone number changed. When multiple users concurrently accessing the data, it will have a plurality of transactions simultaneously access the same data in the database. If uncontrolled concurrent operation may be read and store incorrect data, destroy consistency of the database (dirty reads, non-repeatable read, phantom read, etc.), a deadlock may occur. To solve this problem, the lock is a very important technology for achieving database concurrency control is a good program. Simply put, when executed sql statement before a transaction want to manipulate table records, Xianxiang database request for access to the record set your lock, before this transaction to release the lock, other transactions can not update the data.

Database lock Category:

Optimistic lock:

Optimistic locking is not 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.
Example:
single operation comprises three steps:
1. queried product information

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

2. The commodity information generating line
3. Product Review status 2

update t_goods

set status=2,version=version+1

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

Pessimistic locking:
with lock optimism is pessimism 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 locks:

S known locks or read locks, plus a shared lock data objects can be read by other transactions, but can not modify, typically the data object is read is completed, the lock is released immediately

Exclusive lock:

Also known as X lock or read lock, when data object is combined with exclusive lock, a transaction must be key in order to access the data object, until the end of the lock transaction were released. In between other transaction can not read and modify it.

Line Lock:
Line 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 = "
due to the city table, id field as the primary key, it is also equivalent to the index. When performing lock, this index will id record 1 plus the lock, the lock is a row lock.

Supplementary ==> ( deadlocks code )

Deadlock:

Deadlock refers to two or more processes in the implementation process, due to the competition for resources or A blocking phenomenon caused due communicate with each other, without external force, they will not be able to promote it.

These processes always waiting for another process called the deadlock

Generate the necessary conditions for deadlock:

1、互斥使用,即当资源被一个线程使用(占有)时,别的线程不能使用
2、不可抢占,资源请求者不能强制从资源占有者手中夺取资源,资源只能由资源占有者主动释放。
3、请求和保持,即当资源请求者在请求其他的资源的同时保持对原有资源的占有。
4、循环等待,即存在一个等待队列:P1占有P2的资源,P2占有P3的资源,P3占有P1的资源。这样就形成了一个等待环路。

死锁产生的原因:

1) 系统资源的竞争

通常系统中拥有的不可剥夺资源,其数量不足以满足多个进程运行的需要,使得进程在 运行过程中,会因争夺资源而陷入僵局

2) 进程推进顺序非法

进程在运行过程中,请求和释放资源的顺序不当,也同样会导致死锁。例如,并发进程 P1、P2分别保持了资源R1、R2,而进程P1申请资源R2,进程P2申请资源R1时,两者都 会因为所需资源被占用而阻塞。

3)信号量使用不当也会造成死锁。

进程间彼此相互等待对方发来的消息,结果也会使得这 些进程间无法继续向前推进。

如何避免死锁?

三种用于避免死锁的技术:

1)加锁顺序:

一个线程需要一些锁,那么它必须按照确定的顺序获取锁。它只有获得了从顺序上排在前面的锁之后,才能获取后面的锁。

例如,线程2和线程3只有在获取了锁A之后才能尝试获取锁C(获取锁A是获取锁C的必要条件)。因为线程1已经拥有了锁A,

所以线程2和3需要一直等到锁A被释放。然后在它们尝试对B或C加锁之前,必须成功地对A加了锁。

2)加锁时限 :

另外一个可以避免死锁的方法是在尝试获取锁的时候加一个超时时间,这也就意味着在尝试获取锁的过程中若超过了这个时限该线程则放弃对该锁请求。

并会进行回退并释放所有已经获得的锁,然后等待一段随机的时间再重试。这段随机的等待时间让其它线程有机会尝试获取相同的这些锁,

并且让该应用在没有获得锁的时候可以继续运行(加锁超时后可以先继续运行干点其它事情,再回头来重复之前加锁的逻辑)。

3)死锁检测:

每当一个线程获得了锁,会在线程和锁相关的数据结构中(map、graph等等)将其记下。除此之外,每当有线程请求锁,也需要记录在这个数据结构中。

当一个线程请求锁失败时,这个线程可以遍历锁的关系图看看是否有死锁发生。如果检测到死锁,就释放所有锁,回退,并且等待一段随机的时间后再重试

遇到死锁怎么办?

我们先了解下死锁定理: 
                     ①如果资源分配图中没有环路,则系统没有死锁; 
                     ②如果资源分配图中出现了环路,则系统可能有死锁。

从上面的死锁定理中我们可以知道只要打破死锁的环路就可以解开死锁,以下是处理死锁的两种名方法:

1)抢占资源:挂起某些死锁进程,并抢占它的资源,将这些资源分配给其他的死锁进程。但应防止被挂起的进程长时间得不到资源,而处于资源匮乏的状态。

2)终止(或撤销)进程:终止或撤销系统中的一个或多个死锁进程,直至打破死锁状态



(完)

 

Guess you like

Origin www.cnblogs.com/xiaofengwang/p/11291944.html