存储过程中事务的问题
我有以下存储过程:
create procedure proc_aa
@a varchar(10).
@b varchar(10)
@ret int
as
begin tran
declare @id int
select @id=id from a
select @id = @id + 1
insert into b(a,id) values(@a,@id)
if @error <> 0
begin
rollback tran
set @ret=-1
return
end
update a set id=@id
if @error <> 0
begin
rollback tran
set @ret=-1
return
end
commit tran
set @ret=1
return
客户端调用此存储过程的频率很高的,现在两张表a和b总是死锁
------解决方案--------------------在存储过程里加上下面的设置:
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
设置事务隔离级别,在commit之前,其他事务只能读。
------解决方案--------------------create procedure proc_aa
@a varchar(10).
@b varchar(10)
@ret int
as
set lock_timeout 20000
set transaction isolation level read uncommitted
begin tran strat
declare @id int
select @id=id from a
select @id = @id + 1
insert into b(a,id) values(@a,@id)
if @error <> 0
begin
set @ret=-1
rollback tran strat
return
end
else
begin
update a set id=@id
if @error <> 0
begin
set @ret=-1
rollback tran strat
return
end
set @ret=1
commit tran strat
end
return
------解决方案--------------------LZ 可以加上 with(nolock) 试试看
select @id=id from a with(nolock)