update A set A.id=1 where A.name='zw' ----1
update A set A.id=2 where A.name='zw' ----2
我想实现在修改语句一的时候,语句二不能执行.请各位大哥给点建议吧.谢谢了.说详细一点.小弟万分感激.
[b]问题补充:[/b]
sybase数据库
create table abc(
cat_id int not null,
section_id varchar(10) not null,
attr_id varchar(10) not null,
key_attr_seq int not null,
constraint a primary key(cat_id,section_id,attr_id)
)
alter table abc add rid int identity
alter table abc lock [datarows] --加入行级锁
计算机A: begin tran
select * from abc
waitfor delay '00:00:20'
commit tran
计算机B:update abc set section_id='999' where rid=5
请问为何在A运行时机B仍然能修改表abc的数据呢
我想实现在A运行上面SQL时其它进程只能查看,不能更新A查询出来的数据.
谢谢.
[b]问题补充:[/b]
数据库表abc已经写死了,不能随意的添加字段.
[b]问题补充:[/b]
好象不行哦.constraint a primary key(cat_id,section_id,attr_id) 是复合主键盘,不行的.更新的是key_attr_seq 字段.
[b]问题补充:[/b]
key_attr_seq 字段是在UI上用户更新的字段.所以不能把它作为一个标识字段随意增加.能否给我提供怎么加锁呀.加哪种锁比较好.谢谢.
[b]问题补充:[/b]
恩,我试试.除了这种方法还有其它的办法吗.比如数据库的隔离级别或者加锁之类的.
[b]问题补充:[/b]
恩,十分感谢.
如果不是集群环境下使用的话,我上面提供的方法应该是最好的解决方案了。
没看明白
说的详细点,1、2有什么关系么?
传说中的脏读
可以考虑在程序上枷锁嘛~
这个问题很简单处理,如果是sybase只能使用乐观锁,版本控制原理了,加个varsion int类型的字段,每次查询时取出varsion,更新时加1,假如你查询出来这条记录的varsion为100,更新时这样写
[code="java"]update abc set section_id='999',varsion=varsion+1 where rid=5 and varsion=100.[/code]
这样如果在更新时这条记录已经被更新了,这里更新就不起作用了,具体要抛出异常还是怎么做可以由你选择。
看了下,不加字段也行啊,你现在不是更新section_id字段吗?可以把这个字段当版本字段用。
先这样查询
[code="java"]select key_attr_seq,cat_id,section_id,attr_id
from abc
where cat_id=? and section_id=? and attr_id=?[/code]然后这样更新
[code="java"]update key_attr_seq=?
where cat_id=? and section_id=? and attr_id=? and key_attr_seq=?[/code]
实在不行,可以自己写个内存锁(非集群环境使用),就是自己写个静态变量,把它当做版本字段,道理是一样的。
对应sybase的加锁机制不是很了解,你可以自己找找资料,但据我所知,sybase加了行级锁后是不能查询这条记录的,对并发影响很大,请慎重考虑。