加锁的问题

treeforest 2003-11-26 09:55:17
加锁可以保证事务运行是数据库的一致性,可以防止死锁的发生。但是,加锁是在那个地方加呢?是在应用程序中间,还是在数据库内部?比如下面这种题,该如何解决?我编写过VB的数据库软件,当时都没有用到加锁,会有问题吗?
有三个事务:
T1:A=A+2 T2:A=A*2 T3:A=A**2
若这三个事务都遵守两段封锁协议,请给出一个产生死锁的调度。

谢谢!
...全文
70 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-11-26
  • 打赏
  • 举报
回复
--自己控制加锁

--锁定记录,只允许单用户修改的例子:

--创建测试环境
--创建测试表--部门表
create table 部门(departmentid int,name varchar(10))

--记录锁定表
create table lock(departmentid int,dt datetime)

go
--因为函数中不可以用getdate,所以用个视图,得到当前时间
create view v_getdate as select dt=getdate()
go
--创建自定义函数,判断记录是否锁定
create function f_chk(@departmentid int)
returns bit
as
begin
declare @re bit,@dt datetime
select @dt=dt from v_getdate
if exists(select 1 from lock where departmentid=@departmentid
and datediff(ss,dt,@dt)<5)
set @re=1
else
set @re=0
return(@re)
end
go

--数据处理测试
if dbo.f_chk(3)=1
print '记录被锁定'
else
begin
begin tran
insert into lock values(3,getdate())
update 部门 set name='A' where departmentid=3
delete from lock where departmentid=3
commit tran
end

--删除测试环境
drop table 部门
drop view v_getdate
drop function f_chk
zjcxc 元老 2003-11-26
  • 打赏
  • 举报
回复
--设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 --自动提交模式。在自动提交模式下,如果各个语句成功
--完成则提交。
------------------------------------------------------------------------*/
pengdali 2003-11-26
  • 打赏
  • 举报
回复
增设table2(D,E)
D E
d1 e1
d2 e2
在第一个连接中执行以下语句
begin tran
update table1
set A='aa'
where B='b2'
waitfor delay '00:00:30'
update table2
set D='d5'
where E='e1'
commit tran

在第二个连接中执行以下语句
begin tran
update table2
set D='d5'
where E='e1'
waitfor delay '00:00:10'
update table1
set A='aa'
where B='b2'
commit tran

同时执行,系统会检测出死锁,并中止进程

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧