数据库加锁操作_副本.doc

  1. 1、本文档共10页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
例: begin?? tran? ? ??????? insert?? 表??TABLOCKX???? --加锁? ? ??????? (字段列表)?? values(值列表)? ? ??????? commit?? tran 其他锁的示例? ? ?? ? ? --设tb(A,B,C)?? ? create?? table?? tb(A?? varchar(2),B?? varchar(2),C?? varchar(2))?? ? insert?? into?? tb?? ? select?? a1,b1,c1?? ? union?? all?? select?? a2,b2,c2?? ? union?? all?? select?? a3,b3,c3?? ??? ? --1)排它锁?? ? --在第一个连接中执行以下语句?? ? begin?? tran?? ??????? update?? tb?? ??????? set?? A=aa?? ??????? where?? B=b2?? ??????? waitfor?? delay?? 00:00:30???? --等待3秒?? ? commit?? tran?? ??? ? --在第二个连接中执行以下语句?? ? begin?? tran?? ??????? select?? *?? from?? tb?? ??????? where?? B=b2???????? ? commit?? tran?? ? --若同时执行上述两个语句,则select查询必须等待update执行完毕才能执行即要等待30秒?? ??? ? --2)共享锁?? ? --在第一个连接中执行以下语句?? ? begin?? tran?? ??????? select?? *?? from?? tb?? holdlock?? --holdlock人为加锁?? ??????? where?? B=b2???? ??????? waitfor?? delay?? 00:00:3???? --等待3秒?? ? commit?? tran?? ??? ? --在第二个连接中执行以下语句?? ? begin?? tran?? ??????? select?? A,C?? from?? tb?? ??????? where?? B=b2???? ??????? update?? tb?? ??????? set?? A=aa?? ??????? where?? B=b2???????? ? commit?? tran?? ? --若同时执行上述两个语句,则第二个连接中的select查询可以执行?? ? --而update必须等待第一个连接中的共享锁结束后才能执行?? 即要等待3秒?? ??? ? --3)死锁?? ? --增设tb2(D,E)?? ? create?? table?? tb2(D?? varchar(2),E?? varchar(2))?? ? insert?? into?? tb2?? ? select?? d1,e1?? ? union?? all?? select?? d2,e2?? ??? ? --在第一个连接中执行以下语句?? ? begin?? tran?? ??????? update?? tb?? ??????? set?? A=aa?? ??????? where?? B=b2???? ??????? waitfor???? delay?? 00:00:5?? ??????? update?? tb2?? ??????? set?? D=d5?? ??????? where?? E=e1???? ? commit?? tran?? ????????? ? --在第二个连接中执行以下语句?? ? begin?? tran?? ??????? update?? tb2?? ??????? set?? D=d5?? ??????? where?? E=e1???? ??????? waitfor???? delay?? 00:00:3?? ??????? update?? tb?? ??????? set?? A=aa?? ??????? where?? B=b2?????? ? commit?? tran?? ??? ???? ? --同时执行,系统会检测出死锁,并中止进程?? ? /*-------------------------------------------------------------?? ? SET?? IMPL

文档评论(0)

xiaofei2001128 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档