Computer Science An Overview _J. Glenn Brookshear _11th Edition

To solve such problems, a DBMS could force transactions to execute in
their entirety on a one-at-a-time basis by holding each new transaction in a
queue until those preceding it have completed. But a transaction often spends
a lot of time waiting for mass storage operations to be performed. By inter-
weaving the execution of transactions, the time during which one transaction
is waiting can be used by another transaction to process data it has already
retrieved. Most large database management systems therefore contain a
scheduler to coordinate time-sharing among transactions in much the same
way that a multiprogramming operating system coordinates interweaving of
processes (Section 3.3).

To guard against such anomalies as the incorrect summary problem and the
lost update problem, these schedulers incorporate a locking protocol in which
the items within a database that are currently being used by some transaction

are marked as such. These marks are called locks; marked items are said to be
locked. Two types of locks are common—shared locks and exclusive locks.
They correspond to the two types of access to data that a transaction might
require—shared access and exclusive access. If a transaction is not going to alter
a data item, then it requires shared access, meaning that other transactions are
also allowed to view the data. However, if the transaction is going to alter the
item, it must have exclusive access, meaning that it must be the only transaction
with access to that data.

In a locking protocol, each time a transaction requests access to a data
item, it must also tell the DBMS the type of access it requires. If a transaction
requests shared access to an item that is either unlocked or locked with a
shared lock, that access is granted and the item is marked with a shared lock.
If, however, the requested item is already marked with an exclusive lock, the
additional access is denied. If a transaction requests exclusive access to an
item, that request is granted only if the item has no lock associated with it. In
this manner, a transaction that is going to alter data protects that data from
other transactions by obtaining exclusive access, whereas several transactions
can share access to an item if none of them are going to change it. Of course,
once a transaction is finished with an item, it notifies the DBMS, and the asso-
ciated lock is removed.

Various algorithms are used to handle the case in which a transaction’s
access request is rejected. One algorithm is that the transaction is merely
forced to wait until the requested item becomes available. This approach, how-
ever, can lead to deadlock, since two transactions that require exclusive access
to the same two data items could block each other’s progress if each obtains
exclusive access to one of the items and then insists on waiting for the other. To
avoid such deadlocks, some database management systems give priority to
older transactions. That is, if an older transaction requires access to an item
that is locked by a younger transaction, the younger transaction is forced to
release all of its data items, and its activities are rolled back (based on the log).
Then, the older transaction is given access to the item it required, and the
younger transaction is forced to start again. If a younger transaction is repeat-
edly preempted, it will grow older in the process and ultimately become one
of the older transactions with high priority. This protocol, known as the
wound-wait protocol (old transactions wound young transactions, young
transactions wait for old ones), ensures that every transaction will ultimately
be allowed to complete its task.

Locking的更多相关文章

  1. MySQL 之 Metadata Locking 研究

    MySQL5.5 中引入了 metadata lock. 顾名思义,metadata lock 不是为了保护表中的数据的,而是保护 database objects(元数据)的.包括表结构.schem ...

  2. 数据库中的two phase locking

    数据库中的two phase locking 两段锁协议是指每个事务的执行可以分为两个阶段:生长阶段(加锁阶段)和衰退阶段(解锁阶段). 加锁阶段:在该阶段可以进行加锁操作.在对任何数据进行读操作之前 ...

  3. Double Checked Locking 模式

    转自:http://blog.csdn.net/wwsoon/article/details/1485886 之前在使用Double Check Locking 模式时,发现自己还是不太理解.于是写个 ...

  4. [zt]Singleton和Double-Checked Locking设计模式—UML图及代码实现

    Singleton和Double-Checked Locking设计模式,分别指的是单例模式和双重检查锁模式,它们都可以用于确保某个类只有一个对象实例化. 两个模式的区别在于:Singleton模式用 ...

  5. ConcurrentDictionary 对决 Dictionary+Locking

    在 .NET 4.0 之前,如果我们需要在多线程环境下使用 Dictionary 类,除了自己实现线程同步来保证线程安全之外,我们没有其他选择. 很多开发人员肯定都实现过类似的线程安全方案,可能是通过 ...

  6. SDK Manager failed to install 'java.exe' locking directory

    转自:http://stackoverflow.com/questions/13587478/sdk-manager-failed-to-install-java-exe-locking-direct ...

  7. 无锁编程(一) - Double-checked Locking

      Double-checked Locking,严格意义上来讲不属于无锁范畴,无论什么时候当临界区中的代码仅仅需要加锁一次,同时当其获取锁的时候必须是线程安全的,此时就可以利用 Double-che ...

  8. 锁之“轻量级锁”原理详解(Lightweight Locking)

    大家知道,Java的多线程安全是基于Lock机制实现的,而Lock的性能往往不如人意. 原因是,monitorenter与monitorexit这两个控制多线程同步的bytecode原语,是JVM依赖 ...

  9. 乐观锁(optimistic locking)与悲观锁(pessimistic locking)

    首先,乐观锁(optimistic locking)与悲观锁(pessimistic locking)基本是针对数据处理来说,也就是跟数据库有关的术语,目的是为了解决并发处理时所遇到的相关性能问题,以 ...

随机推荐

  1. ThinkPHP实现移动端访问自动切换主题模板

    ThinkPHP的模板主题机制,如果只是在PC,只要需修改 DEFAULT_THEME (新版模板主题默认是空,表示不启用模板主题功能)配置项就可以方便的实现多模板主题切换. 但对于移动端和PC端,也 ...

  2. loj 1025(记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25902 #include<iostream> #inc ...

  3. diff和common

    diff 命令 diff命令:找出两个文件的不同点,用于比较文件的差异 linux上非常重要的工具,一般用于制作补丁文件,特别是比较两个版本不同的文件以找到改动的地方. diff在命令行中打印每一个行 ...

  4. 模拟 Coder-Strike 2014 - Round 1 A. Poster

    题目地址:http://codeforces.com/problemset/problem/412/A /* 模拟:题目没看懂,但操作很简单,从最近的一头(如果不在一端要先移动到一端)往另一头移动,顺 ...

  5. SGU185 Two shortest(最小费用最大流/最大流)

    题目求一张图两条边不重复的最短路. 一开始我用费用流做. 源点到1连容量2费用0的边:所有边,连u到v和v到u容量1费用cost的边. 总共最多会增广两次,比较两次求得的费用,然后输出路径. 然而死M ...

  6. JavaBean中的get/set 的命名规范

      最近的struts项目中遇到了这样的问题: 我的action中全局变量明明有getset方法,而且是自动生成的,但是在使用的时候,总是说找不到这个属性的getset方法,取不到从jsp传来 的值, ...

  7. TYVJ P1063 数字串 Label:双指针 线性扫描

    描述 给你一个长度为n的数字串,数字串里会包含1-m这些数字.如果连续的一段数字子串包含了1-m这些数字,则称这个数字字串为NUM串.你的任务是求出长度最短的NUM串是什么,只需要输出这个长度即可.1 ...

  8. 蒟蒻修养之tc蓝名计划

    开一个新坑......(听说tc是智商高的人才能玩的QAQ显然我是被屠的... 1 [645DIV2]这个能说是裸模拟吗... 弃坑= =做了一些题感觉没必要放上来了= =等div1先吧....... ...

  9. Java/Js下使用正则表达式匹配嵌套Html标签

    转自:http://www.jb51.net/article/24422.htm 以前写过一篇文章讲解如何使用正则表达式完美解决Html嵌套标签的匹配问题(使用正则表达式匹配嵌套Html标签),但是里 ...

  10. MySQL 里面的Where 和Having和Count 和distinct和Group By对比

    mysql> select accid as uid,date(datetime) AS datetime from game.logLogin GROUP BY accid HAVING da ...