1. Why concurrency control?
Databases share resources, and many transactions are usually running at the same time. When multiple transactions concurrently access the database, the same data will be read and/or modified at the same time. If concurrent operations are not controlled, incorrect data may be accessed and stored, compromising Database Consistency. Therefore, the database management system must provide a concurrency control mechanism.
2. Which types of data may be inconsistent during concurrent operations?
- Loss of Modification
The two transactions TL and T2 read and modify the same data. The results of T2 commit destroy (overwrite) the results of TL commit, resulting in the loss of TL modifications.
- Non-repeated read
After the transaction TL reads data, the transaction T2 performs the update operation, so that the TL cannot reproduce the previous read results.
- Read "dirty" Data
Transaction TL modifies a certain data and writes it back to the disk. After transaction T2 reads the same data, TL is revoked for some reason. At this time, Tl has modified the original data recovery value, the data read by T2 is inconsistent with the data in the database, and the data read by T2 is "dirty", that is, the data is incorrect.
3. Solve Concurrency Control
- Lock
Blocking means that transaction t sends a request to the system before performing operations on a data object, such as a table or record, to lock it. After the lock, transaction T has some control over the data object. Before transaction t releases its lock, other transactions cannot update the data object. Blocking is a very important technology for implementing concurrency control. There are two basic lock types: exclusive locks and share locks ).
- Timestamp sorting
You can choose a sequence between each pair of transactions to guarantee serializability. Each transaction in the system corresponds to a unique fixed timestamp. The timestamp of the transaction determines the serializable sequence of the transaction. In this way, if the transaction Ti timestamp is smaller than the transaction TJ timestamp, this mechanism ensures that the generated scheduling is equivalent to a serial scheduling before the transaction Ti appears before the transaction TJ. This mechanism ensures this by rolling back transactions that violate this order.
- Validity Check
A proper concurrency control mechanism. Each transaction in the system corresponds to a unique fixed timestamp. The serial sequence is determined by the transaction timestamp. In this mechanism, transactions are not delayed. However, to complete a transaction, it must pass the validity check. If the transaction does not pass the validity check, it is rolled back to the initial state.
- Multi-version concurrency
Create a new version for this data item when writing data items based on each transaction. When the read operation is sent, the system selects one of the versions to read. The timestamp and concurrency control mechanism ensure that versions to be read can be selected serially.
Reference:
Http://blog.csdn.net/xiangminjing/article/details/5922325
Http://www.cnblogs.com/ceys/archive/2012/03/16/2400745.html