如何避免多用户同时更改数据库?

sendltd 2003-10-17 09:11:28
在一个用户更改数据库内容的时候锁住其他用户,不叫其他人update。
是连接池自动替我们管理?还是需要手动--
...全文
184 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanslotliu 2003-10-17
  • 打赏
  • 举报
回复
建一张表,设置标志位,每次访问数据库,先访问标志位。
如果是只读,则不能修改。
否则,锁定、修改、释放。
sendltd 2003-10-17
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2218/2218218.xml?temp=.099621里面说
“现在一般不需要我们来设置锁了,数据库系统会自动帮助我们使用锁的,除非你自己有一些特殊的要求的时候才需要使用锁”
是不是说数据库可以自动替我们管理多用户状态,加锁是多余的?
zjcxc 2003-10-17
  • 打赏
  • 举报
回复
sp_dboption 'single user','1'

一个数据库,只允许一个用户连接(单用户使用)



如果你是想仅数据处理时,防止其他用户使用,就用锁的方法(一楼的)
伍子V5 2003-10-17
  • 打赏
  • 举报
回复
sp_dboption 'single user','1'
txlicenhe 2003-10-17
  • 打赏
  • 举报
回复

http://expert.csdn.net/Expert/topic/2218/2218218.xml?temp=.099621
/********** 加锁 ***************
设table1(A,B,C)
A B C
a1 b1 c1
a2 b2 c2
a3 b3 c3

1)排它锁
新建两个连接
在第一个连接中执行以下语句
begin tran
update table1
set A='aa'
where B='b2'
waitfor delay '00:00:30' --等待30秒
commit tran
在第二个连接中执行以下语句
begin tran
select * from table1
where B='b2'
commit tran

若同时执行上述两个语句,则select查询必须等待update执行完毕才能执行即要等待30秒

2)共享锁
在第一个连接中执行以下语句
begin tran
select * from table1 holdlock -holdlock人为加锁
where B='b2'
waitfor delay '00:00:30' --等待30秒
commit tran

在第二个连接中执行以下语句
begin tran
select A,C from table1
where B='b2'
update table1
set A='aa'
where B='b2'
commit tran

若同时执行上述两个语句,则第二个连接中的select查询可以执行
而update必须等待第一个连接中的共享锁结束后才能执行 即要等待30秒

3)死锁
增设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

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


--------------------------------------------------------------
SET IMPLICIT_TRANSACTIONS ON --用户每次必须显式提交或回滚。否则当用户断开连接时,
--事务及其所包含的所有数据更改将回滚

SET IMPLICIT_TRANSACTIONS OFF --自动提交模式。在自动提交模式下,如果各个语句成功
--完成则提交。

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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