--设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:3' --等待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必须等待第一个连接中的共享锁结束后才能执行 即要等待30秒--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--删除临时表
drop table #tb,#tb2--同时执行,系统会检测出死锁,并中止进程
/*-------------------------------------------------------------
SET IMPLICIT_TRANSACTIONS ON --用户每次必须显式提交或回滚。否则当用户断开连接时,
--事务及其所包含的所有数据更改将回滚SET IMPLICIT_TRANSACTIONS OFF --自动提交模式。在自动提交模式下,如果各个语句成功
--完成则提交。
------------------------------------------------------------------------*/
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:3' --等待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必须等待第一个连接中的共享锁结束后才能执行 即要等待30秒--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--删除临时表
drop table #tb,#tb2--同时执行,系统会检测出死锁,并中止进程
/*-------------------------------------------------------------
SET IMPLICIT_TRANSACTIONS ON --用户每次必须显式提交或回滚。否则当用户断开连接时,
--事务及其所包含的所有数据更改将回滚SET IMPLICIT_TRANSACTIONS OFF --自动提交模式。在自动提交模式下,如果各个语句成功
--完成则提交。
------------------------------------------------------------------------*/
解决方案 »
- 【分享】,分享几个常用的分组函数,配简单事例。
- SQL 2005外围应用配置器 在 SQL 2008 的哪里
- 在多个表中查询相同字段后得到的记录集,如何在里面进行进一步的筛选?
- sql 复制数据库问题
- 一个已经定义好的数据库的日志文件大小能修改不?能改的话怎么改啊?
- 既然断开无线路由器与internet的连接后,与该路由器连接的各台电脑照样可以作为局域网进行互访,那就不需要建立有线局域网了,也不需要在墙上布线的麻烦了吗?更不需要交换机、集线器等之类的组网设备了,只要一个无线路由器和一些无线网卡即可组建小局域网了。
- 求一段带比较函数的select语句
- 这个where后面的条件怎么写?
- 关于不了解Microsoft Search问题
- 添加新数据
- 欢迎高级人才加盟中国制造业猎头网,为你打造高薪平台
- 请教高手
HOLDLOCK或TABLOCKX没有用啊