星空网 > 软件开发 > 数据库

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

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

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

事务一:

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

事务二:

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

 




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

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流