码迷,mamicode.com
首页 > 数据库 > 详细

SQLServer并发问题,先SELECT后UPDATE,避免并发脏读情况解决

时间:2016-09-02 13:22:24      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

在SQL Server中,需要对数据操作进行先SELECT 之后UPDATE,对于这样的操作,如果出现高并发,可能导致脏读情况的发生。不能保证数据的同步。

解决方案是在事物中对表进行加更新锁:

事务一:

begin tran 
declare @count int =0
select @count=[Count] from tb_name WITH(UPDLOCK,HOLDLOCK) where id=1
select @count as count1
waitfor delay 00:00:30
update tb_name set [Count]=@count+1 where id=1
commit tran 
 
select * from tb_name

事务二:

begin tran
declare @count int =0
select @count=[Count] from tb_name WITH(UPDLOCK,HOLDLOCK) where  id=1
select @count as count2
update tb_name set [Count]=@count+1 where id=1
commit tran 
 
select * from tb_name

 

SQLServer并发问题,先SELECT后UPDATE,避免并发脏读情况解决

标签:

原文地址:http://www.cnblogs.com/heyangyi/p/5832902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!